Sensor 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.
The Sensor ships as the @foglift/tracker package, and the @foglift/sensor CLI installs and wires it for you.
For recognized AI crawler or referral events only, the Sensor reports the page path, user agent, AI source classification, and event time. It uses no cookies, does not identify visitors, and strips query strings.
Fastest
1. Install with one command
Run this in the project root. Open AI Traffic while signed in to copy the command with your workspace identifier filled in.
npx @foglift/sensor@latest init --token YOUR_WORKSPACE_IDThe CLI detects Next.js, installs @foglift/tracker, and writes the request-boundary file for you. If middleware already exists it composes around it — your authentication, redirects, headers, and routing keep running — and it preserves the original source byte-for-byte in a neighboring *.foglift-original.* file.
On Next.js 16+ the request boundary is a proxy.ts file (Next.js renamed middleware.ts to proxy.ts), and the CLI writes proxy.ts there automatically; on older Next.js it writes middleware.ts. Either way the exported function is the only difference:
// proxy.ts (Next.js 16+ — the same code, renamed file)
import { trackAITraffic } from "@foglift/tracker/nextjs";
export const proxy = trackAITraffic({
siteToken: "YOUR_WORKSPACE_ID",
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};After deploying the patched site, rerun the command with --verify --url https://your-site to prove a new persisted event.
Fallback — add it by hand
2. Install the package
Prefer to wire it yourself? Install the @foglift/tracker runtime the CLI would have added.
npm install @foglift/tracker@^1.4.03. Add the generated boundary file
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. On Next.js 16+, save it as proxy.ts and rename the export to proxy.
// 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).*)"],
};4. 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 Sensor retains its report for the request lifetime with event.waitUntil, without blocking the response.
5. Build and verify
- Run the project tests and production build before deploying.
- Deploy the middleware, then open AI Traffic and keep the page listening.
- 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