example.com/k3x9qe/* to it in the zone that serves your site. The Worker forwards that path to https://in.crispstats.com/p/, and the script tag points at the path with data-api. Ad blockers that stop known analytics hosts then see a request to your own domain.
This works for any site behind Cloudflare, whatever serves it. For a Next.js app on Cloudflare (OpenNext or vinext), the Next.js route handler runs as is. Use your own path instead of /k3x9qe: Settings, Tracking code, Proxy through your own domain proposes one for your site. See the proxy overview for how the path, the check and signing work on every host.
Set up the proxy
1
Deploy the Worker
worker.js
2
Add the route
In the zone that serves your site, add the route
example.com/k3x9qe/* (with your domain and path) to the Worker. Only requests under that path reach it; the rest of your site is untouched.3
Point the script tag at the path
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 sends events to /e on your own domain, and those requests fail.4
Run the setup check
Open the check through your proxy, in a browser, on your own site:Look for
"visitorAddressForwarded": true, "cookiesForwarded": false and "location": { "source": "proxy" }. The proxy overview explains every field.Visitor address and location
The Worker takes the visitor’s address fromcf-connecting-ip and sends it as x-forwarded-for. From request.cf it passes the country, region code and city as x-crispstats-country, x-crispstats-region and x-crispstats-city, and leaves out the codes XX and T1, which are not countries. The collector uses the address for the daily visitor hash and your excluded IP addresses, then discards it.
Caching
The Worker keeps the script in your zone’s cache for a day (cacheTtl: 86400), so most script requests never leave your zone. It also passes cache-control and etag back, so browsers cache the script and revalidate it with a 304. Events, /id and /check are never cached. See script caching.
Sign the requests (optional)
Signing proves a forwarded request came from your proxy. Reveal the secret (cs_px_…) in Settings, Tracking code, Proxy through your own domain, Advanced: sign proxied requests, store it on the Worker and redeploy:
"signed": true in the setup check. See sign proxied requests.
FAQ
Why not new Headers(request.headers)?
Why not new Headers(request.headers)?
Copying every header sends your visitors’ cookies and authorization to a third party. The Worker copies only the headers in
FORWARD.Does the Worker run on every request to my site?
Does the Worker run on every request to my site?
No. It runs only for requests that match the route under your path.
Does crispstats.id() work behind the proxy?
Does crispstats.id() work behind the proxy?
Yes. It asks
/k3x9qe/id, which the same Worker forwards.Next steps
Proxy overview
The path, the check, caching, limits and signing.
Rate limits
Collector limits behind a proxy.
Ad blockers
What a proxy recovers and what it does not.