Skip to main content
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:
gatsby-ssr.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:
gatsby-ssr.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 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.

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.

Send custom events

Call the global from a component. The optional call (?.) keeps the button working when an ad blocker stops the script:
src/components/upgrade-button.js
For clicks you can also use data-cs-event attributes and no JavaScript, see declarative events. In a TypeScript project, add crispstats.d.ts from TypeScript.

FAQ

No. The tag in gatsby-ssr.js is the whole install. There is no CrispStats plugin or npm package.
Yes. onRenderBody adds the tag to the HTML of every page Gatsby renders, and the script runs when a visitor opens one.
Yes, with a forwarding rule on your host. See proxy by host for Netlify, Vercel, Cloudflare and nginx.

Next steps

Custom events

Track signups, clicks and other actions with properties.

Proxy on Netlify

Serve the script from your own domain.

Exclude traffic

Keep your own visits and private paths out.
Gatsby details checked against Gatsby’s gatsby-ssr reference (https://www.gatsbyjs.com/docs/reference/config-files/gatsby-ssr/) on 2026-11-23.
Last modified on November 23, 2026