# Cloudflare Logpush

Cloudflare Logpush sends HTTP request logs to Ahrefs automatically. It is the recommended method for high-traffic sites, but requires a **Cloudflare Enterprise** plan.

## Prerequisites

- A Cloudflare **Enterprise** plan on the zone you want to monitor.
- A Cloudflare API token with the following permissions:
  - **Zone → Zone → Read**
  - **Zone → Logs → Edit**
  - **Zone → DNS → Read** (optional, lets Ahrefs verify that your DNS records are proxied)
- For the [manual setup](#manual-setup) only: [`jq`](https://jqlang.github.io/jq/download/) installed locally, which the setup commands use to read and update your Cloudflare configuration.

> **INFO**
> Logpush only records 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/).

## Limits and costs

The Logpush job sends a log line for every request to your domain and its subdomains. Requests from browsers are sent too; Ahrefs discards everything that is not a bot.

> **WARNING**
> Cloudflare allows at most 4 Logpush jobs per zone. If your zone already has 4, setup fails until you delete one.

Cloudflare measures Logpush usage in uncompressed bytes delivered. Check your Enterprise contract for how it is billed, and see [Logpush](https://developers.cloudflare.com/logs/logpush/) for details.

## 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:
  - **Zone / Logs / Edit** so we can configure a Logpush job to push logs to Ahrefs Bot Analytics
  - **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
- Zone Resources can be set to **Include / All Zones**, alternatively select the Zone which corresponds to your website's domain.

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

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

![](../images/cf_logpush_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 Logpush** 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. Check for existing Logpush jobs.
4. Create a new job that sends logs to Ahrefs.
5. Add a custom log field rule to the zone, so that the logs include the bot verification request headers (`signature`, `signature-input` and `signature-agent`) and the `Location` response header, which shows where a redirect sent the bot. The job requests the `RequestHeaders` and `ResponseHeaders` fields to receive them. Existing rules on your zone are preserved.

> **INFO**
> A job created before the `Location` header was added does not send it. Connect again, or run the manual setup again, to replace the job and the rule.

> **INFO**
> Your token is used once and not stored.

## Manual setup

If you prefer not to provide an API token, you can set up the Logpush job manually using the Cloudflare API.

### Step 1 — Get your API token

1. Go to your [Cloudflare dashboard](https://dash.cloudflare.com/) and select your website's domain.
2. Click "Get your API token" and create a new token with the following permissions:
   - **Zone → Zone → Read**
   - **Zone → Logs → Edit**

   See [Obtaining API token](#obtaining-cloudflare-api-token) for detailed instructions.

> **INFO**
> You do not need to look up your Zone ID. The commands find the zone from your domain using the **Zone → Zone → Read** permission.

### Step 2 — Create the Logpush job

Fill your API token into the first block, then run each block below one at a time in your terminal, checking that each one completes without an error. Together they find your zone, create the Logpush job, and add the custom log field rule described in [Automatic setup](#automatic-setup). You can find the exact commands pre-filled with your project's details in **Project Settings → Bot Analytics → Set up manually**.

<CloudflareLogpushSnippet />

> **INFO**
> The `BotScore`, `BotScoreSrc` and `BotTags` fields are only available with a [Cloudflare Bot Management](https://developers.cloudflare.com/bots/plans/bm-subscription/) subscription. Bot Analytics works without them, though including them improves bot classification. If your zone does not have Bot Management, remove these three fields from `field_names` before creating the job.

> **WARNING**
> The `destination_conf` URL and property key 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 creating the job, 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.
