Google Tag Manager integration in Hydrogen
This recipe integrates Google Tag Manager (GTM) into your Hydrogen storefront, enabling you to track user interactions, ecommerce events, and implement marketing tags without modifying code.
Hydrogen includes built-in support for the Customer Privacy API, a browser-based JavaScript API that you can use to display cookie-consent banners and verify data processing permissions.
Key features:
- GTM script integration with proper CSP nonce support
- Content Security Policy configuration for GTM domains
- Analytics integration for product views via dataLayer
- Extensible dataLayer implementation for custom events
- Support for GTM's preview mode
- Customer Privacy API integration
- Support for various analytics events by subscribing to event types using the GoogleTagManager component
The recipe includes:
- Content Security Policy updates in entry.server.tsx for GTM domains
- GTM script tags in the head and body sections
- GoogleTagManager component that subscribes to analytics events
- Proper nonce attributes for security compliance and compatibility with the Content Security Policy
Replace GTM-<YOUR_GTM_ID> with your actual Google Tag Manager container ID in both script locations
Replace GTM-<YOUR_GTM_ID> with your actual Google Tag Manager container ID in both script locations
Anchor to RequirementsRequirements
Prerequisites:
- A Google Tag Manager account and container ID
- Customer privacy settings configured in your Shopify admin (for cookie consent)
- Basic understanding of GTM and dataLayer events
- Knowledge of Shopify's analytics events
To enable cookie consent:
- In your Shopify admin, go to Settings → Customer Privacy → Cookie Banner.
- Configure region visibility for the cookie banner.
- Customize the banner's appearance and position as needed.
Anchor to IngredientsIngredients
New files added to the template by this recipe.
| File | Description |
|---|---|
| app/components/GoogleTagManager.tsx |
Anchor to Step 1: Document GTM setup in the READMEStep 1: Document GTM setup in the README
Update the README file with GTM-specific documentation and setup instructions.
File
Anchor to Step 2: Add GTM domains to Content Security PolicyStep 2: Add GTM domains to Content Security Policy
Configure CSP headers to allow Google Tag Manager and Analytics scripts.
Anchor to Step 3: Create the analytics componentStep 3: Create the analytics component
Build a component that subscribes to Hydrogen analytics events and pushes them to GTM's dataLayer.
File
Anchor to Step 4: Add GTM scripts to the appStep 4: Add GTM scripts to the app
Insert Google Tag Manager tracking code in the head and body sections.
File
Anchor to Next stepsNext steps
After applying this recipe:
-
Replace
GTM-<YOUR_GTM_ID>with your actual container ID in app/root.tsx (2 locations). -
Configure GTM in your Google Tag Manager dashboard:
- Set up tags for Google Analytics 4 or other tracking services
- Create triggers for the
viewed-productcustom event - Configure ecommerce data layer variables
-
Extend the GoogleTagManager component to track additional events:
- cart_updated
- collection_viewed
- search_performed
- checkout_started
-
Test your implementation:
- Use GTM Preview mode to verify tags are firing
- Check browser console for dataLayer pushes
- Verify CSP is not blocking any GTM resources
-
Enable customer privacy settings for GDPR compliance in your Shopify admin.