
Next.js / Vercel
Add one proxy.ts file to your Next.js app. It runs before every page request, reports crawler visits to Rankealo in the background, and never changes what humans see.
Prerequisites#
- A Next.js app (App Router or Pages Router) that you can deploy, on Vercel or self-hosted.
- Your site key and ingest URL from Rankealo → Traffic → Agent Analytics in the app (the key identifies your site, and you can regenerate it there). You set them as environment variables on your host (see Environment variables); the code only reads them, so it never contains a key. See the HTTP API reference for the full payload.
Steps#
- 1
Create the file
Createproxy.tsat the root of your project (next toapp/orpages/, or insidesrc/if you use it). On Next.js 15 and older the file ismiddleware.tsand the exported function must be namedmiddleware. - 2
Paste the snippet
Paste the snippet below. It reads its settings from environment variables, so it contains no keys. If you already have a proxy or middleware file, merge the crawler block into it. - 3
Set the environment variables
AddRANKEALO_BOT_TOKENandRANKEALO_INGEST_URLin your host's settings (details below). - 4
Deploy
Deploy (or redeploy) so the new variables are picked up.
import { NextResponse, type NextFetchEvent, type NextRequest } from "next/server";
const CRAWLER_UA = /bot|crawler|spider|chatgpt|gptbot|claude|perplexity|bing|google|applebot|bytespider|ccbot/i;
const STATIC_ASSET = /\.(?:js|css|png|jpe?g|gif|webp|svg|ico|woff2?)$/i;
export async function proxy(request: NextRequest, event: NextFetchEvent) {
// Set these in your host's environment settings. Missing values = tracking is off.
const token = process.env.RANKEALO_BOT_TOKEN;
const ingestUrl = process.env.RANKEALO_INGEST_URL;
if (token && ingestUrl && (request.method === "GET" || request.method === "HEAD")) {
const path = request.nextUrl.pathname;
const ua = request.headers.get("user-agent") ?? "";
if (!STATIC_ASSET.test(path) && CRAWLER_UA.test(ua)) {
const href = request.nextUrl.origin + path;
event.waitUntil(
fetch(ingestUrl, {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer " + token,
},
body: JSON.stringify({
domain: request.nextUrl.hostname,
href,
ai: { userAgent: ua, ip: request.headers.get("x-forwarded-for"), source: "server_middleware" },
}),
}).catch(() => {})
);
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
Connect GitHub instead
If your repo is on GitHub, you can connect it from Agent Analytics Setup and we can add this file for you in a pull request.Environment variables#
The code reads two environment variables. If any is missing it does nothing, so your site keeps working while you set them up.
RANKEALO_BOT_TOKEN: Per-site ingest key for this website. Keep it secret.RANKEALO_INGEST_URL: The Rankealo ingest endpoint the code posts crawler hits to.
Where to set them: Vercel: Project Settings → Environment Variables (Production), then redeploy. Self-hosted: your server or container environment.
The values are in Rankealo → Traffic → Agent Analytics, each with a copy button. Paste them into your host's settings yourself; never commit them to your repository.
What is sent#
- URLOrigin and path. The query string is never sent.
- User agentUsed to identify the crawler.
- IPFrom x-forwarded-for; hashed on arrival, never stored raw.
Only requests whose user agent looks like a crawler are reported, and static assets are skipped. Requests are sent with event.waitUntil, so the page response never waits for Rankealo.
Limitations#
- The matcher skips
/api,_next/static,_next/image, andfavicon.ico. Crawls of those paths are not counted. - Pages served straight from a CDN cache in front of Next.js without running the proxy are not seen. On Vercel, the proxy runs on every matched request.
- No status code is sent, because the proxy runs before the page renders.
Confirm data is flowing#
- 1In Rankealo, open Traffic → Agent Analytics.
- 2Press Check now in step 3. Once a crawler has visited, it shows Receiving data with the time of the last hit.
- 3Open the Bot traffic tab to see hits by crawler and by page.
The first hit depends on when a crawler next visits, which can take up to 24 hours. To test right away, request a page with a crawler user agent, for example curl -A "GPTBot/1.2" https://www.your-site.com/, then press Check now. A test like this is recorded as a claimed identity, like any hit without CDN verification.
Troubleshooting#
Nothing shows up after deploying+
src/) and is named correctly for your Next.js version: proxy.ts on 16+, middleware.ts with a middleware export on 15 and older.Check now says No data received+
Hits are skipped+
Still stuck? Contact support
