# CrispStats on Netlify: install and proxy

Updated 2026-11-23

To use CrispStats on Netlify, add the script tag to the head of your pages, either in your code or with Netlify's snippet injection setting, and deploy. To count visitors whose ad blocker stops analytics hosts, add one edge function that forwards a path such as /k3x9qe/* to the collector with only safe headers, the visitor's address and their location.

## Steps

### 1. Add the script tag to your site

Copy the tag from **Settings**, **Tracking code** in the dashboard and put it inside `<head>` on every page, in your layout or HTML files. This is the most reliable route, because it covers every page however Netlify serves it.

```html
<script
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  src="https://in.crispstats.com/d.js"
></script>
```

### 2. Or paste it with snippet injection

Without touching code, open **Project configuration**, **Developer settings**, **Post processing**, find **Snippet Injection**, select **Add Snippet**, paste the tag, choose to insert it before `</head>` and select **Save**. Netlify injects it at once, with no redeploy. [Netlify's help page](https://docs.netlify.com/build/post-processing/snippet-injection/) states no plan restriction for it. It says snippets are injected when pages are served, and not on server-rendered pages, routes handled by functions or proxy routes, so those pages need the tag in code.

### 3. Add the proxy edge function

Under **Settings**, **Tracking code**, **Proxy through your own domain**, the dashboard proposes a path for your site. Save this function with that path. The `config` export at the end sets the path it runs on. Use it rather than a `_redirects` proxy rule, which copies every request header, including your visitors' login cookies.

`netlify/edge-functions/crispstats.ts`

```ts
// Forwards /k3x9qe/* to the collector. 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/*" };
```

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

Replace the tag, in code or in the snippet. `data-api` is required: without it the script sends events to `/e` on your own domain, and those requests fail.

```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**, 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.

## Check that it works

Deploy, then 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" }`. In the network panel, `/k3x9qe/d.js` loads with `200` or `304` and the `POST` to `/k3x9qe/e` answers `202`. Your visit appears on **Realtime** within seconds.

## What you get

- Pageviews, visitors, sources and channels for your production domain, with no cookies set on the visitor's device.
- Visitors whose [ad blocker](/glossary/ad-blocker) stops analytics hosts, counted again once the edge function is live.
- Country, region and city from Netlify's `context.geo`, instead of a country guessed from the browser's timezone.
- Revenue attribution through the proxy: `crispstats.id()` asks `/k3x9qe/id`, which the same function forwards.
- A core script of about 2.2 KB gzipped, cached by browsers for a day and revalidated with a `304`.

## Notes for Netlify

- Deploy previews on `*.netlify.app` send nothing, because `data-domains` lists only your real domain. Add a preview hostname only if you want those visits counted.
- A `_redirects` rule with status 200 is a plain proxy: the setup check would report `"cookiesForwarded": true`. The edge function forwards only the headers in `FORWARD`.
- The function passes `context.ip` as `x-forwarded-for`. The collector uses it for the daily visitor hash and your excluded IP addresses, then discards it.
- A blocker that lists your own path can still stop it. A path unique to your site makes that unlikely.

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

## Frequently asked questions

### How do I add analytics to a Netlify site without editing code?

Use snippet injection under Project configuration, Developer settings, Post processing, and insert the tag before the closing head tag. Netlify says it does not reach server-rendered pages or routes handled by functions.

### Why not proxy with a _redirects rule?

A proxy rule copies every request header to the collector, including your visitors' login cookies. The edge function picks the headers itself and adds the visitor's address and location.

### Does the edge function cache the script?

It passes cache-control and etag back and forwards if-none-match, so browsers keep the script for a day and then revalidate it with a 304.

### Is the proxy available on every plan?

Yes. Every CrispStats feature is on every plan and in the 14-day free trial. Netlify's own limits and pricing for edge functions apply to your site.

## Related

- [Ad blockers and analytics](/glossary/ad-blocker)
- [Cookieless analytics explained](/blog/cookieless-analytics-explained)
- [Plain HTML install](/integrations/html)
- [Revenue attribution](/features/revenue-attribution)
- [Proxy on Vercel](/integrations/vercel)
- [Proxy with a Cloudflare Worker](/integrations/cloudflare)
