Install on Next.js
Add XPmetric to Next.js App Router or Pages Router with a plain script tag in the root layout or _document.
Add XPmetric to Next.js so every route sends pageviews to your dashboard.
Replace YOUR_SITE_TOKEN with your site token from Settings → Sites → Get Script.
Tracking snippet
<script defer data-site="YOUR_SITE_TOKEN" src="https://xpmetric.com/p.js"></script>App Router (recommended)
Add a plain <script> tag inside <head> in the root layout, app/layout.tsx:
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<script
defer
data-site="YOUR_SITE_TOKEN"
src="https://xpmetric.com/p.js"
></script>
</head>
<body>{children}</body>
</html>
);
}Replace YOUR_SITE_TOKEN with your token from Settings → Sites → Get Script.
Pages Router
Add the same tag inside <Head> in pages/_document.tsx:
import { Html, Head, Main, NextScript } from "next/document";
export default function Document() {
return (
<Html lang="en">
<Head>
<script
defer
data-site="YOUR_SITE_TOKEN"
src="https://xpmetric.com/p.js"
></script>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}Use your site's token in place of YOUR_SITE_TOKEN.
Use a plain script tag, not next/script
The next/script component adds the tag in the browser, so it is not in the HTML your server sends. XPmetric's install check on the Verify screen reads that HTML, so a working next/script install can still read as "snippet not found". A plain <script defer> tag is part of the server HTML, does not block rendering, and the tracker follows route changes on its own.
Already installed with next/script? It keeps tracking. To make the install check see it, swap it for the plain tag above and remove the old <Script> so the tracker does not load twice.
Verify
- Deploy or run locally and visit any page.
- Open your site dashboard — you should see a pageview in the live feed within ~60 seconds.
Client-side navigation
The script follows Next.js route changes on its own — links, router.push, router.replace and the back/forward buttons each send a pageview when the page actually changes. You do not need to call anything.
- A new pageview needs a new path (or new UTM parameters). Re-rendering the same URL, changing only a hash, or changing only non-UTM query parameters like
?tab=annualdoes not count. - Sites that route with a hash (
/#/pricing) are tracked as a single page. - After the first pageview, the referrer is the previous page on your site, the same as a normal page load.
- Sign-in, verification and password-reset routes are never sent.
Troubleshooting
If pageviews never appear, your Content-Security-Policy may block p.js. See CSP troubleshooting.