Skip to content

Frameworks

Next.js analytics with CrispStats

One next/script element in app/layout.tsx adds CrispStats to a Next.js app: paste it, then deploy. Navigation between routes is tracked for you, so you need no usePathname effect and no npm package.

Updated

Set up CrispStats on Next.js

  1. 01

    Step 1: Copy the snippet with your key

    In the dashboard, open your site’s Settings, then Tracking code, and choose the Next.js tab. It shows the element below with your public key (it starts with cs_pk_) and your domain filled in.

  2. 02

    Step 2: Add the Script element to the root layout

    Open app/layout.tsx (or src/app/layout.tsx). Import Script and add the element inside <body>, after {children}. Change nothing else: metadata, fonts and providers stay as they are. On the Pages Router, add the same element to pages/_app.tsx, next to <Component {...pageProps} />.

    Replace the highlighted values with your own.

    app/layout.tsx
    import Script from "next/script";// Inside <body> in app/layout.tsx, after {children}.<Scriptsrc="https://in.crispstats.com/d.js"data-site="cs_pk_your_site_key"data-domains="example.com"strategy="afterInteractive"/>
  3. 03

    Step 3: Deploy and open your production site

    Deploy as you normally do, then open the production URL in a normal browser window. data-domains keeps preview deployments and copies of the site on other hosts out of your numbers; list more hostnames with commas.

  4. 04

    Step 4: Send a custom event (optional)

    Call the global from a client component. The optional call (?.) keeps the button working when an ad blocker stops the script. For typed calls and a queue that catches calls made before the script loads, add the two small files from the Next.js guide.

    app/pricing/upgrade-button.tsx
    "use client";export function UpgradeButton() {return (<button onClick={() => window.crispstats?.("upgrade clicked", { from: "pricing" })}>Upgrade</button>);}

Check that it works

Open your production site in a normal window. Your visit shows on the Realtime screen within a few seconds, and Settings, Tracking code shows Receiving events. Visits to next dev on localhost:3000 are not counted, so test on a deployed URL, or turn on debug mode to see each event in the console without sending it (test locally).

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

    • Pageviews for every route change: <Link>, router.push and the back button, read through the History API and the Navigation API.
    • Sources and channels for each route, so you see whether search, a launch post or an AI assistant such as Perplexity sent people to your docs or your pricing page.
    • A core script of about 2.2 KB gzipped, loaded after the page is interactive, so it never delays your first paint.
    • Optional web vitals (LCP, INP, CLS at p75), JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.
    • Revenue by source once you pass crispstats.id() to your checkout: see Stripe revenue attribution.
  • Notes and limits

    • Use strategy="afterInteractive". lazyOnload loads the script so late that short visits are missed, and beforeInteractive is not needed.
    • Do not give the element an id. Browsers expose an element’s id as a global, so id="crispstats" would make the global the script element until it loads.
    • In JSX, write flag attributes with an empty string: data-outbound="", not a bare data-outbound, which renders as data-outbound="true".
    • To count visitors whose ad blocker stops the collector host, proxy it through a route handler on your own domain, never a rewrite in next.config.ts, which would forward your visitors’ cookies. See proxy through your domain.
    • AI crawlers such as GPTBot do not run JavaScript. Report them from proxy.ts (middleware.ts before Next.js 16), which sends only the user agent and the path.
    • The proxy route handler runs as it is on Vercel, and on Cloudflare through OpenNext or vinext.

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 next/script element in your root layout. There is nothing to install, and route changes in the App Router and the Pages Router are tracked without extra code.

  • Yes. The Script element can sit in a server component such as app/layout.tsx, and next/script handles the client side. Custom events are sent from client components.

  • The script ignores localhost and automated browsers on purpose, so development and test runs do not pollute your numbers. Turn on debug mode in development to see each event in the browser console instead.

  • Yes. Add the same Script element to pages/_app.tsx, next to the page component. Route changes made by next/router are read from the History API, exactly as in the App Router.

  • Each route change is one pageview event, and preview deployments cost nothing because data-domains keeps them out. 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

Guides

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.