Docs
Search docs...⌘K

Cloudflare Worker

A Cloudflare Worker runs a script on each request that sends data to Ahrefs. This method works with any Cloudflare plan, including Free.

Prerequisites

  • A Cloudflare account with your website's domain added as a zone.
  • A Cloudflare API token with the following permissions:
    • Account → Workers Scripts → Edit
    • Zone → Workers Routes → Edit
    • Zone → Zone → Read

See How to create a Cloudflare API token.

Automatic setup

  1. In Ahrefs, go to Project Settings → Bot Analytics.
  2. Select Cloudflare Worker as the method.
  3. Paste your Cloudflare API token and click Connect.

Ahrefs will use your token to:

  1. Find the zone matching your project's domain.
  2. Deploy the Ahrefs analytics worker.
  3. Set up a route to run on all requests.

info

Your token is used once and not stored.

Manual setup

If you prefer not to provide an API token, you can deploy the worker manually using the Cloudflare API.

Step 1 — Get your Account ID, Zone ID, and API token

  1. Go to your Cloudflare dashboard and select your website's domain.
  2. Find your $ACCOUNT_ID and $ZONE_ID in the dashboard.
  3. Click "Get your API token" and create a new token with the following permissions:
    • Account → Workers Scripts → Edit
    • Zone → Workers Routes → Edit
    • Zone → Zone → Read

Step 2 — Deploy the worker

You can find the exact commands pre-filled with your project's details in Project Settings → Bot Analytics → Set up manually.

Step 1: Upload the worker script

curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/scripts/ahrefs--bot-analytics" \
  -X PUT \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/javascript" \
  --data-binary '
const WA_ENDPOINT = "https://analytics.ahrefs.com/api/cf_worker";
const WA_SCRIPT = "https://analytics.ahrefs.com/analytics.js";
const WA_CF_DATA_KEY = "YOUR_DATA_KEY";
const WA_DATA_KEY = "";
async function handleBotAnalytics(event, response) {
  let request = event.request;
  event.waitUntil(sendAnalytics(request, response));
  return response;
}

async function sendAnalytics(request, response) {
  try {
    let referer = request.headers.get("referer") || "";
    let userAgent = request.headers.get("user-agent") || "";
    let ip = request.headers.get("cf-connecting-ip") || "";
    let contentType = response.headers.get("content-type") || "";
    let payload = {
      n: "serverpageview",
      u: request.url,
      ip: ip,
      ua: userAgent,
      ct: contentType,
      m: request.method,
      s: response.status,
      k: WA_CF_DATA_KEY,
      r: referer,
    }
    let analyticsResponse = await fetch(WA_ENDPOINT, {
      method: "POST",
      body: JSON.stringify(payload)
    })
    if (!analyticsResponse.ok) {
      console.error("ahrefs bot analytics error:", analyticsResponse.status, analyticsResponse.statusText)
    }
  } catch (error) {
    console.error("ahrefs bot analytics error:", error.message)
  }
}
addEventListener("fetch", event => {
  event.respondWith((async () => {
    let response = await fetch(event.request);
    response = await handleBotAnalytics(event, response);
    return response;
  })());
});
'

Step 2: Create the route

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes" \
  -X POST \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary '{"pattern":"*youwebsite.com/*","script":"ahrefs--bot-analytics"}'

Replace yourwebsite.com with your domain.

warning

The WA_CF_DATA_KEY and route pattern are unique to your project. Copy the exact commands from your Project Settings → Bot Analytics page to ensure the correct values.

Step 4 — Verify the connection

After deploying the worker and creating the route, go back to Project Settings → Bot Analytics in Ahrefs and click Check status to verify that data is being received.