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

# Script attributes reference for d.js

> Every data- attribute the CrispStats script reads, with values and defaults, what the script sends and cuts from URLs, and the crispstats global with its queue stub.

The CrispStats script reads all of its options from `data-` attributes on its own `<script>` tag. Only `data-site` is required: with it you get pageviews, single-page navigation and engagement. The dashboard also adds `data-domains` with your site's domain, so copies of your site elsewhere are not counted. Every other attribute is off until you add it.

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

Use a classic script tag with `defer` or `async`. `type="module"` does not work, because the script finds its options through `document.currentScript`, which is not set for modules. Do not give the tag `id="crispstats"`: browsers expose an element's id as a global, so `window.crispstats` would be the element until the script loads.

## Write flag attributes

A flag attribute is on as soon as it is present, whatever its value. Write it the way your platform expects:

| Where | Flag on | Why |
| - | - | - |
| HTML, Liquid, PHP templates | `data-outbound` | A bare attribute. |
| JSX (Next.js, React, Remix) | `data-outbound=""` | A bare JSX attribute renders as `data-outbound="true"`, and for attributes that take a list, such as `data-downloads`, `"true"` is read as the list. |
| Config objects (Nuxt `app.head`) | `"data-outbound": ""` | Same as JSX. |
| `setAttribute` (Google Tag Manager) | `s.setAttribute("data-outbound", "")` | Same as JSX. |

**Settings**, **Tracking code** writes every option this way for each platform tab. An option starts working once the updated snippet is on your site.

## Where it runs

**data-site** `string` *required*

The site's public key, which starts with `cs_pk_`. It tells the collector which site an event belongs to and is safe to publish. Without it the script tracks nothing.

**data-domains** `string` (default: any hostname)

Only track on these hostnames and their subdomains, separated by commas: `data-domains="example.com,shop.example.org"`. Preview deployments, `workers.dev` copies, staging hosts and scraped copies of your pages then send nothing. The dashboard fills it in with the site's **Domain** and **Additional hostnames**. A leading `www.` is not special: `example.com` covers `www.example.com` because it is a subdomain.

**data-api** `string` (default: the origin the script was loaded from)

