Skip to content
New Bring your history: import it from Google Analytics, Plausible, Simple Analytics, Umami, Matomo or GoatCounter, on every plan

Next.js

The @mettr/next package adds the browser script, serves it from your own domain, and sends crawler visits, 404s, errors and your events from the App Router.

Next.js 15.1 or later, App Router. Page views come from the browser script, served from your own domain; crawler visits (search engines and AI crawlers seldom run scripts), 404s and errors from the server. The source is on GitHub, MIT licensed.

Install

npm install @mettr/next

Set METTR_SECRET_KEY and METTR_PUBLIC_KEY (the site's settings, Tracking). Nothing is sent outside production unless METTR_ENABLED=true.

1. The script

// app/layout.tsx
import { Mettr } from '@mettr/next';

export default function RootLayout({ children }) {
    return (
        <html lang="en">
            <head>
                <Mettr />
            </head>
            <body>{children}</body>
        </html>
    );
}

2. The middleware

// proxy.ts (Next 16), or middleware.ts (Next 15) with `export const middleware`
import { withMettr } from '@mettr/next/middleware';

export const proxy = withMettr();

export const config = {
    matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
};

It serves the script and its events at /_live/script.js and /_live/events, so ad blockers mostly let them through, and sends each crawler's visit. Your own middleware goes inside: withMettr(async (request) => { ... }). Pass user to tie events to the signed-in person: withMettr(undefined, { user: (request) => ... }).

Middleware runs before the page, so a crawler's visit carries the status your middleware answered (a redirect), or 200: Next gives middleware no way to see the page's own.

3. 404s and errors

// app/not-found.tsx
import { reportNotFound } from '@mettr/next/server';

export default async function NotFound() {
    await reportNotFound();

    return <h1>Not found</h1>;
}
// instrumentation.ts
export { onRequestError } from '@mettr/next/server';

Errors go by their class only, never their message.

Your own events

On the server (server actions, route handlers), sent after the response:

import { track, trackRevenue } from '@mettr/next/server';

await track('signup', { plan: 'pro' }, { userId: user.id });
await trackRevenue('order', 49.5, 'EUR');

In the browser:

'use client';
import { track } from '@mettr/next/client';

track('export', { format: 'csv' });

Props are flat: up to 30, each a string, number, boolean or null.

Your MCP server

If your app answers MCP in a route handler (app/mcp/route.ts, Vercel's mcp-handler), wrap it outside its auth, so a 401 that sends a client to sign in counts too:

import { withMcp } from '@mettr/next/server';

const handler = createMcpHandler(/* your tools */);

export const GET = withMcp(handler);
export const POST = withMcp(handler, { userId: (request) => userFromToken(request) });

Each handshake, tool call, resource read and prompt becomes an mcp.* event with the client, how it ended and how long it took, sent through after(); never the arguments or the answer. The middleware sends no crawl for an MCP client's request. More in MCP analytics.

Anything else

getMettr() from @mettr/next/server gives the @mettr/node client, for subscriptions of your own billing, history and deploy notes.

Updated · This page as Markdown · Something unclear? [email protected]

Search the docs, features, comparisons, blog and changelog