# Next.js analytics with CrispStats

Updated 2026-11-23

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.

## Steps

### 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. 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} />`.

`app/layout.tsx`

```tsx
import Script from "next/script";

// Inside <body> in app/layout.tsx, after {children}.
<Script
  src="https://in.crispstats.com/d.js"
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  strategy="afterInteractive"
/>
```

### 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. 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](/docs/install/nextjs#send-events-with-types).

`app/pricing/upgrade-button.tsx`

```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](/docs/install/nextjs#test-locally)).

## 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](/integrations/stripe).

## Notes for Next.js

- 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](/docs/install/nextjs#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.

Full reference: [Next.js in the docs](https://docs.crispstats.com/install/nextjs)

## Frequently asked questions

### Do I need an npm package to add analytics to Next.js?

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.

### Does it work with the App Router and server components?

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.

### Why do I see no visits from next dev?

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.

### Does it work with the Pages Router?

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.

### What does it cost to track a Next.js app?

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.

## Related

- [Developers: web analytics that stays out of your code](/for/developers)
- [Web vitals from real visits](/features/web-vitals)
- [Error tracking in your analytics](/features/error-tracking)
- [CrispStats vs Vercel Analytics](/vs/vercel-analytics)
