Frameworks
Vue analytics with CrispStats
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.
Updated
Set up CrispStats on Vue
- 01
Step 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. - 02
Step 2: Paste it into the head of index.html
In a Vue app created with Vite,
index.htmlsits at the project root, because Vite treats it as the entry point. Add the tag inside<head>, above your app’s module script. Keep it a classic script withdefer: the script reads its options from its own tag, sotype="module"does not work for it.Replace the highlighted values with your own.
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 app</title><scriptdeferdata-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> - 03
Step 3: Add data-hash if the router uses hash history
With
createWebHistory()you need nothing extra. `createWebHashHistory()` puts the route after#, which the script cuts from every URL unless the tag hasdata-hash. Without it, every route counts as the same page.index.html<scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-hashsrc="https:/ / in. crispstats. com/ d. js"></script> - 04
Step 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, savecrispstats.d.tsfrom the TypeScript page assrc/crispstats.d.tsso the call type-checks.src/components/UpgradeButton.vue<script setup lang="ts">function upgrade() {window.crispstats? . ( "upgrade clicked", { from: "settings" });}</script><template><button @click= "upgrade">Upgrade</ button></template> - 05
Step 5: Deploy and open the live site
Pages on
localhost,127.0.0.1andfile://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.
CrispStats on Vue
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.
- A pageview for every Vue Router navigation, including
Notes and limits
- 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. Useapp.head.scriptinnuxt.config.tsinstead: see Nuxt analytics. - The same URL twice in a row is one pageview, so a
router.replaceto 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-eventanddata-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.
- There is no Vue plugin and no npm package. The tag is the whole install, and components call
The docs page covers the optional steps this guide leaves out.
Setup questions. Straight answers.
What people ask before they connect it.
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.
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.
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.
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.
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.
Not answered here? Write to hello@crispstats.com
Go further
Related
- Nuxt analytics
Add CrispStats to a Nuxt app with one entry in app.head.script in nuxt.config.ts.
- Web analytics for developers
A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.
- Visitor journeys, page by page
Follow a visit page by page, from first referrer to purchase.
- Cookieless analytics explained
How cookieless analytics counts unique visitors with a daily rotating hash instead of a cookie, what it can and cannot tell you, and the trade-offs.
- Angular analytics
Add CrispStats to an Angular app with one script tag in src/index.html.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
Your traffic, crisp and clear
One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.