# Gatsby analytics with CrispStats

Updated 2026-11-23

Gatsby needs no analytics plugin: render the CrispStats script tag into the head with the onRenderBody API in gatsby-ssr.js, then rebuild and deploy. Every page Gatsby builds carries the tag, and page changes made by Gatsby's router are counted because the script watches the History API.

## Steps

### 1. Find your public key

In the dashboard, open your site's **Settings** and select **Tracking code**. Your public key starts with `cs_pk_` and is safe to publish.

### 2. Add the tag with onRenderBody

Create `gatsby-ssr.js` at the root of your project, or add to the one you have. [`onRenderBody` runs as each page is rendered](https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/), and `setHeadComponents` adds the element to that page's head. Replace the key and domain with yours. If the file already calls `setHeadComponents`, add the element to that array instead of calling it twice.

`gatsby-ssr.js`

```jsx
import * as React from "react";

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

### 3. Turn on more tracking (optional)

In JSX an attribute needs a value, so give each option an empty string. A bare 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.

`gatsby-ssr.js`

```jsx
<script
  key="crispstats"
  defer
  data-site="cs_pk_your_site_key"
  data-domains="example.com"
  data-outbound=""
  data-vitals=""
  data-errors=""
  src="https://in.crispstats.com/d.js"
/>
```

### 4. Send a custom event (optional)

Call the global from a component. The optional call (`?.`) keeps the button working when an ad blocker stops the script. In a TypeScript project, add `crispstats.d.ts` from the [TypeScript page](/docs/install/typescript). For clicks you can also use `data-cs-event` attributes and no JavaScript.

`src/components/upgrade-button.js`

```jsx
export function UpgradeButton() {
  return (
    <button onClick={() => window.crispstats?.("upgrade clicked", { from: "pricing" })}>
      Upgrade
    </button>
  );
}
```

### 5. Rebuild, deploy and open the live 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 and a `POST` to `https://in.crispstats.com/e` on the first load and on each page change, and the collector answers `202`. In the dashboard, **Settings**, **Tracking code** shows **Receiving events.** and the visit appears on **Realtime** within seconds. To check the key alone, open `https://in.crispstats.com/check?k=cs_pk_your_site_key` with your own key: `"site": "known"` means it is right. To see events under `gatsby develop`, add `data-debug=""` and `localhost` in `data-domains`, then remove both before you deploy. See [localhost and testing](/docs/tracking/localhost-and-testing).

## What you get

- A pageview for the first load and every page change Gatsby's links make with `history.pushState`, with the previous page as referrer.
- Sources and channels, including search, newsletters tagged with [UTM parameters](/glossary/utm-parameters) and AI assistants such as ChatGPT and Perplexity.
- A core script of about 2.2 KB gzipped, loaded with `defer`, so it does not slow the first paint of a prebuilt page.
- Time on page and scroll depth per page, which tell you whether a post is read or skimmed.
- Optional web vitals, JavaScript errors, outbound links, downloads and 404 pages, each switched on with one attribute.

## Notes for Gatsby

- There is no Gatsby plugin and no npm package from CrispStats. The element in `gatsby-ssr.js` is the whole install.
- Gatsby prefetches linked pages, but prefetching does not change the URL, so it is not counted as a pageview.
- The same URL twice in a row is one pageview, so a redirect to the current page adds nothing.
- Give the element a `key`, as above. React expects one on each element of the array passed to `setHeadComponents`.
- Leave out private paths with `data-exclude`, and your own visits with the ignore flag in your browser. See [exclude traffic](/docs/tracking/exclude-traffic).
- To serve the script from your own domain, add a forwarding rule on your host, for example on Netlify. See [proxy by host](/docs/tracking/proxy).

Full reference: [Gatsby in the docs](https://docs.crispstats.com/install/gatsby)

## Frequently asked questions

### Do I need a Gatsby plugin for analytics?

No. The script element in gatsby-ssr.js is the whole install. CrispStats has no Gatsby plugin or npm package, so there is nothing to keep updated.

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

### Why do I see no visits from gatsby develop?

The script ignores localhost on purpose, so development runs stay out of your numbers. Deploy and test on the live URL, or use debug mode to log each event in the console without sending it.

### Does Gatsby's link prefetching inflate pageviews?

No. Gatsby fetches linked pages in advance without changing the URL, and only a change of URL is a pageview. A page counts when the reader actually opens it.

### What does it cost for a Gatsby blog?

A blog mostly sends pageviews, one event each, and prefetching adds none. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Privacy-friendly analytics for bloggers](/for/bloggers)
- [Bounce rate vs engagement](/blog/bounce-rate-vs-engagement)
- [Track visitors from ChatGPT and Perplexity](/features/ai-traffic)
- [React analytics](/integrations/react)
