Developer pages

Gatsby

Install HumanBehavior in Gatsby with gatsby-browser onClientEntry and GATSBY public env vars.

Gatsby has dedicated browser, SSR, and Node APIs. HumanBehavior belongs in gatsby-browser.*, specifically onClientEntry, so tracking starts only in the browser bundle.

Wizard detection

The wizard detects Gatsby when it sees:

SignalDetails
gatsby dependencyVersion read from package.json
gatsby-config.js / .ts / .mjsConfig-file fallback

Gatsby detection runs before generic React. Do not use the Vite React guide for Gatsby projects.

Package

pnpm add humanbehavior-js

Environment variables

GATSBY_HUMANBEHAVIOR_API_KEY=your-api-key
GATSBY_HUMANBEHAVIOR_INGESTION_URL=https://ingest.humanbehavior.co

Gatsby exposes GATSBY_ prefixed variables to browser code.

Init in gatsby-browser.tsx

import { HumanBehaviorTracker } from "humanbehavior-js";
import type { GatsbyBrowser } from "gatsby";

export const onClientEntry: GatsbyBrowser["onClientEntry"] = () => {
  const apiKey = process.env.GATSBY_HUMANBEHAVIOR_API_KEY;
  if (!apiKey) return;

  HumanBehaviorTracker.init(apiKey, {
    ingestionUrl: process.env.GATSBY_HUMANBEHAVIOR_INGESTION_URL,
  });
};

For JavaScript projects, use gatsby-browser.js with the same body.

Do not initialize in server files

Gatsby fileStatus
gatsby-browser.js / .tsxRecommended
gatsby-ssr.jsDo not initialize
gatsby-node.jsDo not initialize
React page component useEffectWorks but can repeat across pages; prefer browser API

onClientEntry fires once when the client bundle loads.

Options example

export const onClientEntry: GatsbyBrowser["onClientEntry"] = () => {
  const apiKey = process.env.GATSBY_HUMANBEHAVIOR_API_KEY;
  if (!apiKey) return;

  HumanBehaviorTracker.init(apiKey, {
    ingestionUrl: process.env.GATSBY_HUMANBEHAVIOR_INGESTION_URL,
    release: process.env.GATSBY_APP_VERSION,
    environment: process.env.GATSBY_DEPLOY_ENV,
    redactionStrategy: { mode: "visibility-first" },
  });
};

Only expose metadata that is safe for browser code.

Gatsby route changes

Gatsby's client router uses browser history. The SDK automatically records navigation events in normal Gatsby apps.

If you need a Gatsby-specific hook anyway, keep the tracker instance and use onRouteUpdate:

let tracker: HumanBehaviorTracker | null = null;

export const onClientEntry: GatsbyBrowser["onClientEntry"] = () => {
  const apiKey = process.env.GATSBY_HUMANBEHAVIOR_API_KEY;
  if (!apiKey) return;
  tracker = HumanBehaviorTracker.init(apiKey);
};

export const onRouteUpdate: GatsbyBrowser["onRouteUpdate"] = () => {
  void tracker?.trackPageView();
};

Most Gatsby sites do not need the manual onRouteUpdate call.

Common pitfalls

PitfallSymptomFix
Env var lacks GATSBY_undefined in browser bundleRename and rebuild
Init in gatsby-ssrServer/DOM errorsMove to gatsby-browser
Adding init to every pageDuplicate setup attemptsUse onClientEntry once
Deploy env changed without rebuildOld key remains in bundleRebuild/redeploy Gatsby
Source maps uploaded without matching releaseMinified stacks remain unreadableSet release and upload source maps

Wizard config summary

Wizard fieldValue
Integrationgatsby
Env varsGATSBY_HUMANBEHAVIOR_API_KEY, GATSBY_HUMANBEHAVIOR_INGESTION_URL
Detectiongatsby dependency or gatsby-config.*
Reference entrygatsby-browser.tsx

Error and release docs

Gatsby production builds often minify aggressively. For useful Issues:

  1. Set release in init.
  2. Upload source maps for that release.
  3. Stamp environment if you have staging/production.

See Source maps.

Verify

  1. Restart/rebuild after env changes.
  2. Open the Gatsby site in a browser.
  3. Confirm ingestion requests return 2xx.
  4. Navigate between pages.
  5. Open Replays.

Full checklist: Verify data is flowing.