How to add analytics to your Next.js site
This adds measuremy.site, cookieless website analytics, to a Next.js site. It is one script tag of under 1 KB: no cookies, no npm package, no consent banner for the tracker. Every plan starts with a 30-day free trial, no card.
The quickest way: ask your coding agent
Add measuremy.site analytics to this project: connect the measuremy.site MCP server at https://a.measuremy.site/mcp for all my projects (claude mcp add --scope user; it signs in with OAuth, no key, and one sign-in covers every site), then tell me how to sign in. Details: measuremy.site/llms.txtThe agent connects to measuremy.site, you sign in through your browser, and it adds the tag below to the right file for you. To do it by hand, follow the steps.
1. Get your site key
Start a free trial and add your domain. The Setup tab shows your snippet with your site key filled in. The key is public and can only record visits, so it is safe in your source code.
2. Add the script tag
The quickest way is the component, which adds the tag with next/script (and leaves it out of next dev):
npm install next-measuremysite
Then put it once in the root layout, app/layout.tsx, inside <body>, or in pages/_app.tsx on the Pages Router:
import { Measuremysite } from "next-measuremysite";
// inside <body>, after {children}
<Measuremysite siteKey="pk_..." />
Or leave out siteKey and set NEXT_PUBLIC_MEASUREMYSITE_KEY. For events, import { track } from "next-measuremysite" and call track("signup") after a successful action.
Or add the tag by hand. App Router: put it inside <head> in the root layout, app/layout.tsx (or .jsx):
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script defer src="https://a.measuremy.site/t.js" data-key="YOUR_SITE_KEY"></script>
</head>
<body>{children}</body>
</html>
);
}
Pages Router: put the same tag inside <Head> in pages/_document.tsx.
Client-side navigation with <Link> and router.push is counted automatically, because the script listens for History API changes. You don't need next/script or a route-change hook.
3. Check it works
Publish the change, then open your live site. The first visit from your own domain verifies it and the visit shows up on the dashboard within seconds. Visits from localhost aren't counted, so test on the deployed site.
4. Track signups and other conversions
Call window.measuremysite.track from a client component, for example after a successful signup:
"use client";
declare global {
interface Window {
measuremysite?: { track: (name: string, props?: Record<string, unknown>) => void };
}
}
export function onSignedUp() {
window.measuremysite?.track("signup", { plan: "free" });
}
Or, with no JavaScript, add data-measuremysite-event="signup_click" to any link, button or form.
Any event counts as a conversion for the channel the visitor came from.
Other platforms, and funnels and campaign links, are in the install guide.
Try it on your site
30-day free trial, no card. Plans from $9/month. Paste one sentence into your coding agent, or add one script tag yourself.
Start free trial See the live demoAdd measuremy.site analytics to this project: connect the measuremy.site MCP server at https://a.measuremy.site/mcp for all my projects (claude mcp add --scope user; it signs in with OAuth, no key, and one sign-in covers every site), then tell me how to sign in. Details: measuremy.site/llms.txt
Free tools
- Analytics tag checker: Which analytics and pixels a site runs, plus duplicate and broken tags.
- HTTP status and redirect checker: Status codes, redirect chains and headers for up to 25 URLs.
- Free UTM link builder: Add utm_source, medium and campaign to any link.
More guides
- A website analytics MCP server: ask your agent about your traffic
- Add website analytics to your project with Claude Code
- Find out why your website traffic spiked
- Track clicks and find where visitors drop off, without writing code
- Cookieless analytics: how it works, and what you give up
- A simpler Google Analytics alternative for developers
- How to add analytics to your Astro site
- How to add analytics to your Vite, React or Vue site
- How to add analytics to your SvelteKit site
- How to add analytics to your Hugo site
- How to add analytics to your Docusaurus site
- How to add analytics to your WordPress site