As Google enforces Consent Mode v2, marketers must not only collect consent—but also route tracking events dynamically based on whether consent has been granted.
🧰 Prerequisites
Tool/Tech | Requirement |
---|---|
OpenCart v3.x/v4.x | With editable theme files |
Google Tag Manager (Web) | Installed properly |
GA4 Tag in GTM | Configured with Consent Mode v2 |
Cookiebot / OneTrust | Cookie consent tool that sets gtag() correctly |
🎯 Goal
✅ Read GA4 consent signals (analytics_storage
, ad_storage
, etc.)
✅ Conditionally fire GA4, Google Ads, Meta Pixel, TikTok, etc.
✅ Avoid firing events when user has not granted consent
✅ Centralize platform routing based on real consent status
🔧 Step 1: Enable Google Consent Mode v2
A. Add a Consent Initialization Tag in GTM
- Tag Type: Consent Initialization – Configuration
- Trigger: Consent Initialization – All Pages
- Settings:
ad_storage: denied
analytics_storage: denied
personalization_storage: denied
functionality_storage: granted
security_storage: granted
This prevents all Google tags from firing until consent is updated.
🔁 Step 2: Sync Cookie Consent with Google Consent Mode
A. Create a Custom HTML tag in GTM
<script>
window.addEventListener('CookieConsentDeclaration', function () {
gtag('consent', 'update', {
ad_storage: Cookiebot.consents.marketing ? 'granted' : 'denied',
analytics_storage: Cookiebot.consents.statistics ? 'granted' : 'denied',
personalization_storage: Cookiebot.consents.preferences ? 'granted' : 'denied',
security_storage: 'granted'
});
});
</script>
- Trigger: Consent Initialization – All Pages
🧠 Adjust this if you use OneTrust, Axeptio, or another tool instead.
📦 Step 3: Create a Custom JavaScript Variable to Read Consent State
Variable: JS - Consent Analytics
function() {
try {
return gtag && gtag.get('consent', 'default')?.analytics_storage || 'denied';
} catch (e) {
return 'denied';
}
}
Variable: JS - Consent Ads
function() {
try {
return gtag && gtag.get('consent', 'default')?.ad_storage || 'denied';
} catch (e) {
return 'denied';
}
}
These read the live consent signal pushed by Google Consent Mode.
🧠 Step 4: Route Events Based on Consent in GTM Tags
Let’s use these variables to control which tags fire and when.
A. GA4 Tag
- Enable Built-in Consent Checks
- Consent type:
analytics_storage
- Add trigger condition:JS
- Consent Analytics equals granted
B. Google Ads Tag
- Consent type:
ad_storage
- Trigger condition:JS
- Consent Ads equals granted
C. Meta Pixel Tag (Custom HTML)
Use a Custom HTML Tag that fires only if marketing consent is true
:
<script>
fbq('init', 'PIXEL_ID');
fbq('track', 'PageView');
</script>
Trigger: Custom Event CookieConsentDeclaration
Condition:
Cookiebot.consents.marketing equals true
🔁 Step 5: Shared Event Dispatch Logic (Centralized Routing)
Instead of duplicating event code, push a universal event into dataLayer:
dataLayer.push({
event: "ecommerce_event",
action: "purchase",
order_id: "12345",
value: 199.99,
consent_analytics: '{{JS - Consent Analytics}}',
consent_ads: '{{JS - Consent Ads}}'
});
Then build separate tags for GA4, Ads, Meta, etc., each using:
- The same trigger:
ecommerce_event
- With conditions on
consent_analytics
orconsent_ads
📊 Step 6: Testing Consent-Based Event Routing
Tool | Use |
---|---|
GTM Preview Mode | Ensure event routing based on consent |
DevTools > Console | Run gtag.get('consent', 'default') |
Google Tag Assistant | Validate tag fire status with consent logs |
Real-time GA4 DebugView | Confirm events only appear post-consent |
🧪 Bonus: Push Consent Info into GA4 as Parameters
You can send consent context into GA4 for visibility:
gtag('event', 'purchase', {
consent_analytics: '{{JS - Consent Analytics}}',
consent_ads: '{{JS - Consent Ads}}'
});
This helps in reporting opt-in vs opt-out conversion performance.