@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
- A Siteline website key
- A Next.js project using the App Router or Pages Router
Setup
Add the middleware
Create or update your
middleware.ts file in the root of your project:middleware.ts
How it works
The package hooks into Next.js middleware, which runs on every matched request before the page renders:- A visitor (or AI bot) makes a request to your Next.js site
- The middleware extracts bot-relevant metadata (URL, user agent, IP, referrer)
- Tracking data is sent to Siteline asynchronously — the request is not awaited
- The page response is returned to the visitor unmodified
_next/static, _next/image, favicon.ico) are excluded via the route matcher.
Configuration
Your Siteline website key. Found under Agent Analytics > Manage Configuration in your Siteline workspace.
Next.js route matcher config. The default pattern excludes API routes and static assets. Adjust to match the routes you want to track.
Troubleshooting
Middleware not tracking visits
Middleware not tracking visits
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.Where do I find my website key?
Where do I find my website key?
Navigate to Agent Analytics in your Siteline workspace and click Manage Configuration to view or regenerate your key.
I already have middleware — will this conflict?
I already have middleware — will this conflict?
No. Call
gptrendsTrack(request) alongside your existing logic. It runs asynchronously and does not modify the request or response.Will this slow down my site?
Will this slow down my site?
No. The tracking call is fire-and-forget — it is not awaited and never blocks the response. All errors are silently caught.