Google Tag Manager
This document explains how Google Tag Manager (GTM) integrates with our Hydrogen application, how analytics events flow through our system, and how to safely modify Heap-related tags in the GTM container.
Application Integration Overviewβ
Our GTM integration is handled through:
app/components/GoogleTagManager/googleTagManager.component.tsx
This file exports a GoogleTagManager component that listens for analytics events using the subscribe method from Hydrogenβs useAnalytics hook.
useAnalytics Subscriptionβ
The application does not use a single enum for analytics events.
Instead, it uses multiple enums grouped by context, including:
- CustomAnalyticsEvent
- StandardAnalyticsEvent
- CustomHomePageAnalyticsEvent
- CustomLandingPageAnalyticsEvent
- CustomPDPAnalyticsEvent
- CustomLayoutAnalyticsEvent
Each enum represents a set of event names specific to a particular part of the application.
The GoogleTagManager component listens for all of them through the shared analytics subscription.
When an event occurs, the component calls our gtag function, which pushes an event into:
window.dataLayer
This allows GTM to receive and act on application-level analytics events.
Publishing Events From Componentsβ
Components throughout the application publish events using:
publish(CustomAnalyticsEvent.SomeEventName, payload)
These must match the enum values exactly to be recognized by the GTM integration.
GTM Container Accessβ
You must have access to Birdy Greyβs GTM container.
Request access through management.
GTM Link - https://tagmanager.google.com
Container name: www.birdygrey.com
When you open it, your workspace will default to:
Default Workspace
This functions as GTMβs "main branch."
Workspace Limitsβ
GTM only allows 3 workspaces at a time.
Your workflow must be:
- Create a new workspace
- Make changes
- Publish changes
- Delete your workspace
GTM as the Gateway to Heapβ
The flow of analytics is:
Application β gtag β window.dataLayer β GTM β Heap
GTM receives our application events and forwards them to Heap using tags configured inside the GTM UI.
Workflow for Updating Heap Eventsβ
1. Create a Workspaceβ
Create a new workspace with a descriptive name for your changes.
2. Navigate to Tagsβ
Open the Tags tab in the left sidebar.
Heap event tags follow this naming format:
Heap - [Event Name]
3. Modify or Create a Tagβ
Search for the tag you need.
Make your edits and click Save in the top-right corner.
4. Test Using Preview Modeβ
Click Preview in the top header.
This launches Tag Assistant so you can verify tags are firing.
5. Publish to Stagingβ
Click Submit, select staging, and deploy changes.
Confirm the event works correctly on staging.
6. Deploy to Productionβ
After staging is verified:
- Publish to production
- Delete your workspace
- GTM automatically creates a new version
Helpful Tipsβ
- Install the Tag Assistant Chrome extension to view fired tags
- Disable ad blockers to avoid blocking tracking
- GTM custom JavaScript variable and tag scripts should avoid modern ES6+ syntax for maximum compatibility, meaning:
- No arrow functions
- No optional chaining
- No trailing commas
- You may create custom variables under:
Variables β User-Defined Variables
(Check for existing variables first.)
Summaryβ
GoogleTagManagerlistens to analytics events published by the app- Events must match the names in
CustomAnalyticsEvent - Events push into
window.dataLayer, which GTM uses to trigger Heap events - All GTM edits should occur in a temporary workspace
- Test in Preview β publish to staging β publish to production β delete workspace