Frameworks · 2 min setup

How to add analytics to a Vue app

To add VisitTrack to a Vue app, paste the script tag into the <head> of your project's index.html. Vue Router in history mode (createWebHistory) has every route change counted as a pageview automatically; hash mode only records the first load.

Updated

A Vite-based Vue 3 project (what npm create vue@latest scaffolds) has a single index.html at the project root. That file is the page shell for every route, so the tag goes there once — not in App.vue, and not in a plugin. Using Nuxt? Follow the Nuxt guide, which configures the head in nuxt.config.ts.

Vue at a glance

Where the tag goes
index.html at the project root (Vite)
Router
createWebHistory() tracked automatically
Hash mode
createWebHashHistory() → use events per screen
Custom events
window.visitrack("name", props)
Vue CLI projects
public/index.html

How to install VisitTrack on Vue

  1. Step 1: Copy your site id

    From the install step when you add a site (or Settings → General), copy the snippet. SITE_ID below is the placeholder for your id.

  2. Step 2: Add the tag to index.html

    Inside <head>, before your app's module script. On an older Vue CLI (webpack) project, the file is public/index.html.

    index.html
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My Vue app</title>
        <script defer data-site="SITE_ID" src="https://visitrack.app/tracker.js"></script>
      </head>
      <body>
        <div id="app"></div>
        <script type="module" src="/src/main.ts"></script>
      </body>
    </html>
  3. Step 3: Check your router mode

    Open src/router/index.ts. If it uses createWebHistory(), you're done. If it uses createWebHashHistory(), read the navigation section below.

    src/router/index.ts
    import { createRouter, createWebHistory } from "vue-router";
    
    export default createRouter({
      history: createWebHistory(import.meta.env.BASE_URL), // tracked per route
      routes,
    });
  4. Step 4: Deploy and browse

    Run npm run build, deploy the dist folder and open the live URL. The dev server on localhost is ignored on purpose.

How to check VisitTrack is working on Vue

  1. On the deployed app, navigate between two routes with <RouterLink>s.
  2. Your VisitTrack live view shows an active visitor within seconds; the Pages tab lists both paths.
  3. In DevTools → Network you'll see one POST to visitrack.app/api/collect per route change.

Vue Router's web history calls history.pushState on every navigation; the tracker wraps it (and replaceState, and listens for popstate), so <RouterLink>, router.push() and back/forward each create a pageview. Pageviews are keyed on the pathname, so router.replace({ query: { sort: "new" } }) isn't counted as a new page.

With createWebHashHistory() the route lives after # and the pathname stays /, so route changes aren't separate pageviews. Either switch to createWebHistory() (your host must rewrite unknown paths to index.html), or send a custom event from a global afterEach hook as shown below.

src/router/index.ts (hash mode only)
// Only for hash-mode routers: one event per screen.
router.afterEach((to) => {
  window.visitrack?.("screen_view", { screen: String(to.name ?? to.path) });
});

How to track custom events and goals in Vue

In any component, call window.visitrack(name, props) once the action succeeds. In templates, data-vt-goal works on any element because it's a plain attribute — Vue passes it through to the DOM.

Component.vue
<script setup lang="ts">
async function subscribe(email: string) {
  const res = await fetch("/api/subscribe", { method: "POST", body: JSON.stringify({ email }) });
  if (res.ok) window.visitrack?.("newsletter_signup", { location: "footer" });
}
</script>

<template>
  <a href="/pricing" data-vt-goal="pricing_cta">See pricing</a>
  <section data-vt-scroll="testimonials_viewed">…</section>
</template>

For TypeScript, declare window.visitrack once in an env.d.ts (same declaration as in the React guide). Then turn any event into a goal in Settings → Goals.

Script options you might need on Vue

All optional — add them to the same tag. Full reference: script configuration.

AttributeWhat it does
data-cookielessStore nothing in the browser; the server derives a daily-rotating id. See cookieless mode.
data-exclude="/app/*"Never track these paths (comma-separated, * wildcard). Checked on every navigation.
data-allow-localTrack on localhost and *.local — for testing an install only.
data-debugLog every event the script sends, and why it skipped one, to the console.
data-auto="false"Turn off automatic events (downloads, contact clicks, form submits, rage clicks, JS errors, 404s).

Vue troubleshooting

Only / shows in the Pages tab

Your router uses hash history. Switch to createWebHistory() or add the afterEach event above.

Nothing tracked from npm run dev

Expected: localhost is ignored. Use npm run preview with data-allow-local temporarily, or test on a deployment.

Doubled pageviews

The tag exists twice — for example in index.html and injected again through useHead or a plugin. Keep one.

A CSP error mentions visitrack.app

Add https://visitrack.app to script-src and connect-src in the header your host sends.

Vue analytics FAQ

Is there a Vue plugin for VisitTrack?

No — it isn't needed. One tag in index.html covers pageviews and Vue Router navigation, and custom events are one global call. Fewer moving parts than a plugin with router hooks.

Does VisitTrack track Vue Router navigation automatically?

Yes, in history mode (createWebHistory). The script listens to the History API, so every route change is a pageview. Hash mode keeps the pathname unchanged, so use the afterEach event shown above.

Does it work with Vue 2?

Yes. The tag is framework-agnostic; Vue Router 3 in mode: "history" uses the same History API. Put the tag in public/index.html on a Vue CLI project.

Can I use VisitTrack with Vite's preview server?

Only if you add data-allow-local, because the tracker ignores localhost by default. Remove it before deploying so local testing never mixes into real numbers.

How do I exclude my own visits?

Open your live site once with ?vt_ignore=1 added to the URL. That browser stops sending data until you visit with ?vt_ignore=0.

Keep going

Add VisitTrack to your Vue site

Cookie-free analytics with revenue attribution, live visitors, funnels and session replays. One script tag, every feature on every plan. 14 days free, no card required.

Not on Vue? See all 29 integration guides.