Skip to content
Docs menuInstall CentPath › Install the script

Add CentPath to Next.js

On this page

Add CentPath to your Next.js app with two next/script tags, so every page of the app counts its visits.

Before you start

This guide covers both of Next.js's routers. If your app has an app folder, follow the App Router steps. If it has only a pages folder, follow the Pages Router steps.

In CentPath, open your site, go to Setup → Install and pick Next.js: the code there already holds your site's id.

1. Add the two scripts to your app

The first script is a one-line queue: it lets your code send goals before CentPath's script has loaded, and keeps them until it has. The second loads CentPath's script with your site's id. Keep both, the queue first.

App Router

  1. Open your root layout, app/layout.tsx, or src/app/layout.tsx if your app keeps its code in src.
  2. Import Script from next/script.
  3. Add the two <Script> tags inside <body>, after {children}. Keep the rest of your layout as it is.

If your app uses JavaScript (layout.js), leave out the type: write { children } in place of { children }: { children: React.ReactNode }.

This root layout holds both tags, so you can compare it with yours. Put your site's own id, from Setup → Install, in place of YOUR_SITE_ID:

import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script id="centpath-queue" strategy="beforeInteractive">
          {"window.analytics = window.analytics || Object.assign([], { q: [], goal: function () { this.q.push(arguments); } });"}
        </Script>
        <Script
          src="https://centpath.app/script/p.js"
          data-site-id="YOUR_SITE_ID"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}

The queue is beforeInteractive: Next.js runs it before your page becomes interactive, so a goal your components send at once is kept. CentPath's script is afterInteractive, the strategy Next.js suggests for analytics: it loads early, once Next.js has started to hydrate the page, so your page never waits for it.

Pages Router

  1. Open pages/_document.tsx, or create it, and add the queue inside <body>. Next.js runs a beforeInteractive script only from the custom Document.
import { Html, Head, Main, NextScript } from "next/document";
import Script from "next/script";

export default function Document() {
  return (
    <Html lang="en">
      <Head />
      <body>
        <Main />
        <NextScript />
        <Script id="centpath-queue" strategy="beforeInteractive">
          {"window.analytics = window.analytics || Object.assign([], { q: [], goal: function () { this.q.push(arguments); } });"}
        </Script>
      </body>
    </Html>
  );
}
  1. Open pages/_app.tsx, or create it, and add CentPath's script beside your page, so it loads on every page.

In _app.js, leave out import type { AppProps } from "next/app"; and : AppProps.

Put your site's own id, from Setup → Install, in place of YOUR_SITE_ID:

import type { AppProps } from "next/app";
import Script from "next/script";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script
        src="https://centpath.app/script/p.js"
        data-site-id="YOUR_SITE_ID"
        strategy="afterInteractive"
      />
    </>
  );
}

To send goals from your components, see Set up goals.

2. Deploy your app

Deploy your app the way you always do, so your live pages carry both scripts.

Verify the setup

  1. Open your live site in a new tab and load any page.
  2. Back in CentPath, look under the steps in Setup → Install. Waiting for your first pageview… turns into First pageview received. once the visit arrives.
  3. If it said First pageview received. before you started, your site has had visits already: look for this one in your dashboard instead. Reload it, and the visit shows in the Visits feed, usually within a minute.
  4. Click a link to another page of your app. Next.js changes the page without loading a new one, and CentPath counts it as a new page view: the script counts each new path, and the visitor's Back and Forward.

A change to the query string alone, like ?tab=2, isn't a new page view. Visits from localhost count too, whenever your development server's pages load the scripts.

Troubleshooting

Setup still waits for the first pageview

  1. Load a page of a deployment built after you added the scripts.
  2. CentPath's script tag isn't in your page's source, since next/script adds it in the browser. Look in your browser's developer tools instead, at the page's elements, and search them for your site's id.
  3. If your app sends a Content-Security-Policy, allow https://centpath.app in its script-src and connect-src, and give both <Script> tags your nonce, as the Content Security Policy guide in Next.js's own docs shows.

A goal from your code throws an error

The error means window.analytics doesn't exist yet: the queue is missing. Add the first <Script> as step 2 shows. It must sit in your root layout with the App Router, and in pages/_document.tsx with the Pages Router, since Next.js runs a beforeInteractive script from nowhere else.

Pages under another root layout count no visits

Your app has more than one root layout, and only one of them holds the scripts. Add both tags to each root layout.

For ad blockers, a wrong site id and other causes, see Troubleshooting.