Base URL of the collector, without a trailing slash. Events go to `<api>/e` and `crispstats.id()` asks `<api>/id`. With the standard tag this is `https://in.crispstats.com`. A path works too, for a first-party proxy: `data-api="/k3x9qe"` sends events to `/k3x9qe/e` on your own domain. See [proxy](https://docs.crispstats.com/tracking/proxy.md).

**data-exclude** `string` (default: none)

Paths not to track, separated by commas or spaces: `data-exclude="/admin, /admin/*, /preview"`. See [exclude paths](#exclude-paths) for the exact matching rule. Nothing is sent from an excluded page: no pageview, no engagement and no custom events. Its URL is never used as a referrer.

**data-allow-localhost** `flag` (default: off)

Send events from `localhost`, `127.x.x.x`, `[::1]`, `*.localhost` and `file://` pages, which are ignored otherwise. The collector still drops them unless `localhost` is one of the site's hostnames. Remove it before you ship. See [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md).

**data-debug** `flag` (default: off)

Log every event in the browser console and send nothing. It also logs on localhost, in automated browsers and on hostnames outside `data-domains`, and each line says why the event would not have been sent there. Use it to test locally and in CI; never ship it, because a page with `data-debug` sends nothing. See [debug mode](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

**data-dnt** `flag` (default: off)

Do not track browsers that send the Do Not Track signal. The script collects no personal data either way, so this is off by default.

## What is sent

**data-keep-params** `string` (default: none)

Query parameters to keep on the page URL and the referrer, on top of the defaults, separated by commas: `data-keep-params="page,lang"`. `data-keep-params="*"` keeps the whole query string, as the script did before it cut query strings. Names match without regard to case. See [what the script sends](#what-the-script-sends).

**data-hash** `flag` (default: off)

Treat the URL fragment as part of the page, for hash routers whose URLs look like `/#/settings`. Only a fragment that starts with `#/` or `#!/` is sent; in-page `#anchors` and tokens such as `#access_token=` never are. Sites with real paths, such as Next.js, Nuxt, SvelteKit, Astro and Remix, do not need it. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md#hash-routers).

**data-manual** `flag` (default: off)

No automatic pageviews. The script counts a pageview only when your code calls `crispstats.pageview()`, including on the first page load. Use it only for a router the script cannot see. Custom events sent before the first `pageview()` call are dropped. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md#manual-pageviews).

**data-persist** `flag` (default: off)

Keep a random visitor id in `localStorage.crispstats_vid`, so returning visitors are recognised across days. Do not add it by hand: turn on **Persistent visitors** in **Settings**, **General**, and the snippet in **Settings**, **Tracking code** includes it. See [persistent visitors](#persistent-visitors).

## Optional modules

Each module is off until its attribute is on the tag. They are in `d.js` and left out of the smaller `d.core.js`, which ignores these attributes. See [script builds](https://docs.crispstats.com/tracking/script-builds.md).

**data-outbound** `flag` (default: off)

Sends an `Outbound link` event with a `url` prop for clicks and middle clicks on `http` and `https` links to another host. See [outbound links and downloads](https://docs.crispstats.com/track/outbound-links-and-downloads.md).

**data-downloads** `flag or string` (default: off)

Sends a `File download` event with a `url` prop for links with a `download` attribute or a known file extension: pdf, xlsx, docx, txt, rtf, csv, exe, key, pps, ppt, pptx, 7z, pkg, rar, gz, zip, avi, mov, mp4, mpeg, wmv, midi, mp3, wav, wma and dmg. Give a list to replace the defaults: `data-downloads="pdf, epub"`.

**data-vitals** `flag` (default: off)

Sends one `vitals` event the first time the page is hidden, with LCP, INP, CLS, FCP and TTFB as far as the browser supports them. See [web vitals](https://docs.crispstats.com/health/web-vitals.md).

**data-errors** `flag` (default: off)

Reports uncaught errors and unhandled promise rejections, each distinct error once and at most ten per page load. Cross-origin `Script error.` messages and failed resource loads are skipped. See [JavaScript errors](https://docs.crispstats.com/health/errors.md).

**data-404** `flag or string` (default: off)

Sends one `404` event with a `path` prop per pageview of a page that was not found: a page with `<meta name="crispstats:404">`, an element that matches the selector you give (`data-404=".not-found"`), or, in Chromium browsers, a full page load that the server answered with `404`. See [404 pages](https://docs.crispstats.com/track/404-pages.md).

The `Outbound link` and `File download` events carry the link's URL in `url`, and a JavaScript error carries its script's URL in `source`. These are cut the same way as the page URL, and lose their fragment.

## What the script sends

Every event carries the page URL and the referrer. Before either leaves the browser, the script cuts them:

* **The fragment** (`#…`) is removed. Fragments often carry tokens such as `#access_token=`. With `data-hash` on, a fragment that is a route (`#/…` or `#!/…`) is kept; any other fragment, such as `#pricing` or `#access_token=`, is still removed.
* **The query string** is cut to the parameters analytics needs. Query strings carry license keys, password reset tokens, checkout session ids and e-mail addresses, so everything else is removed.
* **The length** is capped at 2,048 characters.

The parameters that stay:

| Kept | With its value |
| - | - |
| `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `ref`, `source`, `via` | Yes. The collector reads them for the source and campaign. |
| Ad click ids: `gclid`, `gbraid`, `wbraid`, `msclkid`, `fbclid`, `ttclid`, `twclid`, `li_fat_id` | No: the value becomes `1`. The collector only needs to see the name to count the visit as paid, and the value identifies the click. |
| The names in `data-keep-params` | Yes. |

So `/activate?key=LIC-123&utm_source=newsletter&gclid=Cj0KCQ` is sent as `/activate?utm_source=newsletter&gclid=1`. The order of the parameters is kept.

The collector then stores even less: the path, the query parameters listed under **Query parameters** in **Settings**, **General**, UTM values in their own fields, and only the host of the referrer. See [data collected](https://docs.crispstats.com/privacy/data-collected.md#paths-and-query-strings). When you keep a parameter in **Settings**, **General**, the snippet in **Settings**, **Tracking code** adds it to `data-keep-params` for you, so the browser does not cut what you asked the collector to keep.

## Exclude paths

`data-exclude` takes a list of patterns, each matched against the whole path of the page (without the query string):

* `*` matches any characters, slashes included.
* A trailing slash on the page's path is optional.
* Everything else matches literally.

| Pattern | Matches | Does not match |
| - | - | - |
| `/admin` | `/admin`, `/admin/` | `/admin/users` |
| `/admin/*` | `/admin/users`, `/admin/users/42` | `/admin` |
| `/admin*` | `/admin`, `/admin/users`, `/administrator`, `/admin-tools` | `/blog/admin` |
| `/blog/*/edit` | `/blog/hello/edit`, `/blog/2026/hello/edit` | `/blog/hello` |

For an admin section and nothing else, use `/admin, /admin/*`. `/admin*` also matches every path that starts with `/admin`, such as `/administrator`.

**Excluded paths** in **Settings**, **General** works on the collector instead, with a different rule: there `*` stays within one path segment and `**` crosses segments. See [exclude traffic](https://docs.crispstats.com/tracking/exclude-traffic.md#with-excluded-paths-in-settings).

## Persistent visitors

Without persistent visitors, CrispStats recognises a visitor for one day, from a hash, and stores nothing in the browser. With it, the script keeps a random id in `localStorage.crispstats_vid` and the collector uses that id, so a visitor is recognised across days.

It takes one switch in the dashboard, and the snippet follows it:

1. Turn on **Persistent visitors** in **Settings**, **General**.
2. Copy the snippet again from **Settings**, **Tracking code**. It now includes `data-persist`. Deploy it.

Both halves are needed, and the dashboard keeps them together: the collector ignores the id while the setting is off, and the script creates no id without `data-persist`. If you write the tag by hand, add `data-persist` only while the setting is on. It stores an identifier on the visitor's device, which may need consent: read [persistent visitors](https://docs.crispstats.com/privacy/persistent-visitors.md) before you turn it on.

## The crispstats global

The script installs `window.crispstats`. It exists even when the script decides not to track (on localhost, for an opted-out browser, outside `data-domains`), so calling it is always harmless.

| Call | What it does |
| - | - |
| `crispstats(name, props?)` | Sends a custom event. Props are flat: numbers, booleans and `null` are sent as they are, strings are cut at 500 characters, and arrays or objects are sent as their string form. |
| `crispstats.identify(userId, traits?)` | Sends an `identify` event with the traits as props, and attaches your user id to later events from the same page. The id is kept in memory until the page unloads. Nothing is stored in the browser. See [identify users](https://docs.crispstats.com/track/identify-users.md). |
| `crispstats.revenue(amount, currency, props?)` | Sends a `revenue` event. `amount` is in minor units (cents) and is rounded to an integer. `currency` is an ISO 4217 code. |
| `crispstats.pageview(url?)` | Counts a pageview. Without a URL it counts the current one: always under `data-manual`, otherwise only if it differs from the last one counted, so it never doubles an automatic view. With a URL, such as `crispstats.pageview("/checkout/step-2")`, it always counts that URL, resolved against the current page and cut like any other. |
| `crispstats.id()` | Returns a Promise of the visitor's current anonymous id, or `undefined` when the script is not tracking or in debug mode. Behind a proxy it asks your proxy first, then the collector directly. Pass it to your payment provider for [revenue attribution](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md). Only the loaded script has it, not the queue stub. |
| `crispstats.v` | The script version, as a string. `undefined` on the queue stub. |

```js theme={"theme":{"light":"github-light","dark":"github-dark"}}
crispstats("signup", { plan: "pro" });
crispstats.identify("user_42", { plan: "team" });
crispstats.revenue(4900, "USD");
crispstats.pageview();

const visitorId = await crispstats.id();
```

For types in TypeScript, see [TypeScript](https://docs.crispstats.com/install/typescript.md).

### Declarative events

Any element with a `data-cs-event` attribute sends that event when it, or anything inside it, is clicked. Each `data-cs-prop-*` attribute becomes a prop. This works in both builds. See [declarative events](https://docs.crispstats.com/track/declarative-events.md).

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<button data-cs-event="add to cart" data-cs-prop-sku="tee-01">Add to cart</button>
```

## Call the global before the script loads

The script is deferred, so inline code can run before it exists. Put this stub above the script tag. Calls made before the script loads are queued, then replayed in order after the script has started:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!-- Above the script tag. Calls made before it loads wait here and run in order. -->
<script>
  if (typeof window.crispstats != "function") {
    var queue = [];
    var stub = function () { queue.push([].slice.call(arguments)); };
    stub.q = queue;
    ["identify", "revenue", "pageview"].forEach(function (method) {
      stub[method] = function () { queue.push(Object.assign([].slice.call(arguments), { m: method })); };
    });
    window.crispstats = stub;
  }
</script>
```

The stub checks that `window.crispstats` is a function, not only that it exists, because an element with `id="crispstats"` would make it exist. It queues `crispstats()`, `identify`, `revenue` and `pageview`. It has no `id()` and no `v`: call `crispstats.id()` from code that runs after the page has loaded, such as a click handler. For a TypeScript module, use the stub on the [TypeScript](https://docs.crispstats.com/install/typescript.md) page.

The script replaces the stub with the real function. If a copy of the script already ran on the page, a second copy sees `crispstats.v` and does nothing.

## When the script sends nothing

The script loads, installs `window.crispstats`, and sends nothing at all when the first of these applies. With `data-debug` on, each console line names the one that applies:

| Reason in the console | When |
| - | - |
| `no data-site` | The tag has no `data-site`. |
| `automated browser` | `navigator.webdriver` is set (Playwright, Puppeteer, Selenium), or the page runs in Cypress. |
| `localhost` | The page is local (`localhost`, `*.localhost`, `127.x.x.x`, `[::1]`, `file://`) and the tag has no `data-allow-localhost`. |
| `domain not allowed` | `data-domains` is set and the hostname is not one of them or their subdomains. |
| `do not track` | `data-dnt` is set and the browser sends Do Not Track. |
| `opted out` | `localStorage.crispstats_ignore` is `1` in this browser. See [exclude your own browser](https://docs.crispstats.com/tracking/exclude-traffic.md#exclude-your-own-browser). |
| `excluded path` | The current path matches `data-exclude`. |
| `no pageview yet` | Under `data-manual`, an event was sent before the first `crispstats.pageview()`. |
| `debug mode` | None of the above: on a normal page, this event would have been sent. |

While a page is being prerendered, the script waits and counts the pageview when the page is shown.

## Next steps

> [card] [Single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md)
>
> How route changes are seen, and pageview() for routers that hide them.

> [card] [Localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md)
>
> Debug mode, local builds and tests in CI.

> [card] [Script builds](https://docs.crispstats.com/tracking/script-builds.md)
>
> Which file is which, and how it is cached.


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