Documentation menuJavaScript frameworks: Gatsby

JavaScript frameworks

Add Loogika to Gatsby

Gatsby generates each page's head during the build, so you register the Loogika tag in gatsby-ssr.js. The Gatsby Script component is the second option when you want to decide when the script loads. Both methods put the script on every page of the site.

Last updated .

What do you need before you start?

Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. It looks like this, with your own 12-character site ID in place of YOUR_SITE_ID:

<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>

How do you add Loogika through gatsby-ssr.js?

Export onRenderBody from gatsby-ssr.js and pass the script to setHeadComponents. Gatsby then writes the tag into the head of every page it generates.

  1. Create gatsby-ssr.js in the root of your project if the file does not exist.
  2. Add the code below and replace YOUR_SITE_ID.
  3. Run gatsby build and deploy the public folder.
import React from "react";

export const onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <script
      key="loogika-analytics"
      src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID"
      defer
    />,
  ]);
};

How do you add Loogika with the Gatsby Script component?

Render the Script component from the gatsby package in a layout that wraps every page. Its strategy prop sets the load timing and accepts post-hydrate, idle or off-main-thread.

import React from "react";
import { Script } from "gatsby";

export default function Layout({ children }) {
  return (
    <>
      {children}
      <Script
        src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID"
        strategy="post-hydrate"
      />
    </>
  );
}

The example sits in src/components/layout.js. A page that does not use this layout loads no script, so the gatsby-ssr.js method is safer on sites with several layouts.

Does Loogika track Gatsby route changes?

Yes. A click on a Gatsby Link changes the address without a full page load, and Loogika records that change as a pageview. The script waits 500 milliseconds after a route change before it sends the pageview.

Route tracking depends on the SPA Navigation setting in Site Settings, which is on by default. With the setting turned off, you record route changes yourself from gatsby-browser.js:

export const onRouteUpdate = ({ location, prevLocation }) => {
  if (!prevLocation) return;
  if (location.pathname !== prevLocation.pathname && window.loogika) {
    window.loogika.pageview();
  }
};

The first call to onRouteUpdate has no prevLocation, and the code skips it. Test the default behaviour before you add this code.

How do you track custom events in Gatsby?

Any React component can call window.loogika.event() with an event name and optional properties. During the build Gatsby also runs your components on the server, where window does not exist, so check for it first.

import React from "react";

export default function TrackedButton({ label, eventName, eventData }) {
  const handleClick = () => {
    if (typeof window !== "undefined" && window.loogika) {
      window.loogika.event(eventName, eventData);
    }
  };

  return <button onClick={handleClick}>{label}</button>;
}

The custom events guide lists the limits for names and properties. It also shows how to track a click with a data attribute instead of JavaScript.

How do you check that Loogika is installed?

Bring up the deployed site in a separate browser tab and browse a few pages. The pageviews show up in the Loogika dashboard within a few seconds.

If nothing arrives, open the page source and find script.js?siteId=. A missing tag on a Gatsby site usually means the site was not rebuilt and deployed after the change. The browser Network tab should show script.js with status 200 and requests to /api/track.