# Proxy CrispStats through a Cloudflare Worker

Updated 2026-11-23

To proxy CrispStats through Cloudflare, deploy a small Worker, add a route such as example.com/k3x9qe/* to it in the zone that serves your site, and point the script tag at that path with data-api. Ad blockers that stop known analytics hosts then see a request to your own domain, and the Worker passes on the visitor's address and location but never their cookies.

## Steps

### 1. Get your proxy path

In the dashboard, open **Settings**, **Tracking code**, **Proxy through your own domain**. It proposes a path made for your site, such as `/k3x9qe`, and fills it into every example. Use that path, not `/stats` or `/analytics`, which blockers can list as easily as a host. This works for any site behind Cloudflare, whatever serves it.

### 2. Deploy the Worker

Create a Worker with this code, with your path in `PREFIX`. It copies only the headers analytics needs, takes the visitor's address from `cf-connecting-ip`, passes country, region and city from `request.cf`, and keeps the script in your zone's cache for a day.

`worker.js`

```js
// A Worker on the route example.com/k3x9qe/*. Sends only the headers below:
// never your visitors' cookies or authorization.
const PREFIX = "/k3x9qe/";
const UPSTREAM = "https://in.crispstats.com/p/";
const FORWARD = ["content-type", "user-agent", "accept-language", "referer", "origin", "if-none-match"];
const RETURN = ["content-type", "cache-control", "etag", "access-control-allow-origin"];

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    const headers = new Headers();
    for (const name of FORWARD) {
      const value = request.headers.get(name);
      if (value) headers.set(name, value);
    }
    headers.set("x-forwarded-for", request.headers.get("cf-connecting-ip") ?? "");
    const { country, regionCode, city } = request.cf ?? {};
    if (country && country !== "XX" && country !== "T1") headers.set("x-crispstats-country", country);
    if (regionCode) headers.set("x-crispstats-region", regionCode);
    if (city) headers.set("x-crispstats-city", encodeURIComponent(city));
    if (env.CRISPSTATS_PROXY_SECRET) headers.set("x-crispstats-proxy-secret", env.CRISPSTATS_PROXY_SECRET);

    const script = request.method === "GET" && url.pathname.endsWith(".js");
    const upstream = await fetch(UPSTREAM + url.pathname.slice(PREFIX.length) + url.search, {
      method: request.method,
      headers,
      body: request.method === "POST" ? request.body : undefined,
      // The script is the same for a day: keep it in this zone's cache.
      cf: script ? { cacheEverything: true, cacheTtl: 86400 } : undefined,
    });
    const out = new Headers();
    for (const name of RETURN) {
      const value = upstream.headers.get(name);
      if (value) out.set(name, value);
    }
    return new Response(upstream.body, { status: upstream.status, headers: out });
  },
};
```

### 3. Add the route in your zone

In the Cloudflare dashboard, open **Workers & Pages**, select the Worker, then **Settings**, **Domains & Routes**, **Add**, **Route**, choose the zone and enter the pattern with your domain and path. [Cloudflare's routes guide](https://developers.cloudflare.com/workers/configuration/routing/routes/) notes the hostname needs a proxied DNS record. With Wrangler, the same route goes in the config file. Only requests under the path reach the Worker; the rest of your site is untouched.

`wrangler.toml`

```toml
[[routes]]
pattern = "example.com/k3x9qe/*"
zone_name = "example.com"
```

### 4. Point the script tag at the path

Replace the tag on every page. `src` loads the script from your domain and `data-api` sends events and visitor id requests to the same path. `data-api` is required: without it the script posts to `/e` on your own domain, and those requests fail. Keep any other attributes you use, such as `data-outbound`.

```html
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-api="/k3x9qe"
  src="/k3x9qe/d.js"
></script>
```

### 5. Sign the requests (optional)

Reveal the proxy secret (`cs_px_…`) under **Advanced: sign proxied requests**, then run `npx wrangler secret put CRISPSTATS_PROXY_SECRET` and redeploy. The Worker sends it when it is set, which proves a forwarded request came from your proxy and lets signed traffic skip the per-site ceiling.

## Check that it works

Open `https://example.com/k3x9qe/check?k=cs_pk_your_site_key` with your domain, path and key, in a browser on your own site. Look for `"visitorAddressForwarded": true`, `"cookiesForwarded": false` and `"location": { "source": "proxy" }` (and `"signed": true` if you sign). Then filter the network panel by your path: `d.js` loads with `200` or `304`, and the `POST` to `/k3x9qe/e` answers `202`.

## What you get

- Visitors whose [ad blocker](/glossary/ad-blocker) stops requests to known analytics hosts, counted again, because the browser only talks to your domain.
- Country, region and city from Cloudflare's own data, instead of a country guessed from the browser's timezone.
- `crispstats.id()` through the same route, so [revenue attribution](/features/revenue-attribution) works behind the proxy.
- The script served from your zone's cache for most requests, with browsers revalidating it cheaply with a `304`.
- Content Security Policy that only needs `'self'` for the script and events.

## Notes for Cloudflare

- Never build the forwarded headers with `new Headers(request.headers)`. That sends your visitors' cookies and authorization to a third party; the setup check reports it as `cookiesForwarded: true`.
- Every script load, event and id request through the path is a Worker request. [Cloudflare's limits page](https://developers.cloudflare.com/workers/platform/limits/) gives the Workers Free plan 100,000 requests a day, reset at midnight UTC. A site with 40,000 pageviews a day sends at least 40,000 event requests, plus script loads from browsers without a cached copy, so check your usage before you rely on the free allowance.
- Past that daily limit, depending on the route's fail mode, Cloudflare either skips the Worker or shows an error page. Either way the path stops reaching the collector until the limit resets.
- For a Next.js app on Cloudflare (OpenNext or vinext), use the Next.js route handler instead of a separate Worker: see the [Next.js guide](/integrations/nextjs).
- A blocker that lists your own path can still stop it. A path unique to your site makes that unlikely, not impossible.

Full reference: [Cloudflare in the docs](https://docs.crispstats.com/tracking/proxy/cloudflare)

## Frequently asked questions

### Does the Worker run on every request to my site?

No. It runs only for requests that match the route under your proxy path. Pages, images and everything else are served as before.

### Are visitors' IP addresses stored when I proxy?

No. The Worker passes the address in x-forwarded-for so the collector can compute the daily visitor hash and apply your excluded IP addresses, then it is discarded. CrispStats never stores IP addresses.

### Does a proxy make analytics count every visitor?

It counts visitors whose blocker matches analytics hosts. Visitors who block JavaScript entirely, or whose blocker lists your path, are still not counted.

### Is the proxy available on every plan?

Yes. Every feature is on every CrispStats plan and in the 14-day free trial. Cloudflare's own Worker limits and pricing apply on your Cloudflare account.

## Related

- [Ad blockers and analytics](/glossary/ad-blocker)
- [First-party data explained](/glossary/first-party-data)
- [Track AI crawlers and AI traffic](/features/ai-traffic)
- [Web analytics for developers](/for/developers)
- [Proxy on Vercel](/integrations/vercel)
- [Proxy on Netlify](/integrations/netlify)
