Skip to main content
Back to Guides
Setup3 min readBy Armin Zaribaf

Gatsby Cookie Consent: Add GDPR Compliance to Your Static Site

How to add a compliant cookie consent banner to a Gatsby site. Covers gatsby-ssr.js script injection, blocking third-party scripts until consent, and handling consent in a static-site context.

Gatsby builds static HTML at compile time, but that doesn't mean it's cookie-free. The moment you add Google Analytics, a Meta Pixel, Hotjar, or any third-party embed, your static site sets cookies. And if you serve EU visitors, you need consent before those cookies fire.

The good news: adding consent to Gatsby is simpler than most frameworks because you control exactly when scripts load. There's no server-side rendering to worry about and no hydration race conditions.

Place the CookieBeam loader as the first element in <head>, loaded synchronously before GTM, analytics, or other scripts. Do not add async or defer, and exclude it from script-delay optimizers.

Quick setup: one script tag

The fastest way to add CookieBeam to Gatsby is through gatsby-ssr.js (or gatsby-ssr.tsx):

// gatsby-ssr.js
exports.onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <script
      key="cookiebeam"
      src="https://cdn.cookiebeam.com/banner/YOUR_BANNER_ID/default/loader.js"
    />,
  ]);
};

Replace YOUR_BANNER_ID with your actual banner ID from the CookieBeam dashboard. That's it. The banner loads on every page and handles consent collection, cookie blocking, and Consent Mode signals automatically.

If you're loading analytics or marketing scripts via Gatsby's gatsby-ssr.js or gatsby-browser.js, you need to make sure they don't fire before consent. Two approaches:

Option 1: Let CookieBeam handle it (recommended)

CookieBeam's auto-blocking mode detects known tracking scripts and blocks them until the visitor consents. If your scripts are loaded via standard script tags, this works out of the box.

Option 2: Conditional loading in gatsby-browser.js

// gatsby-browser.js
exports.onClientEntry = () => {
  let gaLoaded = false;
  const maybeLoadGA = (categories) => {
    if (gaLoaded || !categories.includes('analytics')) return;
    gaLoaded = true;
    // Load Google Analytics only after consent
    const script = document.createElement('script');
    script.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXX';
    document.head.appendChild(script);
  };
  // A choice made on this page, and a stored choice re-applied on page load
  const onConsent = (e) => maybeLoadGA(e.detail.categories);
  window.addEventListener('cookiebeam:consentUpdate', onConsent);
  window.addEventListener('cookiebeam:consentRestored', onConsent);
  // The stored choice may already have been restored before this code ran
  try {
    const cb = window.CookieBeam;
    if (cb && cb.validConsent()) maybeLoadGA(cb.getUserPreferences().acceptedCategories);
  } catch (err) {
    // Runtime not started yet; the events above deliver the choice
  }
};

This gives you full control over what loads and when.

If you use Google Tag Manager or gtag.js, add the consent default before the GTM script loads:

// gatsby-ssr.js
exports.onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    // CookieBeam handles consent default + updates automatically
    <script
      key="cookiebeam"
      src="https://cdn.cookiebeam.com/banner/YOUR_BANNER_ID/default/loader.js"
    />,
    // GTM loads after CookieBeam sets the default consent state
    <script
      key="gtm"
      dangerouslySetInnerHTML={{
        __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXX');`
      }}
    />,
  ]);
};

CookieBeam sets consent('default', ...) before GTM initializes, so Google tags respect the consent state from the first pageview.

Does the cookie banner work with Gatsby's client-side routing?
Yes. CookieBeam loads once and persists across Gatsby's client-side page transitions. The banner state (accepted/rejected) is stored in cookies, so it doesn't re-appear on navigation.
Will the consent banner affect my Lighthouse score?
The CookieBeam loader is a small script that must stay first in the head as a plain synchronous script (no async or defer), because it has to run before the trackers it blocks. It shouldn't significantly affect your Core Web Vitals.
Can I use gatsby-plugin-cookiebeam?
There's no official Gatsby plugin yet, but you don't need one. The script tag approach in gatsby-ssr.js is the recommended method and gives you the same result with fewer dependencies.

6 min read

Manage CookieBeam from ChatGPT or Claude

Connect an AI assistant to your CookieBeam account over MCP and manage banners, scans and analytics from a conversation. Covers connecting, what the assistant can do, example prompts, access control and known limits.

8 min read

Shopify Cookie Consent: GDPR and CCPA Setup Guide for 2026

Shopify's built-in cookie banner covers the basics, but falls short for stores selling into the EU, California, or multiple regions at once. This guide walks through what Shopify gives you, where it stops, and how to add a proper consent layer with CookieBeam.

4 min read

Remix Cookie Consent: Server-Side Rendering and GDPR

Add GDPR-compliant cookie consent to a Remix app. Covers script placement in root.tsx, streaming SSR considerations, consent-aware loaders, and the framework-specific quirks that trip up most implementations.