> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Proxy CrispStats with a Cloudflare Worker

> Serve the CrispStats script and events from your own domain with a Cloudflare Worker on one route. It passes the visitor's address and location and caches the script.

To proxy CrispStats with Cloudflare, deploy a Worker and add the route `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](https://docs.crispstats.com/tracking/proxy/nextjs.md) 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](https://docs.crispstats.com/tracking/proxy.md) for how the path, the check and signing work on every host.

## Set up the proxy

**1. Deploy the Worker**

```js worker.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
// 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 });
  },
};
```

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

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-api="/k3x9qe"
  src="/k3x9qe/d.js"
></script>
```

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

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
https://example.com/k3x9qe/check?k=cs_pk_xxxxxxxx
```

Look for `"visitorAddressForwarded": true`, `"cookiesForwarded": false` and `"location": { "source": "proxy" }`. The [proxy overview](https://docs.crispstats.com/tracking/proxy.md#check-it-works) explains every field.

## Visitor address and location

The Worker takes the visitor's address from `cf-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](https://docs.crispstats.com/tracking/proxy.md#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:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
npx wrangler secret put CRISPSTATS_PROXY_SECRET
```

The Worker sends it when it is set. Then look for `"signed": true` in the setup check. See [sign proxied requests](https://docs.crispstats.com/tracking/proxy.md#sign-proxied-requests).

## FAQ

> [accordion] 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`.

> [accordion] Does the Worker run on every request to my site?
>
> No. It runs only for requests that match the route under your path.

> [accordion] Does crispstats.id() work behind the proxy?
>
> Yes. It asks `/k3x9qe/id`, which the same Worker forwards.

## Next steps

> [card] [Proxy overview](https://docs.crispstats.com/tracking/proxy.md)
>
> The path, the check, caching, limits and signing.

> [card] [Rate limits](https://docs.crispstats.com/api-reference/rate-limits.md)
>
> Collector limits behind a proxy.

> [card] [Ad blockers](https://docs.crispstats.com/help/ad-blockers.md)
>
> What a proxy recovers and what it does not.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.