brainsum/cookieconsent

How block google analytics by google tag manager?

Opened this issue · 3 comments

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' } } } }

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

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.

@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