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

# Install CrispStats with Google Tag Manager

> Load the CrispStats script through a Google Tag Manager Custom HTML tag that fires on All Pages, using the snippet generated in your site settings.

To install CrispStats with Google Tag Manager, create a Custom HTML tag that fires on All Pages and paste the snippet from your site settings. The snippet creates the CrispStats script element, sets its attributes and appends it to `<head>`, so the script behaves exactly as if you had pasted the tag into your HTML.

## Add the tag

**1. Copy the GTM snippet**

In the dashboard, open your site's **Settings**, select **Tracking code** and choose the **Google Tag Manager** tab. It has your public key filled in:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!-- Tag type: Custom HTML. Trigger: All Pages. -->
<script>
  (function () {
    var s = document.createElement("script");
    s.defer = true;
    s.setAttribute("data-site", "cs_pk_xxxxxxxx");
    s.setAttribute("data-domains", "example.com");
    s.src = "https://in.crispstats.com/d.js";
    document.head.appendChild(s);
  })();
</script>
```

**2. Create a Custom HTML tag**

In your GTM container, create a new tag, choose **Custom HTML** as the tag type and paste the snippet into the HTML field.

**3. Fire it on All Pages**

Set the trigger to **All Pages** so the script loads on every page view. You only need one trigger: the script tracks single-page navigation by itself from the History API.

**4. Publish the container**

Submit and publish the container version, then open your live site. The status row on the **Tracking code** tab shows **Receiving events.** once the first event has been stored. Visits from `localhost` and `file://` are ignored, so GTM preview mode on a local site does not count.

## Add optional modules

The switches under **Extra events** on the **Tracking code** tab add one `setAttribute` line per module to the GTM snippet. Each attribute is set with an empty value, which turns the module on. With outbound links, downloads, web vitals and errors on, the snippet looks like this:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script>
  (function () {
    var s = document.createElement("script");
    s.defer = true;
    s.setAttribute("data-site", "cs_pk_xxxxxxxx");
    s.setAttribute("data-domains", "example.com");
    s.setAttribute("data-outbound", "");
    s.setAttribute("data-downloads", "");
    s.setAttribute("data-vitals", "");
    s.setAttribute("data-errors", "");
    s.src = "https://in.crispstats.com/d.js";
    document.head.appendChild(s);
  })();
</script>
```

The same pattern works for any [script attribute](https://docs.crispstats.com/tracking/script-attributes.md) that takes a value, for example `s.setAttribute("data-exclude", "/admin/*");`.

## Why the attributes still work

The script reads its options from `document.currentScript`, the `<script>` element that is running. A script element created with `document.createElement` and appended to the page is a classic script, so while it runs, `document.currentScript` points to that element and the script reads the `data-` attributes you set on it.

Two details in the snippet matter:

* **Set the attributes before `appendChild`.** The script reads them once when it starts.
* **Use `setAttribute("data-…", …)`**, not a property such as `s.site`. Only `data-` attributes are read.

The script also sends events to the origin it was loaded from, `https://in.crispstats.com`, unless you set `data-api`.

> [!NOTE]
>
> If the CrispStats tag is also in your page HTML, the copy that runs second does nothing, so the visit is not counted twice.

## Check that it works

* Open your live site with the browser's developer tools on the **Network** tab. You should see `d.js` load from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` that gets `202`.
* In the **Elements** panel, find the `<script>` element for `d.js` in `<head>` and check that it carries `data-site` and your other attributes. If they are missing, the tag sets them after `appendChild`, or with properties instead of `setAttribute`.
* Open `https://in.crispstats.com/check?k=cs_pk_xxxxxxxx` with your key. `"site": "known"` means the key is right. The visit appears in **Realtime** within seconds.

Previewing your container on the live site loads the real script, so your test visits count unless you [exclude your browser](https://docs.crispstats.com/tracking/exclude-traffic.md#exclude-your-own-browser).

## How page changes are counted

The script counts the first load and then every URL change made through the History API, so single-page navigation is tracked without any extra trigger. Do not fire the CrispStats tag on a history change trigger as well: it would add a second script element, and while that copy does nothing, it is a wasted request. The same URL twice in a row is one pageview.

## Send custom events

Once the script has loaded, any tag can call the global. For a click you already have a GTM trigger for, add a second Custom HTML tag that fires on that trigger:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!-- Tag type: Custom HTML. Trigger: your click trigger. -->
<script>
  if (window.crispstats) window.crispstats("signup click", { place: "header" });
</script>
```

For clicks you can also put `data-cs-event` attributes in your page's HTML, with no GTM tag at all. See [declarative events](https://docs.crispstats.com/track/declarative-events.md). A call made before the script has loaded is lost unless the [queue stub](https://docs.crispstats.com/track/custom-events.md#call-crispstats-before-the-script-loads) runs first.

## FAQ

> [accordion] Does a consent setting in GTM block CrispStats?
>
> GTM fires the tag according to the triggers and consent settings you give it. If the CrispStats tag waits for consent, visitors who do not consent are not counted. The script sets no cookie by default; see [compliance](https://docs.crispstats.com/privacy/compliance.md) when you decide. This is not legal advice.

> [accordion] Can I use the GTM tag and a script tag in the HTML?
>
> Use one. If both run, the copy that runs second does nothing, so the visit is still counted once.

> [accordion] Can I load the script from my own domain through GTM?
>
> Yes. Set up a [proxy](https://docs.crispstats.com/tracking/proxy.md), then set `s.src` to your path's `d.js` and add `s.setAttribute("data-api", "/k3x9qe");` with your path.

## Next steps

> [card] [Script attributes](https://docs.crispstats.com/tracking/script-attributes.md)
>
> Every option you can set with `setAttribute`.

> [card] [Track custom events](https://docs.crispstats.com/track/custom-events.md)
>
> Send actions with `crispstats()` once the script is loaded.

> [card] [Troubleshooting](https://docs.crispstats.com/help/troubleshooting.md)
>
> What to check when no events arrive.


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