# Vue analytics with CrispStats

Updated 2026-11-23

A Vue app built with Vite gets analytics from one CrispStats script tag in the head of index.html, then a deploy. Vue Router changes the URL through the History API, which the script watches, so every route change is a pageview with no plugin, no package and no router guard.

## Steps

### 1. Copy the tag with your key

In the dashboard, open your site's **Settings** and select **Tracking code**. The tag there has your public key (it starts with `cs_pk_`) and your domain filled in. The key is safe to publish.

### 2. Paste it into the head of index.html

In a Vue app created with Vite, `index.html` sits at the project root, because [Vite treats it as the entry point](https://vite.dev/guide/). Add the tag inside `<head>`, above your app's module script. Keep it a classic script with `defer`: the script reads its options from its own tag, so `type="module"` does not work for it.

`index.html`

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My app</title>
    <script
      defer
      data-site="cs_pk_your_site_key"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>
```

### 3. Add data-hash if the router uses hash history

With `createWebHistory()` you need nothing extra. [`createWebHashHistory()`](https://router.vuejs.org/guide/essentials/history-mode.html) puts the route after `#`, which the script cuts from every URL unless the tag has `data-hash`. Without it, every route counts as the same page.

`index.html`

```html
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-hash
  src="https://in.crispstats.com/d.js"
></script>
```

### 4. Send a custom event (optional)

The script installs a global function, `window.crispstats`. Call it from a component. The optional call (`?.`) keeps the button working when an ad blocker stops the script. In TypeScript, save `crispstats.d.ts` from the [TypeScript page](/docs/install/typescript) as `src/crispstats.d.ts` so the call type-checks.

`src/components/UpgradeButton.vue`

```vue
<script setup lang="ts">
function upgrade() {
  window.crispstats?.("upgrade clicked", { from: "settings" });
}
</script>

<template>
  <button @click="upgrade">Upgrade</button>
</template>
```

### 5. Deploy and open the live site

Pages on `localhost`, `127.0.0.1` and `file://` are ignored, so a visit to the Vite dev server is not counted. Deploy, then open your production site in a browser without an ad blocker.

## Check that it works

In your browser's developer tools, the **Network** tab shows a `POST` to `https://in.crispstats.com/e` on the first load and on each route change, and the collector answers `202`. In the dashboard, the site's **Tracking code** tab shows **Receiving events.** with the time of the last event. To see events locally, add `data-debug` to the tag and `localhost` to `data-domains` while you develop: each event is logged in the console and nothing is sent. Remove both before you ship. See [localhost and testing](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for every Vue Router navigation, including `router.push`, `<RouterLink>` and the back and forward buttons.
- The source of each visit kept across Vue Router navigation, so a visitor from a newsletter or an AI assistant is credited once, not on every route.
- A core script of about 2.2 KB gzipped that loads outside your bundle, so your build size does not change.
- Time on page and scroll depth per route, sent before each navigation.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.

## Notes for Vue

- There is no Vue plugin and no npm package. The tag is the whole install, and components call `window.crispstats`.
- A Nuxt app does not have this `index.html`. Use `app.head.script` in `nuxt.config.ts` instead: see [Nuxt analytics](/integrations/nuxt).
- The same URL twice in a row is one pageview, so a `router.replace` to the current route adds nothing. Query strings are cut to UTM and a few other parameters before a URL is compared.
- For clicks you can skip JavaScript entirely: `data-cs-event` and `data-cs-prop-*` attributes on an element send an event with properties.
- The script only runs in the browser. If you prerender pages, view the page source to check the built HTML still carries the tag.
- To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see [proxy by host](/docs/tracking/proxy).

Full reference: [Vue in the docs](https://docs.crispstats.com/install/vue-nuxt)

## Frequently asked questions

### Is there a Vue plugin for this analytics?

No. CrispStats is one script tag in index.html. There is nothing to register with app.use, and Vue Router navigation is counted without a router guard.

### Why does every route show as the home page?

Your router most likely uses createWebHashHistory, which keeps the route after the hash. The script drops the hash unless the tag has data-hash. Add it and redeploy.

### Can I track button clicks without writing a method?

Yes. Put data-cs-event with the event name on the element in your template, and data-cs-prop attributes for properties. The script listens for clicks on the whole page, so components rendered later are covered too.

### Does it count pages if I prerender a Vue app?

Yes, as long as the prerendered HTML still carries the tag in its head: view the source of a built page to check. The script runs only in the browser and counts the page when a visitor opens it.

### What does it cost, and do staging builds count?

Staging copies on other hostnames are left out by data-domains, so they use no events. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Nuxt analytics](/integrations/nuxt)
- [Web analytics for developers](/for/developers)
- [Visitor journeys, page by page](/features/visitor-journeys)
- [Cookieless analytics explained](/blog/cookieless-analytics-explained)
- [Angular analytics](/integrations/angular)
