Cookie Consent Banner
Shopify provides a built-in cookie consent banner that helps your store comply with privacy regulations like GDPR, CCPA, and ePrivacy. In Hydrogen storefronts, this banner requires specific configuration to work correctly because the standard Liquid-based initialization is not available. This guide walks you through the complete setup, common pitfalls, and how to verify the banner is working.How It Works
Shopify’s cookie consent system consists of two parts:- Customer Privacy API (
consent-tracking-api.js) — Manages visitor consent state and cookies - Privacy Banner (
storefront-banner.js) — The visible UI that collects consent from visitors
<Analytics.Provider> component. The banner’s appearance and behavior (text, regions, position) are configured in Shopify Admin under Settings → Customer privacy.
The banner configuration (text, regions, theme) is managed entirely in Shopify Admin — not in your Hydrogen code. Your code only needs to pass the correct credentials.
Setup
Step 1: Configure the Consent Object in Your Root Loader
In your root loader’s critical data function (typicallyapp/.server/root.ts), include a consent object with the required fields:
Step 2: Pass Consent to Analytics.Provider
In your root layout (app/root.tsx), pass the consent object to Shopify’s <Analytics.Provider>:
Step 3: Pre-initialize the Storefront Access Token
This is the critical step that most setups miss. The banner script (storefront-banner.js) loads and self-initializes before React hydrates. It looks for window.Shopify.storefrontAccessToken to authenticate with the Storefront API — but in Hydrogen, this value is normally only available after hydration.
You must inject the token in an inline script so it’s available before the banner script executes:
Step 4: Configure the Banner in Shopify Admin
- Go to Shopify Admin → Settings → Customer privacy
- Under Cookie banner, click Set up or Edit
- Configure:
- Banner position — Bottom center, bottom left, etc.
- Banner text — Customize the consent message
- Button labels — Accept, Decline, Manage preferences
- Region visibility — Select which countries see the banner
- Theme — Light or dark, custom colors
- Click Save
Step 5: Set Up Environment Variables
Ensure these variables are set in your.env file and deployment environment:
The
PUBLIC_CHECKOUT_DOMAIN should be your custom checkout domain (e.g., checkout.yourdomain.com). If you don’t have a custom domain, use your .myshopify.com domain.Verifying the Banner Works
Preview Mode
Add?preview_privacy_banner=1 to any page URL to force the banner to display regardless of region or prior consent:
Checklist
Use this checklist to verify your setup:-
consent.withPrivacyBanneris set totruein the root loader -
storefrontAccessTokenis a valid 32-character public token -
checkoutDomainis correctly set -
window.Shopify.storefrontAccessTokenis injected via inline<script>in<head> - Banner appears with
?preview_privacy_banner=1 - Banner appears for visitors in configured regions (without preview param)
- Accepting/declining sets the
_tracking_consentcookie -
window.Shopify.customerPrivacyis not null after page load
Browser Console Check
Open DevTools and verify the privacy API initialized:Troubleshooting
”Error initializing banner: Missing access token”
Cause: The banner script loaded beforewindow.Shopify.storefrontAccessToken was set.
Fix: Add the inline <script> in Step 3 to pre-initialize the token in <head> before the banner script loads.
”Could not find liquid access token”
Cause: This is a harmless warning. The banner script first checks for a Liquid-based token (used in traditional Shopify themes). In Hydrogen, this path doesn’t exist, so the script falls back to the Storefront API path. Resolution: This warning can be safely ignored as long as the Storefront API token is correctly configured.Banner Shows in Preview Mode but Not in Production
Cause: The banner’sinit() function failed silently because it couldn’t find the access token during initial load. With ?preview_privacy_banner=1, the script forces initialization and bypasses this check.
Fix: Ensure Step 3 is implemented — the inline script must inject window.Shopify.storefrontAccessToken before React hydrates.
window.Shopify.customerPrivacy is null
Cause: The banner script failed to initialize, which means it never set up the Customer Privacy API. This prevents the banner from showing and analytics consent from working.
Fix: This is almost always caused by the missing access token. Follow Step 3.
Banner Doesn’t Show for Specific Regions
Cause: The region visibility configuration in Shopify Admin may not include the visitor’s country. Fix: Go to Shopify Admin → Settings → Customer privacy → Cookie banner and verify the region visibility list includes the target countries.React Hydration Error (#418) Alongside Banner Errors
Cause: A hydration mismatch between server-rendered HTML and client-rendered output. This is separate from the banner issue but often appears together. Resolution: UsesuppressHydrationWarning on the inline script tag (as shown in Step 3). For other hydration errors, check that your components render the same content on server and client.