The usual way a Google tag ends up in a Shopify theme is the snippet from Google, pasted into layout/theme.liquid through the code editor. It works, and it loads gtag.js for every visitor on every page, before they have answered the cookie banner. On a Swiss store where I replaced such a snippet, Lighthouse counted about 1.2 MB of Google tag code and 631 ms of main-thread work, all of it before consent.
Here is the version I wrote instead: the same tag, loaded only once Shopify says analytics is allowed.
Let Shopify answer the consent question
Most cookie banner apps for Shopify write the visitor's decision into Shopify's Customer Privacy API. That gives the theme one place to ask, whatever banner the store uses. The API also knows the store's privacy settings: in regions where the store does not require a banner, analytics counts as allowed right away, so those visitors are tracked exactly as before.
The snippet
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
(function () {
var id = {{ settings.google_tag_id | json }};
var loaded = false;
function loadTag() {
if (loaded) return;
loaded = true;
var s = document.createElement('script');
s.async = true;
s.src = 'https://www.googletagmanager.com/gtag/js?id=' + encodeURIComponent(id);
document.head.appendChild(s);
gtag('js', new Date());
gtag('config', id);
}
window.Shopify.loadFeatures(
[{ name: 'consent-tracking-api', version: '0.1' }],
function (error) {
var privacy = window.Shopify.customerPrivacy;
if (error || !privacy) return loadTag();
if (privacy.analyticsProcessingAllowed()) return loadTag();
document.addEventListener('visitorConsentCollected', function (event) {
if (event.detail && event.detail.analyticsAllowed) loadTag();
});
}
);
})();
</script>
Simplified from the version I shipped. Render it after {{ content_for_header }}, because that is what defines Shopify.loadFeatures.
Three decisions worth making on purpose
Where the tag ID lives
I moved the ID out of the code and into a theme setting. When the marketing team switches to a combined Google tag container, or adds a new destination, they change one field in the theme editor, and nobody edits theme.liquid on a live store.
What happens if the privacy API never answers
On a Shopify storefront it should always load. If it does not, I chose to load the tag anyway (fail open) rather than silently lose all analytics and ads data on a live shop. A store with stricter legal requirements may prefer the opposite; either way, decide it in the code, not by accident.
Who sends Consent Mode signals
Many banner apps already send Google Consent Mode defaults and updates (gtag('consent', ...)). If yours does, do not push a second default from the theme; two scripts racing to set the same state is how you end up with data that nobody can explain. Check the app's settings first.
How to check it works
- Open the store in a private window from a region where your banner shows, and open the Network tab before answering it. Nothing should be requested from
googletagmanager.com. - Accept analytics. The request appears, and Google Tag Assistant sees the tag.
- Reload: with consent stored, the tag loads straight away.
- Decline in a fresh private window: no Google requests at all.
One more thing to watch
Theme code is not the only place a Google tag can come from. Shopify's Google & YouTube channel, apps and pixels can add their own. Before and after the change, look at the Network tab for every request to Google domains, so you know you replaced the tag rather than added a second one.
