Skip to content

Hosts

Proxy CrispStats through a Cloudflare Worker

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.

Updated

Set up CrispStats on Cloudflare

  1. 01

    Step 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. 02

    Step 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.

    Replace the highlighted values with your own.

    worker.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. 03

    Step 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 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
    [[routes]]pattern = "example.com/k3x9qe/*"zone_name = "example.com"
  4. 04

    Step 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.

    <scriptdeferdata-site="cs_pk_your_site_key"data-domains="example.com"data-api="/k3x9qe"src="/k3x9qe/d.js"></script>
  5. 05

    Step 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 the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • What you get

    • Visitors whose 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 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 and limits

    • 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 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.
    • A blocker that lists your own path can still stop it. A path unique to your site makes that unlikely, not impossible.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

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

  • 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.

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

  • 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.

Not answered here? Write to hello@crispstats.com

Go further

Related

  • Ad blockers and analytics

    An ad blocker stops ads and trackers from loading in the browser.

  • First-party data explained

    First-party data is data you collect directly from your own visitors and customers.

  • Track AI crawlers and AI traffic

    Visitors sent by ChatGPT, Perplexity and Claude, plus AI crawler hits.

  • Web analytics for developers

    A small cookieless script with web vitals and error tracking, an npm SDK, a REST API and an MCP server, so your analytics fits the way you work.

  • Proxy on Vercel

    Add CrispStats to a site hosted on Vercel, then serve it from your own domain with one route in vercel.json that strips cookies before forwarding.

  • Proxy on Netlify

    Add CrispStats to a Netlify site in your code or with snippet injection, then serve it from your own domain with one edge function.

Guides

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.