How to add privacy-first analytics to your Next.js app in 5 minutes
Add Measure to your Next.js app in under 5 minutes. No cookies, nothing stored on the visitor's device, and an MCP server for querying traffic from your AI tools.
Adding analytics to a Next.js app shouldn’t mean bolting on a cookie banner and a pile of consent plumbing. Here’s how to add Measure — cookieless, privacy-first and AI-queryable — in under 5 minutes.
Step 1: Create your account
Sign up at lets.measure.events. After confirming your email, add your site to get a tracking ID.
Step 2: Add the tracking script
The fastest path is adding the script to your layout.tsx (App Router) or _app.tsx (Pages Router). The plain HTML version is:
<script defer src="https://lets.measure.events/api/script/YOUR_TRACKING_ID"></script>App Router (Next.js 13+)
In your app/layout.tsx:
import Script from 'next/script'
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<Script
src="https://lets.measure.events/api/script/YOUR_TRACKING_ID"
strategy="afterInteractive"
defer
/>
</head>
<body>{children}</body>
</html>
)
}Replace YOUR_TRACKING_ID with the ID from your Measure dashboard.
Pages Router (Next.js 12 and below)
In your pages/_app.tsx:
import Script from 'next/script'
import type { AppProps } from 'next/app'
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Script
src="https://lets.measure.events/api/script/YOUR_TRACKING_ID"
strategy="afterInteractive"
defer
/>
<Component {...pageProps} />
</>
)
}Step 3: Verify tracking
Deploy your change and visit your site. In the Measure dashboard, you should see a pageview appear within seconds. If you’re running locally, set localhost as an allowed domain or test on your deployed preview URL.
Step 4 (optional): Track conversions
There’s nothing else to wire up in the browser. The script tracks pageviews automatically — including client-side navigation in both routers — plus time on page.
To track a conversion, give it a page: send people to a /thanks or /welcome route after signup and watch that path. For actions that happen on the server (a webhook, a completed checkout), send an event from your API route to the REST API:
// app/api/signup/route.ts
await fetch('https://lets.measure.events/api/v1/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.MEASURE_API_KEY}`, // mk_...
},
body: JSON.stringify({
site_key: process.env.MEASURE_SITE_KEY, // your site's tracking ID
path: '/events/signup',
}),
}).catch(() => {}) // never block the request on analyticsStep 5 (optional): Query from your AI tools
This is where Measure earns its keep. Once you’ve collected data, you can query it directly from Claude, Cursor, or any MCP-compatible tool.
Create an API key (it starts with mk_) and add the MCP server to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"measure-events": {
"url": "https://lets.measure.events/mcp",
"headers": {
"Authorization": "Bearer mk_your_api_key_here"
}
}
}
}Then ask Claude: “What pages are driving the most traffic to my Next.js app this week?”
Why not just use your host’s built-in analytics?
Host-bundled analytics is convenient but usually tied to that one platform, with no MCP server and tight event caps on lower tiers. Measure works on any hosting provider and is built to be queried programmatically.
Why not a cookie-based analytics tool?
A typical cookie-based tool means a consent banner, a complex event model, and visitor data sent to a third party’s servers. If your users are in the EU — or you just want a clean conscience — cookieless analytics is the better default. (If you have specific legal obligations, talk to your counsel.)
Performance impact
Measure’s tracking script is a couple of KB, loads with defer, and doesn’t block rendering. Your Core Web Vitals won’t notice it.
What you get
- Pageviews and unique visitors
- Top pages and referrer sources
- Humans, AI agents (ChatGPT-User, ClaudeBot, PerplexityBot and friends) and bots counted separately
- Real-time and historical data
- MCP server for AI-native querying
- No cookies and nothing stored on the visitor’s device, so no cookie banner needed for Measure
Measure is $29/month for up to 40 sites, with a 14-day trial — see pricing.
Let your agent read the numbers.
$29 a month covers 40 sites, then $0.50 a site. 14-day trial.