Next.js analytics without cookies: add LightMetrica with next/script
LightMetrica's script is about 2 KB, sets no cookies and tracks client-side navigation out of the box — a good fit for Next.js apps where every kilobyte counts.
Get your tracking ID
Sign up, add your production domain, and copy the counter ID from the settings. The plain HTML version of the code looks like this:
<script defer src="https://lightmetrica.com/lm.js" data-site="YOUR_ID"></script>
App Router
Add the script to your root layout with next/script:
// 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://lightmetrica.com/lm.js" data-site="YOUR_ID" strategy="afterInteractive" />
</body>
</html>
)
}
Pages Router
// pages/_app.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://lightmetrica.com/lm.js" data-site="YOUR_ID" strategy="afterInteractive" />
</>
)
}
Client-side navigation
Next.js changes pages with the History API. LightMetrica listens for pushState, replaceState and popstate, so
every route change is counted as a page view — no router events needed. Query strings and #anchors don't create new
page views.
Local development
The script doesn't send data from localhost by default, so development doesn't pollute your stats. Preview
deployments on other domains are ignored too: LightMetrica accepts visits only from the domain in your counter
settings and its subdomains.
Custom goals
Add data-lm-goal="Sign up" to any element, or call the API after a successful action:
window.lightmetric?.('goal', 'Sign up', { plan: 'pro' })
FAQ
Does it work with static export?
Yes — it's a client-side script, so output: 'export' and any static hosting work.
Will it affect Core Web Vitals? The script is small and loads after the page becomes interactive, so the impact is negligible.
Do I need a cookie banner for it? LightMetrica doesn't set cookies. Whether you need a banner depends on the rest of your stack and your jurisdiction — this isn't legal advice.
Try LightMetrica — free
Lightweight, independent, cookie-free analytics: the counter installs in a minute — on its own or next to your current tools. Every week — an AI-powered Smart Report: what changed and what to do next.