🎛️How to Integrate Google Consent Mode V2 on Your Shopify Checkout Page
PreviousGoogle Consent Mode V2 IntegratedNextMicrosoft Universal Event Tracking Consent Mode Integrated
Last updated
Last updated
analytics.subscribe("page_viewed", function (event) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
if(/.+\/checkouts?\/.*/.test(window.location.href)) {
let timeout = 0
const checkoutInterval = setInterval( async () => {
timeout += 100
if(typeof gtag != 'undefined') {
//default consent
gtag("consent", "default", {
ad_user_data: "denied",
ad_personalization: "denied",
ad_storage: "denied",
analytics_storage: "denied",
functionality_storage: "denied",
personalization_storage: "denied",
security_storage: "denied",
wait_for_update: 500
});
// cosent update
await updateGtagConsentFromCookie();
clearInterval(checkoutInterval)
}
else if(timeout >= 3000) {
clearInterval(checkoutInterval)
}
},100)
// Google tag magner code will come here
// Checkout event will go here eg: add_payment_info, add_shipping_info, purchase
}
async function getCookieValue(name) {
return await browser.cookie.get(name)
}
async function parseConsentikCookie() {
const raw = await getCookieValue('_consentik_gcm');
if (!raw) return null;
try {
// Cookie might be encoded (like %5B...%5D), so decode first
return JSON.parse(decodeURIComponent(raw));
} catch (e) {
console.error('Invalid _consentik_gcm JSON', e);
return null;
}
}
async function updateGtagConsentFromCookie() {
const parsed = await parseConsentikCookie();
if (!parsed || !Array.isArray(parsed)) return;
const selected = parsed || [];
const has = (category) => selected.includes(category);
gtag("consent", "update", {
ad_user_data: has('advertising') ? 'granted' : 'denied',
ad_personalization: has('advertising') ? 'granted' : 'denied',
ad_storage: has('advertising') ? 'granted' : 'denied',
analytics_storage: has('analytics') ? 'granted' : 'denied',
functionality_storage: has('functional') ? 'granted' : 'denied',
personalization_storage: has('functional') ? 'granted' : 'denied',
security_storage: 'granted' // typically always granted
});
console.log({dataLayer})
}
});