# Install Statlark on Next.js

This is the markdown version of https://statlark.com/docs/install/nextjs. Every docs and Learn page has one: add .md to its address. Index: https://statlark.com/llms.txt

Add the tracker to your root layout with next/script, and it counts every page, client-side navigations included.

## <a id="where"></a>Where it goes

`app/layout.tsx` on the App Router, or `pages/_app.tsx` on the Pages Router.

## Steps

1. Open your root layout, `app/layout.tsx`. In a layout you already have, add only the import and the `Script` element below; keep everything else as it is.
2. Import `Script` from `next/script` and add it inside `<body>`, after `{children}`, with your site ID from Settings → Install. Next.js passes `data-website-id` through to the final script tag.
3. Keep `strategy="afterInteractive"`. It's the default, and it's the one Next.js recommends for analytics. `lazyOnload` waits for idle time, so it can miss short visits.
4. That's all. Next.js loads the script once and keeps it as people move between pages, and the tracker counts each client-side navigation as a pageview, so you don't add any routing code.
5. On the Pages Router, add the same `Script` to `pages/_app.tsx` instead.

app/layout.tsx

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

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://statlark.com/script.js"
          data-website-id="YOUR_SITE_ID"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}
```

pages/\_app.tsx (Pages Router)

```tsx
import type { AppProps } from "next/app";
import Script from "next/script";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script
        src="https://statlark.com/script.js"
        data-website-id="YOUR_SITE_ID"
        strategy="afterInteractive"
      />
    </>
  );
}
```

The tag sets a first-party analytics cookie as soon as it loads. Where the law asks for consent first, such as for visitors in the EU or UK, install the [consent banner](https://statlark.com/docs/consent.md) in its place: it loads the tracker only after the visitor accepts. Or switch on [cookieless mode](https://statlark.com/docs/script-configuration.md#cookieless), which stores nothing on the device.

For cookieless mode, add `data-cookieless="true"` to the `Script`.

## <a id="ai-prompt"></a>Hand this to your AI

For AI tools: this is the prompt a customer pastes in to start this task. If you are already following it, don’t start again: use the rest of this page as the guide.

Paste this into Claude Code, Cursor or another AI coding tool, in your project. Replace `YOUR_SITE_ID` with the ID from Settings → Install first. It never needs your API key.

```
Add Statlark analytics to this Next.js app. My Statlark site ID is YOUR_SITE_ID. If that isn't a real ID (they start with sl_), ask me for it. Use it in place of the guide's placeholder.

Follow this guide for the exact code: https://statlark.com/docs/install/nextjs.md
If that link doesn't open, try the same address without .md. If neither opens, stop and tell me. Don't write this from memory.

1. First check the site doesn't already load Statlark, whether through a script tag, consent.js, @statlark/web or a first-party proxy. If it does, stop and tell me.
2. Statlark sets first-party analytics cookies. Ask me whether this site has visitors in the EU or UK; you can't tell from the code. If it does, or I'm not sure, stop before adding it, because it may need to load through the consent banner: https://statlark.com/docs/consent.md
3. Add the guide's next/script tag once, in the root layout: app/layout.tsx on the App Router, or pages/_app.tsx on the Pages Router. Keep strategy="afterInteractive", and don't add any routing code: the tracker counts client-side navigations itself.
4. The tracker stays off on localhost. If you add data-allow-localhost="true" to test, take it out again before you finish.
5. If the site has a Content Security Policy (check its server or framework config and any <meta http-equiv="Content-Security-Policy"> tag), allow https://statlark.com for scripts and https://collect.statlark.com for connections.
6. Don't change anything else. Finish by telling me what you changed, and remind me to deploy, then confirm it in Statlark under Settings, Install, Verification.
```

## <a id="check"></a>Check it’s working

Deploy your site and open it. `next/script` adds the tag in the browser, so View Source won't show it. Look in your browser's developer tools instead: the Network tab should show `script.js` loading.

Then open Settings → Install in Statlark and look under Verification. It says “You're live” once the site's first event arrives, usually within seconds. On a site that already had traffic it's green already, so the real test is your own visit: open your live site, then find yourself on the Overview with the Now range. A browser you excluded with `?statlark_ignore=1` won't show up.

## Good to know

- `next dev` runs on localhost, where the tracker stays off. Add `data-allow-localhost="true"` while you test, and take it out before you ship.
- Have a Content Security Policy? Allow `https://statlark.com` in `script-src` and `https://collect.statlark.com` in `connect-src`, or the browser blocks the tracker.

## Other platforms

[WordPress](https://statlark.com/docs/install/wordpress.md) · [Shopify](https://statlark.com/docs/install/shopify.md) · [Webflow](https://statlark.com/docs/install/webflow.md) · [Google Tag Manager](https://statlark.com/docs/install/google-tag-manager.md) · [npm](https://statlark.com/docs/install/npm.md) · [anything else](https://statlark.com/docs/install.md#anything-else)

## Sources

These steps were checked against the pages below on 10 November 2026.

- [nextjs.org/docs/app/guides/scripts](https://nextjs.org/docs/app/guides/scripts)
- [nextjs.org/docs/app/api-reference/components/script](https://nextjs.org/docs/app/api-reference/components/script)
- [nextjs.org/docs/pages/guides/scripts](https://nextjs.org/docs/pages/guides/scripts)

Last reviewed 21 November 2026
