Frameworks · 2 min setup
How to add analytics to a Vue app
<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.htmlat 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
Step 1: Copy your site id
From the install step when you add a site (or Settings → General), copy the snippet.
SITE_IDbelow is the placeholder for your id.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 ispublic/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>Step 3: Check your router mode
Open
src/router/index.ts. If it usescreateWebHistory(), you're done. If it usescreateWebHashHistory(), read the navigation section below.src/router/index.tsimport { createRouter, createWebHistory } from "vue-router"; export default createRouter({ history: createWebHistory(import.meta.env.BASE_URL), // tracked per route routes, });Step 4: Deploy and browse
Run
npm run build, deploy thedistfolder and open the live URL. The dev server onlocalhostis ignored on purpose.
How to check VisitTrack is working on Vue
- On the deployed app, navigate between two routes with
<RouterLink>s. - Your VisitTrack live view shows an active visitor within seconds; the Pages tab lists both paths.
- In DevTools → Network you'll see one
POSTtovisitrack.app/api/collectper route change.
Does VisitTrack track Vue page navigation?
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.
// 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.
<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.
| Attribute | What it does |
|---|---|
data-cookieless | Store 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-local | Track on localhost and *.local — for testing an install only. |
data-debug | Log 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
- Nuxt analyticsAdd VisitTrack to Nuxt 3 or Nuxt 4 through app.head in nuxt.config.ts — rendered on the server, with client-side route changes tracked automatically.
- React analyticsAdd VisitTrack to a React single-page app (Vite, Create React App) through index.html — React Router navigations are tracked automatically.
- Angular analyticsAdd VisitTrack to an Angular app in src/index.html — Router navigations are tracked automatically with the default path location strategy.
- SvelteKit analyticsAdd VisitTrack to SvelteKit by adding one tag to src/app.html — client-side navigations are tracked, and kit.csp gets two extra sources.
- Install the tracking scriptThe reference tag, CSP notes and how to check it's working.
- Custom events and signupsvisitrack(), data-vt-goal, scroll events and server-side signups.
- Use casesHow SaaS teams, indie hackers, stores and agencies use VisitTrack.
- VisitTrack vs Google AnalyticsCookie-free analytics with revenue attribution, compared honestly.
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.
14-day free trial · No card required · Cookie-free · Cancel anytime
Or explore the live demo first →Not on Vue? See all 29 integration guides.