Skip to content

A package for Next.js

Cookie-free analytics for Next.js. One component.

Install @crustat/next, render one component in your root layout, and deploy. It works with the App Router and the Pages Router, and counts page changes on its own.

  • Next.js 13+
  • App Router and Pages Router
  • Page changes counted
app/layout.tsx
import Crustat from '@crustat/next';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        {children}
        <Crustat siteId="YOUR-SITE-ID" />
      </body>
    </html>
  );
}
npm install @crustat/next

Made for Next.js

Works the way your Next.js app works

No router setup, no hook to call, and no event to send by hand on each page change.

How page changes are counted
  • App Router or Pages Router

    One component, in your root layout or in _app.

  • Client-side page changes, counted

    A click on a link changes the page without a reload. Each one still shows up as a page view.

  • Quiet on localhost

    Local visits are skipped, so next dev never shows up in your numbers.

  • Ready for a Content-Security-Policy

    If you set one, allow https://stats.crustat.com in script-src and connect-src.

Setup

Set it up in three steps

About two minutes, start to finish. You need a Next.js project and your Crustat site ID.

Read the full setup guide
  1. Install the package

    Add it to your project like any other npm package.

    npm install @crustat/next
  2. Render it in your root layout

    With the App Router, render the component once inside <body> in app/layout.tsx. With the Pages Router, render it next to your page in pages/_app.tsx. You’ll find your site ID in Crustat under Site settings → Install.

    app/layout.tsx
    import Crustat from '@crustat/next';
    
    export default function RootLayout({
      children,
    }: {
      children: React.ReactNode;
    }) {
      return (
        <html lang="en">
          <body>
            {children}
            <Crustat siteId="YOUR-SITE-ID" />
          </body>
        </html>
      );
    }

    Render it once, in one of these two places, not on each page. Its spot in the layout doesn’t matter: Next.js loads the script itself once the page is ready.

  3. Deploy and visit your site

    Deploy as usual, then open a page on your live site. Your first visit shows up about a minute later.

    Visits from localhost are skipped on purpose, so next dev won’t show up in your stats. Preview deployments on another address are recorded but left out of your stats, unless you add that address to the site.

Under the hood

No magic. The same script.

The component loads Crustat’s script through Next.js’s own script loader, once the page is ready. It’s the same script, with the same site ID, as the line you’d paste by hand, and Next.js keeps it loaded as people move around your site.

You write
<Crustat siteId="YOUR-SITE-ID" />
Next.js loads
<script src="https://stats.crustat.com/script.js"
  data-site="YOUR-SITE-ID"></script>
PropTypeWhat it does
siteId required string Your site ID, from the Install page of your Crustat dashboard.
src optional string Where the tracker loads from. Leave it out.

Beyond page views

Track clicks and forms from your components

Page views, links to other sites and file downloads are counted with nothing to set up. For the moments you care about, add an attribute or one call.

All the ways to track events

A button or link

Add data-crustat-event with a name. Every click on it, or on anything inside it, is recorded.

Any component
<button data-crustat-event="Sign up">Sign up</button>

A form

Put the attribute on the form itself. It’s recorded when the form is sent. What people type is never read.

Any component
<form action="/contact" data-crustat-event="Contact">
  …
</form>

Anything else

Call crustat with a name where it happens, after checking that Crustat is there.

In a click handler
window.crustat && crustat("Purchase");

Rather not touch code? Pick the button on your site from the Events tab instead.

At a glance

The package, in short

Package
@crustat/next
Needs
Next.js 13 or newer, React 18 or newer
Routers
App Router and Pages Router
Adds to your pages
One script, no cookies
Loading
Through Next.js’s script loader, once the page is ready
Page changes
Counted on their own
Local development
Visits from localhost are skipped
Licence
MIT

Questions and answers

Good to know

Read the full setup guide
Does it work with both the App Router and the Pages Router?

Yes. With the App Router, render the component in app/layout.tsx. With the Pages Router, render it in pages/_app.tsx.

Does the component go in <head> or <body>?

In <body>. Next.js loads the script itself once the page is ready, so its spot in the layout doesn’t matter.

Where do I find my site ID?

In Crustat, open your site, go to Site settings → Install and click Copy ID next to Site ID.

Why don’t I see my visits from next dev?

Visits from localhost are skipped on purpose. Deploy, then visit a page on your live site and wait about a minute.

Are client-side page changes counted?

Yes. Crustat follows them on its own and records each one as a page view. There’s no router setup and no hook to call.

Are preview deployments counted?

Preview deployments on another address are recorded but left out of your stats, unless you add that address to the site.

I set a Content-Security-Policy. What do I allow?

Allow https://stats.crustat.com in script-src and connect-src, for example in middleware or next.config.js.

Start counting today.

Try Crustat free for 14 days. No card needed.

See what you'll pay

Every feature is on every plan, with no extras. Try it free for 14 days, no card needed.

Pricing details

Install in a minute

Paste one line in your site, or use our WordPress plugin or the Astro and Next.js packages.

Integration options