OpenCookies

Guides

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

  1. Google Analytics 4 sets cookies (_ga and _ga_ID) as soon as its script loads.
  2. 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.
  3. The OpenCookies test checks it: no analytics cookie before the choice or after “Refuse”, and _ga only after “Accept”.

The steps

  1. Find out how GA4 is loaded

    There are three common cases: the gtag.js code 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.com or google-analytics.com: if a request leaves before any click on the banner, GA4 is loading too early.

  2. 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.

  3. 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.

  4. 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”.

If _ga is present before the choice, GA4 loads too early: go back to step 1 to find where it is added.

Test my site

Sources

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.

Which cookies does GA4 set?

Mainly _ga (identifies a browser) and _ga_ID (session state). Their default lifetime is detailed in the Google documentation above and in our pages _ga and _ga_ID.

← All guides · Cookie dictionary