---
title: "Gatsby Cookie Consent: GDPR-Compliant Banner Setup | CookieBeam"
description: "Add cookie consent to Gatsby in minutes. Script injection via gatsby-ssr.js, blocking trackers until consent, and Google Consent Mode integration for static sites."
url: "https://cookiebeam.com/guides/gatsby-cookie-consent-guide-2026"
lang: "en"
last_updated: "2026-10-17T18:54:14.464Z"
---

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

## Why Gatsby sites need cookie consent

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.

## Blocking scripts until consent

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.

## Google Consent Mode with Gatsby

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.

> **Note: Gatsby plugins vs script tags**
>
> Gatsby plugins like gatsby-plugin-google-gtag inject scripts automatically. If you use these, CookieBeam's auto-blocking can intercept them. But for full control, consider loading tracking scripts manually via gatsby-ssr.js instead of plugins, so you can conditionally load them based on consent.

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