Skip to content

Connect a Cloudflare Worker

Updated 29 November 2026

Deploy a small Cloudflare Worker that reports requests from AI crawlers to Brandexa, for zones without Logpush.

What to know

A Cloudflare Worker runs in front of your site and sends Brandexa one record for each request from a recognised crawler. Use it when your Cloudflare plan does not include Logpush. The Worker passes every response through unchanged.

Before you start

  • Your domain is active and proxied on Cloudflare.
  • You have a Cloudflare API token with Account Workers Scripts Edit, Zone Workers Routes Edit and Zone Read. DNS Read is optional and lets Brandexa check that your records are proxied.

Requests to Workers count against your Workers plan. Brandexa never needs access to your account for this method, and a visitor to your site is never affected.

Guided setup

  1. In Brandexa, open Agent analytics, then Logs, and choose Connect request logs.
  2. Choose Cloudflare Worker, then paste your token.
  3. Choose the domain and confirm what Brandexa will create: the script and one route for the domain and its subdomains.
  4. Choose Connect. The token is used once and not stored.
  5. Choose Check status.

Run the commands yourself

The Request logs screen in Brandexa shows these commands with your domain and your own source key already filled in. The versions below use example.com and a placeholder so you can read them first.

Set your Cloudflare API token
export CF_TOKEN="<your Cloudflare API token>"
BASE="https://api.cloudflare.com/client/v4"
Find the zone and account for example.com
ZONE=$(curl -s --fail-with-body "$BASE/zones?name=example.com&status=active&match=all" \
  -H "Authorization: Bearer $CF_TOKEN")
ZONE_ID=$(echo "$ZONE" | jq -r '.result[0].id // error("no active Cloudflare zone found for example.com")')
ACCOUNT_ID=$(echo "$ZONE" | jq -r '.result[0].account.id')
echo "ZONE_ID=$ZONE_ID ACCOUNT_ID=$ACCOUNT_ID"
Upload the Worker script
curl -s --fail-with-body -X PUT "$BASE/accounts/$ACCOUNT_ID/workers/scripts/brandexa-agent-analytics-logs" \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/javascript" \
  --data-binary @- <<'JS'
// Brandexa agent analytics, version 1. Sends one record for each request from a recognised crawler.
const BX_ENDPOINT = "https://ingest.brandexa.com/v1/logs/worker";
const BX_KEY = "<YOUR_SOURCE_KEY>";
const BX_TOKENS = ["amazonbot","applebot","bingbot","bytespider","ccbot","chatgpt-user","claude-searchbot","claude-user","claudebot","duckassistbot","facebookbot","google-cloudvertexbot","googlebot","gptbot","meta-externalagent","meta-externalfetcher","mistralai-user","oai-searchbot","perplexity-user","perplexitybot"];

addEventListener("fetch", (event) => {
  event.respondWith(handle(event));
});

async function handle(event) {
  const response = await fetch(event.request);
  event.waitUntil(report(event.request, response));
  return response;
}

async function report(request, response) {
  try {
    const userAgent = request.headers.get("user-agent") || "";
    const lower = userAgent.toLowerCase();
    if (!BX_TOKENS.some((token) => lower.includes(token))) return;
    const url = new URL(request.url);
    const body = JSON.stringify({
      records: [{
        t: Date.now(),
        host: url.hostname,
        path: url.pathname,
        method: request.method,
        status: response.status,
        ua: userAgent,
        country: request.cf && request.cf.country ? request.cf.country : null,
        ray: request.headers.get("cf-ray"),
        bytes: Number(response.headers.get("content-length")) || null,
      }],
    });
    await fetch(BX_ENDPOINT, {
      method: "POST",
      headers: { "content-type": "application/json", "x-brandexa-source": BX_KEY, "x-brandexa-script-version": "1" },
      body,
      signal: AbortSignal.timeout(2000),
    });
  } catch (error) {
    // Never affects the visitor. Nothing is logged: the request is not ours to keep.
  }
}
JS
Remove an earlier Brandexa route, if there is one
curl -s --fail-with-body "$BASE/zones/$ZONE_ID/workers/routes" -H "Authorization: Bearer $CF_TOKEN" \
  | jq -r '(.result // [])[] | select(.script == "brandexa-agent-analytics-logs") | .id' \
  | while read -r ROUTE_ID; do
      curl -s --fail-with-body -X DELETE "$BASE/zones/$ZONE_ID/workers/routes/$ROUTE_ID" -H "Authorization: Bearer $CF_TOKEN"
    done
Create the route
curl -s --fail-with-body -X POST "$BASE/zones/$ZONE_ID/workers/routes" \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary '{"pattern":"*example.com/*","script":"brandexa-agent-analytics-logs","request_limit_fail_open":true}'

How the Worker behaves

  • It checks the user agent of each request against the list of crawlers Brandexa recognises. Requests from people and unknown bots are not reported.
  • The report is sent after the response, with a two second limit. An error or a slow Brandexa never changes what a visitor receives.
  • The route is created with fail open on, so your site keeps serving if the Workers request quota is used up.

Brandexa keeps only requests from recognised AI crawlers and search engines. For each one it stores the time, host, path without the query string, method, status, size, country and crawler name. It does not store visitor addresses, query strings, cookies or the user agent text.

If something goes wrong

  • You already have a Worker on the same route. Cloudflare runs one Worker per route, so choose a more specific pattern or combine the scripts.
  • To rotate the source key, deploy the script again from the status screen.
  • To remove it, delete the route and the script named brandexa-agent-analytics-logs.

Still need help?

If you have a question or something is not working, get in touch.

Contact us