Rankealo

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. 1

    Create the file

    Create proxy.ts at the root of your project (next to app/ or pages/, or inside src/ if you use it). On Next.js 15 and older the file is middleware.ts and the exported function must be named middleware.
  2. 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. 3

    Set the environment variables

    Add RANKEALO_BOT_TOKEN and RANKEALO_INGEST_URL in your host's settings (details below).
  4. 4

    Deploy

    Deploy (or redeploy) so the new variables are picked up.
ts
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, and favicon.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#

  1. 1
    In Rankealo, open Traffic → Agent Analytics.
  2. 2
    Press Check now in step 3. Once a crawler has visited, it shows Receiving data with the time of the last hit.
  3. 3
    Open 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+
Check the file is at the project root (or in 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+
Request a page with curl -A "GPTBot/1.2" and check again. If it is still empty, confirm both environment variables are set for the production environment and match the values in Agent Analytics Setup, then redeploy.
Hits are skipped+
The hostname in the URL must belong to the website you set up in Rankealo. Preview deployments on *.vercel.app are ignored on purpose.

Still stuck? Contact support

Reading is step one. Measuring your AI visibility is step two.

Rankealo tracks how often your brand is mentioned and cited across the major AI engines, then helps you publish the pages that close the gaps.