# 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. Requests from browsers are sent too; Ahrefs discards everything that is not a bot.

For each request the worker sends the URL, status code, content type and, for redirects, the `Location` response header, so Bot Analytics can show where a redirect sent the bot. A worker deployed before Location was added keeps sending the old data until you connect again or redeploy it.

## 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**
  - **Zone → DNS → Read** (optional, lets Ahrefs verify that your DNS records are proxied)

> **INFO**
> The worker only runs on requests that pass through Cloudflare. DNS records set to **DNS only** (grey cloud) send visitors straight to your origin, so Bot Analytics receives no data for them. Make sure the records for your domain are **Proxied** (orange cloud), see [proxy status](https://developers.cloudflare.com/dns/proxy-status/).

## Workers request quota

> **WARNING**
> The worker runs on every request to your site, so each request counts toward your Cloudflare Workers request quota. The Free plan allows 100,000 requests per day, shared by all workers on the account. A high-traffic site can use it up.

Ahrefs creates the route with `request_limit_fail_open` set to `true`. When the quota runs out, Cloudflare skips the worker and serves your site as usual. Bot Analytics receives no data until the quota resets. See [Workers limits](https://developers.cloudflare.com/workers/platform/limits/) for the quota on each plan.

If your site gets more traffic than the quota allows, upgrade to Workers Paid or use [Cloudflare Logpush](./cloudflare-logpush.mdx) on an Enterprise plan.

## 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:

![](../images/cf_worker_token1.png)

Now proceed with the **Create Custom Token** flow:

![](../images/cf_worker_token2.png)

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
  - **Zone / DNS / Read** (optional) so we can verify that your DNS records are proxied through Cloudflare
- 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:

![](../images/cf_worker_token3.png)

Now click the **Continue to summary** button, the summary should look like this:

![](../images/cf_worker_token4.png)

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](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/) for additional information.

## 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. Check that your DNS records are proxied through Cloudflare, and warn you if they are not (needs the optional **Zone → DNS → Read** permission).
3. Deploy the Ahrefs analytics worker.
4. 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](https://dash.cloudflare.com/) 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**

   See [Obtaining API token](#obtaining-cloudflare-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**.

<CloudflareWorkerSnippet />

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. If no data arrives, check that your DNS records are proxied.
