Skip to content
metryo

02Install guide

Add Metryo to a Next.js app

Put the script in your root layout. Route changes in the App Router and the Pages Router are counted automatically — there is no provider to wrap your app in.

  1. 01

    Create the website in your dashboard

    Choose Add website and enter its name and domain, without https://. You get a site ID that looks like site_k3v9x2m4q1. To try it on your own machine first, register localhost:3000 as the domain.

  2. 02

    Add the snippet

    app/layout.tsx
    import Script from "next/script";
    
    export default function RootLayout({ children }) {
      return (
        <html lang="en">
          <body>
            {children}
            <Script defer data-site="site_xxxxx"
              src="https://cdn.metryo.io/m.js" />
          </body>
        </html>
      );
    }

    Replace site_xxxxx with your own site ID — everything else is copied as it is.

  3. 03

    Open your site

    Load any page of your site in a browser. The dashboard's install screen checks every few seconds and unlocks the moment the first pageview arrives.

Counting visits that leave early

next/script adds the tag after the page has hydrated. A visitor who leaves before that happens is never counted. A plain deferred tag in <head> is part of the HTML the server sends, so it loads with the document instead:

app/layout.tsx
// app/layout.tsx
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <script
          defer
          data-site="site_xxxxx"
          src="https://cdn.metryo.io/m.js"
        />
      </head>
      <body>{children}</body>
    </html>
  );
}

On the Pages Router the same tag goes inside <Head> in pages/_document.tsx.

Route changes are already counted

Next.js navigates with history.pushState, which the script listens to, so router.push(), <Link> and the back button each record a pageview on their own. router.replace() counts only when the path changes — replacing just the query string, as filters and tabs do, is left alone. If a replace is a real navigation in your app, call metryo.pageview() at that point.

Sending an event

metryo.event() runs in the browser, so call it from a client component:

components/upgrade-button.tsx
"use client";

export function UpgradeButton() {
  return (
    <button
      onClick={() =>
        window.metryo?.event("Upgrade clicked", { plan: "growth" })
      }
    >
      Upgrade
    </button>
  );
}
metryo.d.ts
// metryo.d.ts — so TypeScript knows about window.metryo
type MetryoProps = Record<string, string | number | boolean>;

declare global {
  interface Window {
    metryo?: {
      event(name: string, props?: MetryoProps): void;
      pageview(): void;
    };
  }
}

export {};

Pasted it but nothing arrives?

  • An extension is blocking it. Ad blockers and privacy extensions drop analytics requests. Check in a private window with extensions disabled.
  • It is running on a different domain. A snippet only accepts events from the domain its website was registered with, and that domain's subdomains, so nobody else can send data in your name. Add a separate website for any other domain.
  • You muted yourself earlier. Setting metryo_ignore in localStorage silences the script in that browser. Remove the key to start sending again.
  • The monthly allowance ran out. Once an account passes its plan's allowance, new events stop being recorded until the next month. The allowance is shared across all of an account's websites.

Next: send custom events, turn on click tracking, or see every option the tag accepts.