All things Shopify and commerce
Hi,
It seems that everyone is having a hard time figuring out how to implement Google Consent Mode V2 on their Shopify, without the help of a 3rd party app. It's a shame this feature is not native, given that it impacts the conversion data if it is not implemented.
I came up with the code below that I now run on all my stores, it only requires Shopify's native banner app to work. It does the job perfectly and passes Google check : https://support.google.com/tagmanager/answer/14218557?hl=en
This snippet should be pasted as is in the <head> tag of your /layout/theme.liquid and layout/password.liquid files.
What it does :
Feel free to let me know how it can be improved.
<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} const defaultConsentState = '{{ localization.country.iso_code }}' === 'US' ? 'granted' : 'denied'; gtag('consent', 'default', { 'ad_storage': defaultConsentState, 'analytics_storage': defaultConsentState, 'ad_user_data': defaultConsentState, 'ad_personalization': defaultConsentState }); (function loadConsentMode() { addEventListener("DOMContentLoaded", () => { document.body.addEventListener('click', (e) => { const acceptButtonId = 'shopify-pc__banner__btn-accept'; const declineButtonId = 'shopify-pc__banner__btn-decline'; if(![acceptButtonId, declineButtonId].includes(e.target.id)) return; const newState = e.target.id === acceptButtonId ? 'granted' : 'denied'; gtag('consent', 'update', { 'ad_user_data': newState, 'ad_personalization': newState, 'ad_storage': newState, 'analytics_storage': newState }); }) }); })(); </script>
Hello @AlexF_ ,
This is a great effort, but there are some important considerations to keep in mind:
1️⃣Different privacy laws require different consent handling, and applying a strict policy everywhere except the US may not be accurate in practice.
2️⃣ The snippet doesn’t account for users who have already given consent or those who may want to change their preferences later.
3️⃣ It won’t work seamlessly with Google Tag Manager (GTM) setups.
4️⃣Server-side tracking is not supported, which can impact data collection and attribution.
5️⃣ Relying on custom code means it may not stay up to date with Shopify’s default banner changes.
6️⃣ GDPR compliance is much more than just enabling Google Consent Mode – a stable and comprehensive solution ensures proper GA tracking, effective conversions, and long-term compliance.
Having a structured approach helps businesses stay compliant while optimizing data-driven decisions. 🚀
Hi,
I agree with everything you wrote ! But here are my answers :
Just to be clear, this code does not intend to replace the services provided by the apps such as yours (which is pretty good from what I heard), this is obvious. However, Shopify's relying on 3rd party apps for basic, even required functionalities is very problematic, and is a pain for small merchants who are just starting their online business and don't want to put tens or hundreds of bucks each month towards features that should already be part of the platform. I believe this snippet is fine for small merchants making their money in the US or in the EU and who just want to comply with Google requirements ; for anything more, they'll probably be happy paying for your services.
Hey there,
i was looking for something like this for weeks now. Do you know if this Code also works with Shopify Pixel / Customer Events Section (for example: I'm running Google Ads Tracking as a Customer Event and the user needs to click Accept on the Shopify Cookie Banner)? And if so, do I still need to paste the Code in the theme.liquid file or do I have to paste it in the alredy existing code in the Customer Events section?
Hi,
If your GA is setup through a custom pixel and not through the "Google & Youtube" sales channel/app, the code should certainly be placed in the custom pixel as well. Shopify allows to track clicks on elements through their own events : https://shopify.dev/docs/api/web-pixels-api/dom-events/clicked
They also provide events for consent that should probably be favored to implement this, but they do not explicitly state that the consent change is triggered through Shopify's native banner, some testing would be required.
However, there's a problem regarding the setup of default consent state, as it seems that the country the customer is currently navigating on is not obtainable inside the Custom pixel sandbox.
An alternative, with a way to manage consent states within Shopify, is our app, Pandectes GDPR Compliance, which integrates with Shopify's Custom Pixel, allowing merchants to respect user consent while ensuring accurate data collection.
We've also published a detailed guide on how to use Pandectes Consent Event with Shopify Custom Pixel here:
🔗 Use Pandectes Consent Event with Shopify Custom Pixel
This integration helps Shopify merchants seamlessly update tracking scripts based on user consent preferences, ensuring compliance with GDPR, Google Consent Mode, and other regulations.
Let me know if you have any questions!
@TimS95 hey Tim, you may want to give this comprehensive tutorial a read on how to handle consent with the custom pixel & customer events:
https://karolkrajcir.com/post/ga4-consent-based-conversion-tracking-on-shopify-with-custom-pixels-co... Let me know if you have any questions - I'd be happy to help!
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025