digitoimistodude / air-cookie

13 stars 2 forks source link

Google Consent Mode V2 #6

Open Ciantic opened 4 months ago

Ciantic commented 4 months ago

Not a fan, but SEO-snakes are starting to sell this to customers.

Taken from Google's Consent Mode V2 documentation

It works like this:

  1. Add Google Script always on all users, even before they have accepted any cookies:
<script>
// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}

// Set default consent to 'denied' as a placeholder
// Determine actual values based on your own requirements
gtag('consent', 'default', {
  'ad_storage': 'denied',
  'ad_user_data': 'denied',
  'ad_personalization': 'denied',
  'analytics_storage': 'denied'
});
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID">
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());
  gtag('config', 'TAG_ID');
</script>

With deny on all storages. Then Google can collect s### on users who haven't accepted anything.

  1. When clicking a category from consent dialog we can call these functions:
<!-- Create one update function for each consent parameter -->
<script>
  function consentGrantedAdStorage() {
    gtag('consent', 'update', {
      'ad_storage': 'granted'
    });
  }
</script>

Allowed properties: ad_storage, analytics_storage, ad_user_data, ad_personalization Allowed values: granted or denied

ronilaukkarinen commented 4 months ago

This is actually clever. Thanks for sharing.