Skip to content

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

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

  2. 02

    Step 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, 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.

    Replace the highlighted values with your own.

    gatsby-ssr.js
    import * 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"/>,]);};
  3. 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-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
    <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"/>
  4. 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, add crispstats.d.ts from the TypeScript page. For clicks you can also use data-cs-event attributes and no JavaScript.

    src/components/upgrade-button.js
    export function UpgradeButton() {return (<button onClick={() => window.crispstats?.("upgrade clicked", { from: "pricing" })}>Upgrade</button>);}
  5. 05

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

What the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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.
  • Notes and limits

    • 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.
    • To serve the script from your own domain, add a forwarding rule on your host, for example on Netlify. See proxy by host.

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

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.