🎛️How to Integrate Google Consent Mode V2 on Your Shopify Checkout Page
This guide explains how to integrate Google Consent Mode V2 on your Shopify checkout page, specifically to trigger the consent signal only if the customer has previously accepted your cookie banner on earlier pages (e.g., homepage, product pages, etc.).
🔧 Steps to Implement
Log in to your Shopify Admin Go to: https://admin.shopify.com
Navigate to Settings In the lower-left corner of your admin panel, click Settings.
Go to Customer Events In the Settings menu, click on Customer events.
Open the Custom Pixels tab At the top of the page, switch to the Custom pixels tab.
Add a new custom pixel
Click the “Add custom pixel” button.
Give your pixel a name (e.g.,
Consentik - Google Consent Mode V2
).
Replace the default code
Remove all existing code in the Code editor.
Paste the following code snippet instead:
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
if(/.+\/checkouts?\/.*/.test(window.location.href)) {
//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
});
updateGtagConsentFromCookie();
}
function getCookieValue(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
return null;
}
function parseConsentikCookie() {
const raw = getCookieValue('_consentik_cookie');
if (!raw) return null;
try {
return JSON.parse(decodeURIComponent(raw));
} catch (e) {
console.error('Invalid _consentik_cookie JSON', e);
return null;
}
}
function updateGtagConsentFromCookie() {
const parsed = parseConsentikCookie();
if (!parsed || !Array.isArray(parsed)) return;
const selected = parsed[0].categoriesSelected || [];
const has = (category) => selected.includes(category);
gtag("consent", "update", {
ad_user_data: has('marketing') ? 'granted' : 'denied',
ad_personalization: has('marketing') ? 'granted' : 'denied',
ad_storage: has('marketing') ? 'granted' : 'denied',
analytics_storage: has('analytics') ? 'granted' : 'denied',
functionality_storage: has('preferences') ? 'granted' : 'denied',
personalization_storage: has('preferences') ? 'granted' : 'denied',
security_storage: 'granted' // typically always granted
});
}
Click Save Once you’ve pasted the code, click Save to apply the changes.
✅ You’re Done!
Google Consent Mode V2 will now properly trigger on the checkout page only if the user has previously accepted cookies.
Last updated
Was this helpful?