brainsum / cookieconsent

A Javascript based solution for blocking/allowing even 3rd party cookies to comply with GDPR
https://brainsum.github.io/cookieconsent/
ISC License
407 stars 88 forks source link

How block google analytics by google tag manager? #75

Open tdrzewosz opened 3 years ago

tdrzewosz commented 3 years ago

Hello, I am using Google Tag Manager include Google Analytics.

If I accept the consent Google Analytics are available, but after I decline the GA the GA Cookies will not be deleted.

analytics: { // Existing category Unique name // This example shows how to block Google Analytics category: 'tracking', // Type of blocking to apply here. // This depends on the type of script we are trying to block // Can be: dynamic-script, script-tag, wrapped, localcookie type: 'dynamic-script', // Only needed if "type: dynamic-script" // The filter will look for this keyword in inserted scipt tags // and block if match found search: 'analytics', // List of known cookie names or Regular expressions matching // cookie names placed by this service. // These willbe removed from current domain and .domain. cookies: [ { // Known cookie name. name: '_gid', // Expected cookie domain. domain:.foobar.de }, { // Known cookie name. name: '_gcl_aw', // Expected cookie domain. domain:.foobar.de }, { // Regex matching cookie name. name: /^_ga/, domain:.foobar.de }, { // Regex matching cookie name. name: /^__utm/, domain:.foobar.de }, { // Regex matching cookie name. name: /^_g/, domain:.foobar.de } ], language: { locale: { de: { name: 'Google Analytics' } } } }

vikramshrowty commented 3 years ago

I don't think there is a direct solution for it. Once a third party cookie is set there is no way for a 1st party JS to access it. I think the best that can be done is to reload the page, which may be adequate

swigrid commented 2 years ago

you could wrap your google code to

if ( (document.cookie.match('(^|;)\\s*' + 'cconsent' + '\\s*=\\s*([^;]+)')?.pop() || '') != '' ) {}

It should prevent execute the code before cconsent cookies is saved, however I haven't tested it.

nklido commented 2 years ago

@tdrzewosz You could use ScriptTagFilter.

Add the google tag manager script with type="text/plain" and with the attribute data-consent

<script type="text/plain" data-consent="googletagmanager">
  // tag manager code here
</script>

Then add google tag manager as a service in CookieConsent config

services: {
  google_tag_manager: {
    category: 'marketing',
    type: 'script-tag',
    search: 'googletagmanager',
    language: {
    locale: {
        en: {
          name: 'Google tag manager'
        }
      }
    }
  }
}

Now the google tag manager script won't run unless marketing cookies are accepted