GA4 with Cookie Compliance

Cookbook · guide Last verified View raw markdown (for agents)

In short: keep the Cookie Compliance snippet as the first script in <head>, then paste Google's GA4 tag below it. The banner holds the recognised Google loader until the visitor allows Content Personalisation, in regions whose rule requires consent first.

You need #

Where it goes #

Google's docs place the standard tag immediately after the opening <head> tag. With Cookie Compliance installed, keep the banner snippet first, then paste the GA4 tag below it — same shape Google gives you:

<!-- Cookie Compliance snippet first, then: -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXX');
</script>

Firing GA4 through Google Tag Manager instead? Put the banner above the GTM container and gate tags with consent triggers — see gtm and the GTM cookie consent guide.

What the order controls #

The banner recognises the googletagmanager.com loader (gtag/js or gtm.js) and, with Google Consent Mode off, holds it under category 2 Content Personalisation. In regions whose rule requires consent first, the tag waits until the visitor allows that category; it starts after Allow. A script placed above the banner snippet runs straight away.

Confirm the result #

View the page source: the banner snippet comes first in <head>, the GA4 tag after it. Before choosing, in a region whose rule requires consent first, _ga and _ga_<container-id> are absent; the held script shows type="javascript/blocked" in the Elements panel. After Allow, those cookies appear. Browser checks: plain-html; full procedure: verify-install.

Gotchas #

More help #