@aranx/ai-analytics package runs in the Next.js proxy. It records
requests from AI bots such as ChatGPT-User and PerplexityBot, and visits
from people who open your site from a link in an AI app. It sends nothing for
other traffic.
Before you start
- In Aranx, open AI Visibility for the website and create a server key. Aranx shows the key once.
- Next.js 13 or later. Next.js 16 calls the file
proxy.ts. Earlier versions call itmiddleware.ts.
Setup
1
Install the package
2
Set the key
Add the key to Add the same variable to your host, for example in Vercel > Project >
Settings > Environment Variables. Do not commit the key, and do not
prefix it with
.env.local for local development:.env.local
NEXT_PUBLIC_.3
Add the proxy
Create Before Next.js 16, name the file
proxy.ts next to the app directory (in src/ if your project
uses it):proxy.ts
middleware.ts and the export
middleware.4
Deploy
Deploy the site. Aranx shows the first bot visits on the AI visibility
page.
Client IP
Aranx counts a bot visit only when its IP is in the bot’s published IP ranges, so the proxy must know the client IP. On Vercel it readsX-Real-IP, which
Vercel sets, and you have nothing to do. On another host (OpenNext on
Cloudflare, Netlify, your own server) behind a proxy that is the only way to
reach the server, name the one header that proxy sets:
proxy.ts
x-forwarded-for the package takes the last address, the one
your proxy appended.
Keep an existing proxy
Pass your proxy towithAiTraffic. It records the request, then runs your
proxy and returns its response.
proxy.ts
matcher covers, so the matcher must
cover every public page. If your existing proxy must run only on some paths,
such as /dashboard, widen the matcher and call your proxy only for those
paths:
proxy.ts
Check the setup
Send a request with an AI user agent:What the package sends
For each AI request: the page URL without its query (it keepsutm_source),
the user agent, and the time. Bot visits also carry the client IP, which Aranx checks against the bot’s published IP ranges and does not store. The proxy
runs before the page, so events have no response status. Tracking never
throws and never delays the response.
