> AI agents: this is one page of nextanalytica's docs. The full index for agents: https://nextanalytica.app/llms.txt

# Next.js analytics

> Add analytics to Next.js with @nextanalytica/tag: install, events, consent and AI crawlers, counted without cookies until a yes and read by your agent.

Updated: 2026-12-01

To add analytics to a Next.js app, install `@nextanalytica/tag` and add it once in `app/layout.tsx`. It counts visits and client-side page views, where they came from and what they led to, without cookies until a visitor agrees, and your agent (Claude, Cursor, Codex) reads the numbers over MCP.

> **Tip:** Connected agents do this for you: ask "Add nextanalytica to my Next.js site". It calls `get_tag`, shows you the change and asks before committing.

## How do you add nextanalytica to Next.js?

Install the package:

```bash
pnpm add @nextanalytica/tag@1
```

Then add it once, in `app/layout.tsx` (App Router. On the Pages Router use pages/_app.tsx):

```tsx title="app/layout.tsx"
import { Analytics } from '@nextanalytica/tag/next'

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Analytics project="na_…" />
      </body>
    </html>
  )
}
```

`na_…` is your project's key, from **Site**. The package adds the same tag line and counts client-side navigation, so every route change is a page view.

## How do you send events?

Send the actions that matter from your code:

```ts
import { track } from '@nextanalytica/tag'

track('signup_completed', { plan: 'team' })
```

Calls made before the tag has loaded wait and go in order. Mark the events that count as conversions, and your agent reads conversion rates by channel and page. See [Custom events](/docs/tracking/custom-events).

## How does consent work?

The tag counts without cookies until the visitor answers your banner, so people who never click it are still counted. Pass the answer on every page:

```ts
import { consent } from '@nextanalytica/tag'

consent(true) // or false
```

A yes starts a first-party id; a no stops the tag sending and removes what it kept. See [Next.js cookies and analytics](/docs/tracking/nextjs-cookie-consent). More in [Cookieless counting and consent](/docs/tracking/cookieless-and-consent).

## How do you record orders?

Where the order is confirmed:

```ts
import { order } from '@nextanalytica/tag'

order({ ref: 'A-10492', value: 129.9, currency: 'EUR' })
```

`ref` makes a repeated call count once. Orders your server confirms can go from the server instead: see [Revenue tracking](/docs/learn/revenue-tracking).

## Can you see AI crawlers?

AI crawlers such as GPTBot and ChatGPT-User never run the tag. A sender on your server passes their hits, so your agent sees which pages AI assistants read and which visits followed.

- **Next.js:** Next.js middleware: call `sendCrawlerHit` from `@nextanalytica/tag/next/crawlers` in `middleware.ts`, with a server key.
- **Behind Cloudflare:** a Cloudflare Worker works too.

See [AI crawlers](/docs/tracking/ai-crawlers) for the setup, and [AI traffic analytics](/docs/tracking/ai-traffic) for what you get.

## How do you check it works?

Ask your agent "Check my nextanalytica setup": `check_setup` lists the hosts that sent something and what to do next. Then ask:

- "How many visits did we have this week, and from where?"
- "Which pages do people land on from Google and from ChatGPT?"
- "Which channel brings sign-ups?"

## Next steps

- [Add the tag](/docs/tracking/installation)
- [Custom events](/docs/tracking/custom-events)
- [How to track website traffic](/docs/learn/track-website-traffic)

Canonical address: https://nextanalytica.app/docs/platforms/nextjs
