Google Analytics 4 and cookies: what to do before consent
By the OpenCookies team · published October 22, 2026 · updated October 22, 2026
The essentials in 3 lines
- Google Analytics 4 sets cookies (
_gaand_ga_ID) as soon as its script loads. - Showing a banner is not enough: until the visitor has chosen, GA4 should not load normally, or it should be limited by Google Consent Mode.
- The OpenCookies test checks it: no analytics cookie before the choice or after “Refuse”, and
_gaonly after “Accept”.
The steps
- Find out how GA4 is loaded
There are three common cases: the
gtag.jscode pasted directly into your pages, a tag in Google Tag Manager, or a CMS plugin. The blocking method depends on the case, so start here.In your browser, open the “Network” tab of the developer tools and look for
googletagmanager.comorgoogle-analytics.com: if a request leaves before any click on the banner, GA4 is loading too early. - Set “denied” by default, before the Google tag
Google Consent Mode lets you tell GA4 that nothing is accepted until the visitor has chosen. This code must come before the Google tag:
<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('consent', 'default', { ad_storage: 'denied', analytics_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied' }); </script>After the choice, the banner must update these values (
gtag('consent', 'update', …)). With OpenCookies this is done for you: see the Consent Mode v2 guide. - Choose the blocking level
There are two approaches. “Basic” mode does not load the Google tag at all before consent. “Advanced” mode loads it, but it only sends cookieless signals until consent is given. Basic mode is the more cautious; advanced mode gives Google more modeling data. Choose with your legal adviser.
- Put the banner first in the page head
With OpenCookies, a single line, placed first in
<head>, neutralizes known tracking tags (including GA4) before the choice and restarts them after “Accept”:<script src="https://opencookies.com/banner/oc.js" data-site="KEY"></script>
Check it with the OpenCookies test
Run the OpenCookies test on your site address. The report compares three moments: before any choice, after “Refuse”, after “Accept”.
- Before any choice and after “Refuse”: no
_gacookie and no measurement request to Google. - After “Accept”:
_gaand measurement appear. That is expected, the visitor said yes.
If _ga is present before the choice, GA4 loads too early: go back to step 1 to find where it is added.
Sources
- Google — Cookie usage on websites (GA4)
- Google — Set up consent mode on websites
- EDPB — Guidelines 05/2020 on consent
- ICO (UK) — Guidance on the use of storage and access technologies
This guide describes a technical approach, not legal advice.
Frequently asked questions
Can GA4 be used without a banner?
In the European Union and the UK, audience measurement that stores or reads information on the visitor’s device generally requires consent under the ePrivacy rules. Some national authorities, such as the French CNIL, describe narrow exemptions for strictly configured tools; see the sources above. This guide is not legal advice.