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

# Add CrispStats to a Gatsby site

> Install CrispStats on a Gatsby site by adding the script tag with onRenderBody in gatsby-ssr.js. Page changes made by Gatsby's router are tracked for you.

To add CrispStats to a Gatsby site, render the script tag into `<head>` with the `onRenderBody` API in `gatsby-ssr.js`. Every page Gatsby builds then carries the tag, and page changes made by Gatsby's router are counted because the script watches the History API.

There is no Gatsby plugin and no npm package to install. You need your site's public key, which starts with `cs_pk_`. Find it in your site's settings under **Tracking code**. The key is safe to publish.

## Add the script

**1. Add the tag with onRenderBody**

Create `gatsby-ssr.js` at the root of your project, or add to the one you have:

```jsx gatsby-ssr.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
import * as React from "react";

export const onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <script
      key="crispstats"
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    />,
  ]);
};
```

Replace `cs_pk_xxxxxxxx` with your public key and `example.com` with your domain. If the file already calls `setHeadComponents`, add the `<script>` element to that array instead of calling it twice.

**2. Turn on more tracking (optional)**

In JSX an attribute needs a value. Give each option an empty string, which switches it on:

```jsx gatsby-ssr.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  key="crispstats"
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-outbound=""
  data-vitals=""
  data-errors=""
  src="https://in.crispstats.com/d.js"
/>
```

A bare JSX attribute such as `data-downloads` renders as `"true"`, which the script reads as a list of file extensions. Leave out `data-hash`: Gatsby routes with real paths. [Script attributes](https://docs.crispstats.com/tracking/script-attributes.md) lists every attribute.

**3. Build, deploy and open your site**

`gatsby-ssr.js` runs when pages are rendered, so restart `gatsby develop` or run a new build after you change it. Visits to `localhost` are ignored, so deploy and open your live site in a normal browser window.

## Check that it works

* In your browser's developer tools, the **Network** tab shows `d.js` loading from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` on the first load and on each page change. The collector answers `202`.
* In CrispStats, **Settings**, **Tracking code** shows **Receiving events.** with the time of the last event, and the visit appears in **Realtime** within seconds.
* Open `https://in.crispstats.com/check?k=cs_pk_xxxxxxxx` with your key. `"site": "known"` means the key is right.

To see events while you run `gatsby develop`, add `data-debug=""` to the tag and `localhost` to `data-domains`. Every event is logged in the console and nothing is sent. Remove both before you deploy. See [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

## How page changes are counted

Gatsby's links change pages in the browser with `history.pushState`. The script counts a pageview on the first load and on every `pushState`, `replaceState` and back or forward navigation. The same URL twice in a row is one pageview, and the referrer of a client-side page change is the previous page. Prefetching a linked page does not change the URL, so it is not counted. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md).

## Send custom events

Call the global from a component. The optional call (`?.`) keeps the button working when an ad blocker stops the script:

```jsx src/components/upgrade-button.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
export function UpgradeButton() {
  return (
    <button onClick={() => window.crispstats?.("upgrade clicked", { from: "pricing" })}>
      Upgrade
    </button>
  );
}
```

For clicks you can also use `data-cs-event` attributes and no JavaScript, see [declarative events](https://docs.crispstats.com/track/declarative-events.md). In a TypeScript project, add `crispstats.d.ts` from [TypeScript](https://docs.crispstats.com/install/typescript.md).

## FAQ

> [accordion] Do I need a Gatsby plugin?
>
> No. The tag in `gatsby-ssr.js` is the whole install. There is no CrispStats plugin or npm package.

> [accordion] Does the script run on pages Gatsby builds at build time?
>
> Yes. `onRenderBody` adds the tag to the HTML of every page Gatsby renders, and the script runs when a visitor opens one.

> [accordion] Can I serve the script from my own domain?
>
> Yes, with a forwarding rule on your host. See [proxy by host](https://docs.crispstats.com/tracking/proxy.md) for Netlify, Vercel, Cloudflare and nginx.

## Next steps

> [card] [Custom events](https://docs.crispstats.com/track/custom-events.md)
>
> Track signups, clicks and other actions with properties.

> [card] [Proxy on Netlify](https://docs.crispstats.com/tracking/proxy/netlify.md)
>
> Serve the script from your own domain.

> [card] [Exclude traffic](https://docs.crispstats.com/tracking/exclude-traffic.md)
>
> Keep your own visits and private paths out.

> [!NOTE]
>
> Gatsby details checked against Gatsby's `gatsby-ssr` reference ([https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/](https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/)) on 2026-11-23.


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