orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
4k stars 410 forks source link

Google Analytics 4 Script can't be blocked #402

Closed edgarhuber closed 2 years ago

edgarhuber commented 2 years ago

Hello Orest, first of all i have to give you props for this great project! Thank you very much!

It seems as if the Google Analytics 4 Script can't be blocked. It's loading although I've used the described way to implement and block it and it's shown in the Google Tag Assistant different to the Google Tag Manager Script in your Demo Files.

I've tried cookieconsent-2.8.6 and cookieconsent-3.0-beta.

That's 2.8.6:

<script type="text/plain" data-cookiecategory="analytics">
  window.dataLayer = window.dataLayer || [];
  function gtag(){window.dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
</script>

And that's 3.0-beta:

<script type="text/plain" data-category="analytics">
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
</script>

Examples (will be deleted soon): Analytics script is loading without consent: https://edgarunger.com/cookieconsent-2.8.6/demo/demo_gtm/index.html Analytics script is loading without consent: https://edgarunger.com/cc3/demo/demo_gtm/index.html Google Tag Manager script is loading correctly after consent is given: https://edgarunger.com/cc3/demo/demo_basic/index.html

I can't find my mistake. I don't know, whether it has something to do with Google Consent Mode or not.

Another issue I came across is that in your demo_gtm (2.8.6 & 3.0-beta) the modal doesn't close after I change the consent status and click on a button. Instead I have to click two times. I think, this behaviour is not intended, isn't it?

orestbida commented 2 years ago

Hi @edgarhuber,

the scripts with data-category (and data-cookiecategory) are working fine in all 3 demos, as far as I can tell. They are only enabled if the analytics category is accepted.

Another issue I came across is that in your demo_gtm (2.8.6 & 3.0-beta) the modal doesn't close after I change the consent status and click on a button. Instead I have to click two times

There is an issue in this demo. Inside the onChange callback, you're executing the following code where the cc variable is not defined:

if (!cc.acceptedCategory('analytics')) {
    typeof gtag === 'function' && gtag('consent', 'update', {
        'analytics_storage': 'denied'
    });
}
  1. use CookieConsent.acceptedCategory;
  2. alternatively, you can place — at the top of the file — the following line so that you can refer to the plugin via the cc name: const cc = CookieConsent;

Edit: Looks like this issue is present in the 2nd demo. Will fix it later.

orestbida commented 2 years ago

Updated the example with the recommended Google Consent Mode, for a more granular control.

edgarhuber commented 2 years ago

Hi @edgarhuber,

the scripts with data-category (and data-cookiecategory) are working fine in all 3 demos, as far as I can tell. They are only enabled if the analytics category is accepted.

Thank you, Orest. I think the mistake lies in Google's Tag Assistant Browser Plugin where the Tag is shown as active whether it's activated or not. But when I check the browser for cookies the Analytics 4 tag is working perfectly with your cookie consent as it should. I guess, Tag Assistant is the wrong tool to check if the consent is working or not. I think, it's even outdated.

Thanks also for updating the example and helping me out!