Install Statlark on Next.js
Add the tracker to your root layout with next/script, and it counts every page, client-side navigations included.
Using an AI coding tool?Hand it the prompt below, or point it at this page as markdown: nextjs.md
Where it goes
app/layout.tsx on the App Router, or pages/_app.tsx on the Pages Router.
Steps
- Open your root layout,
app/layout.tsx. In a layout you already have, add only the import and theScriptelement below; keep everything else as it is. - Import
Scriptfromnext/scriptand add it inside<body>, after{children}, with your site ID from Settings → Install. Next.js passesdata-website-idthrough to the final script tag. - Keep
strategy="afterInteractive". It's the default, and it's the one Next.js recommends for analytics.lazyOnloadwaits for idle time, so it can miss short visits. - 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.
- On the Pages Router, add the same
Scripttopages/_app.tsxinstead.
app/layout.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)
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 in its place: it loads the tracker only after the visitor accepts. Or switch on cookieless mode, which stores nothing on the device.
For cookieless mode, add data-cookieless="true" to the Script.
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.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 devruns on localhost, where the tracker stays off. Adddata-allow-localhost="true"while you test, and take it out before you ship.- Have a Content Security Policy? Allow
https://statlark.cominscript-srcandhttps://collect.statlark.cominconnect-src, or the browser blocks the tracker.
Other platforms
WordPress · Shopify · Webflow · Google Tag Manager · npm · anything else
Sources
These steps were checked against the pages below on .
Last reviewed