Dokumentacja
Przeglądaj dokumentację

Add Privatus Analytics to Gatsby

Add cookieless analytics to a Gatsby site with setHeadComponents in gatsby-ssr.js, so the tracker is in every page's head in production builds.

Zobacz jako Markdown

Create or edit gatsby-ssr.js in the root of your project:

jsx
import React from 'react'

export const onRenderBody = ({ setHeadComponents }) => {
  if (process.env.NODE_ENV !== 'production') return

  setHeadComponents([
    <script
      key="privatus-analytics"
      defer
      src="https://privatusstats.com/js/pa.js"
      data-site="pa_YOURSITEID"
      data-modules="engage,auto,vitals,clicks,ask"
    />,
  ])
}

onRenderBody runs when Gatsby renders the HTML, and setHeadComponents adds the elements to the head of every page. gatsby develop is not a production build, so local work is not counted. Run gatsby build to check.

Gatsby is a single-page app after the first load. The tracker follows its history.pushState navigations automatically. See single-page apps.

Gatsby's Head export is per page, so it is the wrong place for a site-wide tracker. Use gatsby-ssr.js.