<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 Replace
gatsby-ssr.js at the root of your project, or add to the one you have:gatsby-ssr.js
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:A bare JSX attribute such as
gatsby-ssr.js
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.jsloading fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/eon the first load and on each page change. The collector answers202. - 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_xxxxxxxxwith your key."site": "known"means the key is right.
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 withhistory.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
data-cs-event attributes and no JavaScript, see declarative events. In a TypeScript project, add crispstats.d.ts from TypeScript.
FAQ
Do I need a Gatsby plugin?
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.Does the script run on pages Gatsby builds at build time?
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.Can I serve the script from my own domain?
Can I serve the script from my own domain?
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.