Frameworks
Gatsby analytics with CrispStats
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.
Updated
Set up CrispStats on Gatsby
- 01
Step 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. - 02
Step 2: Add the tag with onRenderBody
Create
gatsby-ssr.jsat the root of your project, or add to the one you have. `onRenderBody` runs as each page is rendered, andsetHeadComponentsadds the element to that page’s head. Replace the key and domain with yours. If the file already callssetHeadComponents, add the element to that array instead of calling it twice.Replace the highlighted values with your own.
gatsby-ssr.jsimport * as React from "react";export const onRenderBody = ({ setHeadComponents }) => {setHeadComponents([<scriptkey="crispstats"deferdata-site="cs_pk_your_site_key"data-domains="example.com"src="https:/ / in. crispstats. com/ d. js"/>,]);}; - 03
Step 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-downloadsrenders as"true", which the script reads as a list of file extensions. Leave outdata-hash: Gatsby routes with real paths.gatsby-ssr.js<scriptkey="crispstats"deferdata-site="cs_pk_your_site_key"data-domains="example.com"data-outbound=""data-vitals=""data-errors=""src="https:/ / in. crispstats. com/ d. js"/> - 04
Step 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, addcrispstats.d.tsfrom the TypeScript page. For clicks you can also usedata-cs-eventattributes and no JavaScript.src/components/upgrade-button.jsexport function UpgradeButton() {return (<button onClick={() => window.crispstats? . ( "upgrade clicked", { from: "pricing" })}>Upgrade</button>);} - 05
Step 5: Rebuild, deploy and open the live site
gatsby-ssr.jsruns when pages are rendered, so restartgatsby developor run a new build after you change it. Visits tolocalhostare 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.
CrispStats on Gatsby
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 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.
- A pageview for the first load and every page change Gatsby’s links make with
Notes and limits
- There is no Gatsby plugin and no npm package from CrispStats. The element in
gatsby-ssr.jsis 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 tosetHeadComponents. - Leave out private paths with
data-exclude, and your own visits with the ignore flag in your browser. See 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.
- There is no Gatsby plugin and no npm package from CrispStats. The element in
The docs page covers the optional steps this guide leaves out.
Setup questions. Straight answers.
What people ask before they connect it.
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.
Yes. onRenderBody adds the tag to the HTML of every page Gatsby renders, and the script runs when a visitor opens one.
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.
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.
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.
Not answered here? Write to hello@crispstats.com
Go further
Related
- Privacy-friendly analytics for bloggers
See which posts get read to the end, where readers come from, including AI assistants, and keep your site free of analytics cookies.
- Bounce rate vs engagement
Bounce rate means different things in different tools.
- Track visitors from ChatGPT and Perplexity
Visitors sent by ChatGPT, Perplexity and Claude, plus AI crawler hits.
- React analytics
Add CrispStats to a React or Vite app with one script tag in index.html.
Other integrations
Frameworks
Site builders
Ecommerce platforms
Tag managers
Your traffic, crisp and clear
One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.