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
Obtaining Cloudflare API token
Make sure you are logged in to the Cloudflare Dashboard with the correct account. Then navigate to the https://dash.cloudflare.com/profile/api-tokens page, locate and click the Create Token button in the top right:
Now proceed with the Create Custom Token flow:
Fill in the following information:
- API token name: ahrefs-bot-analytics
- Grant the following permissions:
- Account / Workers Script / Edit so we can upload the worker script for tracking bot visits
- Zone / Workers Routes / Edit so we can assign the worker script to run on requests
- Zone / Zone / Read so we can locate the zone to configure
- Account Resources can be set to Include / All Accounts, alternatively scope it to the one which hosts the website.
- Zone Resources can be set to Include / All Zones, alternatively select the Zone which corresponds to your website's domain.
The filled form should look like this:
Now click the Continue to summary button, the summary should look like this:
Now click the Create Token button and copy the generated token to the clipboard to use in the next steps.
See the Cloudflare documentation on how to create an API token for additional information.
Automatic setup
- In Ahrefs, go to Project Settings → Bot Analytics.
- Select Cloudflare Worker as the method.
- Paste your Cloudflare API token and click Connect.
Ahrefs will use your token to:
- Find the zone matching your project's domain.
- Deploy the Ahrefs analytics worker.
- 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
-
Go to your Cloudflare dashboard and select your website's domain.
-
Find your
$ACCOUNT_IDand$ZONE_IDin the dashboard. -
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
See Obtaining API token for detailed instructions.
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 3 — 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.