Skip to content

Frameworks

Nuxt analytics with CrispStats

In a Nuxt app, analytics is one entry in app.head.script in nuxt.config.ts: add the CrispStats script there and deploy. Nuxt renders it into the head of every page, and client-side navigation goes through the History API, so route changes are counted with no module, plugin or middleware.

Updated

Set up CrispStats on Nuxt

  1. 01

    Step 1: Find your public key

    In the dashboard, open your site’s Settings and select Tracking code. Your public key starts with cs_pk_ and is safe to publish.

  2. 02

    Step 2: Add the script to nuxt.config.ts

    Add an entry to app.head.script. Nuxt’s app.head sets head tags for the whole app, so the tag is on every page, server-rendered or not. Replace the key and domain with yours. If the config already has an app.head block, add the entry to its script array.

    Replace the highlighted values with your own.

    nuxt.config.ts
    export default defineNuxtConfig({app: {head: {script: [{src: "https://in.crispstats.com/d.js",defer: true,"data-site": "cs_pk_your_site_key","data-domains": "example.com",},],},},});
  3. 03

    Step 3: Turn on more tracking (optional)

    Each option is a key with an empty string value, which switches it on. The switches in Settings, Tracking code add data-outbound, data-downloads, data-vitals, data-errors and data-404. Leave Hash routing off: Nuxt routes with real paths.

    nuxt.config.ts
    export default defineNuxtConfig({app: {head: {script: [{src: "https://in.crispstats.com/d.js",defer: true,"data-site": "cs_pk_your_site_key","data-domains": "example.com","data-outbound": "","data-vitals": "","data-errors": "",},],},},});
  4. 04

    Step 4: Send a custom event (optional)

    Call the global window.crispstats 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 as app/crispstats.d.ts in Nuxt 4. To keep calls made before the script loads, run the queue stub from a client plugin, plugins/crispstats.client.ts.

    components/UpgradeButton.vue
    <script setup lang="ts">function upgrade() {window.crispstats?.("upgrade clicked", { from: "settings" });}</script><template><button @click="upgrade">Upgrade</button></template>
  5. 05

    Step 5: Deploy and open the live site

    Pages on localhost are ignored, so a visit to nuxt dev 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 each page load and 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, and the visit appears on Realtime. To see events locally, add "data-debug": "" to the entry 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.

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • What you get

    • A pageview for the first load and for every <NuxtLink>, navigateTo() and back-button navigation, with the previous page as referrer.
    • Entry pages by source, so you see which server-rendered page search and AI assistants such as ChatGPT send people to first.
    • A core script of about 2.2 KB gzipped, loaded with defer, so it never delays the first paint of a server-rendered page.
    • Optional web vitals (LCP, INP and CLS at p75), JavaScript errors, outbound links, downloads and 404 pages.
    • Revenue by source once you pass crispstats.id() to your checkout: see Stripe revenue attribution.
  • Notes and limits

    • CrispStats does not publish a Nuxt module. The config entry is the whole install, with nothing to update.
    • Write each flag in the config object with an empty string, "data-vitals": "", as above. Some flags, such as data-downloads, read their value as a list, so a stray value changes what they do.
    • Keep defer: true and do not set type: "module": the script reads its options from its own tag.
    • The script only runs in the browser. Server rendering, prerendering and static generation all ship the tag in the HTML, and the script counts the page when a visitor opens it.
    • Query strings are cut to UTM and a few other parameters before a URL is compared, so a filter change in the query is not a new pageview. Keep more with data-keep-params.
    • To count visitors whose ad blocker stops the collector host, serve the script from your own domain through your host: see proxy by host.

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 entry in app.head.script in nuxt.config.ts. There is no module to install, and route changes are counted without a plugin or route middleware.

  • The documented install is app.head in nuxt.config.ts, which puts the tag on every page with no code in your components. The tag only needs to be in the head once per page, with defer and its data attributes.

  • The script ignores localhost and automated browsers on purpose, so development runs stay out of your numbers. Test on a deployed URL, or turn on debug mode to log each event in the console without sending it.

  • Yes. Server rendering, prerendering and nuxt generate all ship the tag in the HTML of each page, and the script counts the page once a visitor opens it in the browser. Nothing runs on the server.

  • Yes, when it reaches a new URL: that is a pageview, one event, the same as a full page load. A query change the script ignores adds nothing. 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

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.