Skip to main content

Tracker docs

Track AI traffic with Next.js

The server middleware records recognized AI crawler requests and recognized AI answer referrals. It uses a hostname-bound public workspace identifier, never a secret API key, and reporting does not delay the page response.

1. Install the tracker

npm install @foglift/tracker@^1.4.0

2. Add the generated middleware

Open AI Traffic while signed in to copy this file with your workspace identifier filled in. The matcher excludes static Next.js assets while keeping page requests observable.

// middleware.ts
import { trackAITraffic } from "@foglift/tracker/nextjs";

export const middleware = trackAITraffic({
  siteToken: "YOUR_WORKSPACE_ID",
});

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

3. Preserve existing middleware

If the project already has middleware, compose it through the next option. Do not replace existing authentication, redirects, headers, or routing behavior.

// middleware.ts
import { trackAITraffic } from "@foglift/tracker/nextjs";
import { existingMiddleware } from "./src/middleware-existing";

export const middleware = trackAITraffic({
  siteToken: "YOUR_WORKSPACE_ID",
  next: existingMiddleware,
});

Keep the Next.js request event parameter available. The tracker retains its report for the request lifetime with event.waitUntil, without blocking the response.

4. Build and verify

  1. Run the project tests and production build before deploying.
  2. Deploy the middleware, then open AI Traffic and keep the page listening.
  3. Request a non-static page with a recognized AI-crawler user agent. Foglift confirms the first server-observed event.

A crawler request proves the agent reached the page. It does not prove indexing, citation, recommendation, or model training.

Open AI Traffic