Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How block google analytics by google tag manager? #75

Open
tdrzewosz opened this issue Aug 20, 2021 · 3 comments
Open

How block google analytics by google tag manager? #75

tdrzewosz opened this issue Aug 20, 2021 · 3 comments

Comments

@tdrzewosz
Copy link

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
Copy link

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
Copy link

swigrid commented Feb 22, 2022

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
Copy link

nklido commented Feb 23, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants