Friday, 10 December 2021

No analytics cookies is set upon a consent was updated

I am using the Google Tag Manager with a single tag referencing a default Google Analytics script. My solution is based on the information from these resources:

The code is simple (commit):

index.html: define gtag() and set denied as a default for all storages

  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { window.dataLayer.push(arguments); }
    gtag('consent', 'default', {
      ad_storage: 'denied',
      analytics_storage: 'denied',
      personalization_storage: 'denied',
      functionality_storage: 'granted',
      security_storage: 'granted',
      wait_for_update: 400,
    });

Then load a user configuration from the localStorage and call update:

handleCookies(preferences) {
  console.log('handleCookies callback');
  gtag('consent', 'update', {
    ad_storage: preferences.ad,
    analytics_storage: preferences.analytics,
    personalization_storage: preferences.personalization,
  });
  console.log(window.dataLayer);
},

So far so good because I see the event queue is updated in dataLayer:

chrome console

As the consent is set I anticipate the the cookies will be set for the Google Analytics now. But they are missing. What stupid mistake have I done?

cookies box

GTM configuration

See https://beta.mezinamiridici.cz



from No analytics cookies is set upon a consent was updated

No comments:

Post a Comment