> ## 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 on Netlify with an edge function

> Serve the CrispStats script and events from your own domain on Netlify with one edge function that forwards only safe headers, the visitor address and location.

To proxy CrispStats on Netlify, add one edge function at `netlify/edge-functions/crispstats.ts` that forwards `/k3x9qe/*` to `https://in.crispstats.com/p/*`, then point the script tag at the path with `data-api`. Ad blockers that stop known analytics hosts then see a request to your own domain, so those visitors are counted.

Use an edge function rather than a `_redirects` proxy rule: a plain proxy rule passes your visitors' cookies along, while this function picks the headers itself. 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. Add the edge function**

```ts netlify/edge-functions/crispstats.ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
// Forwards /k3x9qe/* to in.crispstats.com. Sends only the headers below:
// never your visitors' cookies or authorization.
import type { Config, Context } from "@netlify/edge-functions";

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 (request: Request, context: Context) => {
  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", context.ip);
  if (context.geo.country?.code) headers.set("x-crispstats-country", context.geo.country.code);
  if (context.geo.subdivision?.code) headers.set("x-crispstats-region", context.geo.subdivision.code);
  if (context.geo.city) headers.set("x-crispstats-city", encodeURIComponent(context.geo.city));
  const secret = Netlify.env.get("CRISPSTATS_PROXY_SECRET");
  if (secret) headers.set("x-crispstats-proxy-secret", secret);

  const upstream = await fetch(UPSTREAM + url.pathname.slice(PREFIX.length) + url.search, {
    method: request.method,
    headers,
    body: request.method === "POST" ? await request.arrayBuffer() : 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 });
};

export const config: Config = { path: "/k3x9qe/*" };
```

The `config` export at the end sets the path the function runs on.

**2. 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.

**3. Deploy and run the setup check**

Deploy, then 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

Netlify gives the function the visitor's address as `context.ip` and their location as `context.geo`. The function passes the address as `x-forwarded-for`, and the country code, region code and city as `x-crispstats-country`, `x-crispstats-region` and `x-crispstats-city`. The collector uses the address for the daily visitor hash and your excluded IP addresses, then discards it.

## 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**. Add it as `CRISPSTATS_PROXY_SECRET` in the site's environment variables and redeploy. The function reads it with `Netlify.env.get` and 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 a \_redirects rule with status 200?
>
> A proxy rule copies every request header to the collector, including your visitors' login cookies. The setup check would report `"cookiesForwarded": true`. The edge function forwards only the headers in `FORWARD`.

> [accordion] Does the function cache the script?
>
> It passes `cache-control` and `etag` back and forwards `if-none-match`, so browsers cache the script for a day and revalidate it with a `304`. See [script caching](https://docs.crispstats.com/tracking/proxy.md#script-caching).

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

## Next steps

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

> [card] [Content Security Policy](https://docs.crispstats.com/help/content-security-policy.md)
>
> What to allow once the script is first-party.

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