Skip to main content
Use the @gptrends/track middleware package to track AI bot visits on your Next.js site. The tracker runs in a fire-and-forget pattern inside Next.js middleware — requests are not awaited, so there is zero impact on page load times.

Prerequisites

Setup

1

Install the package

npm install @gptrends/track
2

Add the middleware

Create or update your middleware.ts file in the root of your project:
middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { SitelineTrackerNextJS } from "@gptrends/track/nextjs";

const gptrendsTrack = SitelineTrackerNextJS({
  websiteKey: "secret_key_...", // Replace with your website key
});

export async function middleware(request: NextRequest) {
  gptrendsTrack(request);
  return NextResponse.next();
}

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

Deploy

Deploy your application as usual. The middleware will begin tracking AI bot visits immediately.

How it works

The package hooks into Next.js middleware, which runs on every matched request before the page renders:
  1. A visitor (or AI bot) makes a request to your Next.js site
  2. The middleware extracts bot-relevant metadata (URL, user agent, IP, referrer)
  3. Tracking data is sent to Siteline asynchronously — the request is not awaited
  4. The page response is returned to the visitor unmodified
Static assets (_next/static, _next/image, favicon.ico) are excluded via the route matcher.

Configuration

websiteKey
string
required
Your Siteline website key. Found under Agent Analytics > Manage Configuration in your Siteline workspace.
matcher
string[]
Next.js route matcher config. The default pattern excludes API routes and static assets. Adjust to match the routes you want to track.

Troubleshooting

Ensure middleware.ts is in the root of your project (next to package.json), not inside src/ or app/. Next.js only picks up middleware from the project root.
Navigate to Agent Analytics in your Siteline workspace and click Manage Configuration to view or regenerate your key.
No. Call gptrendsTrack(request) alongside your existing logic. It runs asynchronously and does not modify the request or response.
No. The tracking call is fire-and-forget — it is not awaited and never blocks the response. All errors are silently caught.
Need help? Book a setup call and we’ll walk you through it.