Netlify
Add one Edge Function to your Netlify site. It runs in front of every page, including static and cached ones, reports crawler visits to Rankealo in the background, and lets everything else through untouched.
Prerequisites#
- A site deployed on Netlify from a Git repository (any framework, or plain static HTML).
- 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
Createnetlify/edge-functions/rankealo-bots.tsin your repository. If you changed the edge functions directory innetlify.toml, use that folder instead. - 2
Paste the snippet
Paste the snippet below. It reads its settings withNetlify.env.get(), so it contains no keys. Theconfigexport runs it on every path; nonetlify.tomlchange is needed. - 3
Set the environment variables
In Site configuration → Environment variables, addRANKEALO_BOT_TOKENandRANKEALO_INGEST_URL. - 4
Deploy
Push and let Netlify deploy. The function appears under Logs → Edge Functions.
// netlify/edge-functions/rankealo-bots.ts
import type { Config, Context } from "@netlify/edge-functions";
const CRAWLER_UA = /bot|crawler|spider|chatgpt|gptbot|claude|perplexity|bing|google|applebot|bytespider|ccbot/i;
const STATIC_ASSET = /\.(?:js|css|map|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf)$/i;
export default async (request: Request, context: Context) => {
// Site configuration > Environment variables. Missing values = tracking is off.
const token = Netlify.env.get("RANKEALO_BOT_TOKEN");
const ingestUrl = Netlify.env.get("RANKEALO_INGEST_URL");
const url = new URL(request.url);
const ua = request.headers.get("user-agent") || "";
const track =
Boolean(token && ingestUrl) &&
(request.method === "GET" || request.method === "HEAD") &&
!STATIC_ASSET.test(url.pathname) &&
CRAWLER_UA.test(ua);
if (!track) return; // continue to the site untouched
const response = await context.next();
const report = fetch(ingestUrl, {
method: "POST",
headers: { "content-type": "application/json", authorization: "Bearer " + token },
body: JSON.stringify({
domain: url.hostname,
href: url.origin + url.pathname,
ai: {
userAgent: ua,
ip: context.ip,
statusCode: response.status,
source: "server_middleware",
},
}),
}).catch(() => {});
// Background: never delays the response.
if (typeof context.waitUntil === "function") context.waitUntil(report);
return response;
};
export const config: Config = { path: "/*" };
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: Netlify: Site configuration → Environment variables (or `netlify env:set <NAME> <value>`), then redeploy.
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 context.ip; hashed on arrival, never stored raw.
- Status codeThe status your site returned.
Human visitors pass straight through: the function returns without touching the request. For crawler requests it fetches the page as usual and reports the hit with context.waitUntil, so the response never waits for Rankealo. Static assets (scripts, styles, images, fonts) are skipped.
Limitations#
- Edge Function invocations count toward your Netlify plan's usage, since the function runs on every request.
- Hits are marked as claimed identities (user agent only); Netlify does not pass a verified-bot flag.
- The snippet sends anything that looks like a crawler; Rankealo keeps known crawlers and drops the rest.
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#
The function does not run+
Check now says No data received+
Hits are skipped+
Still stuck? Contact support
