EU Cookie Law Free Script - Disable Google Analytic - GDPR






EU Cookie Law Free Script - How to disable Google Analytic?


I decided to post this script because I really struggled finding a EU Cookie Law Free script that works with an Opt-in navbar that let the user activate Google Analytics.
The original script comes from https://cookieconsent.insites.com but I have seen many people struggling with it, this is a working solution.

By using this script, your user will have to click on "Accept" to activate the Google analytics on your website.
If the user clicks on "Dismiss", Google analytics tracking will be disabled. If nothing if clicked, Google analytics will be disabled as default.

Do not forget to create a Google analytic account.

First step:

Add this above your </header>

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx-x"></script>

Replace the xxxxxx-x with your Google analytics UA id

Just below the above code add the below code:

<script>
window.addEventListener("load", function(){

window.cookieconsent.initialise({
    "palette": {
    "popup": {
      "background": "#ffffff"
    },
    "button": {
      "background": "#f1d600"
    }
  },
  "theme": "classic",
content: {
  header: 'Cookies used on the website',
  message: 'This website uses cookies to ensure you get the best experience on our website.',
  dismiss: 'Decline',
  allow: 'Accept',
  link: 'Learn more',
  href: 'https://YourWebsite.com/privacy-policy',
  close: '&#x274c;'
}, 
revokable: true,
animateRevokable: true,
  "type": "opt-in",
cookie: {expiryDays: 1},
onInitialise: function (status) {
  var type = this.options.type;
  var didConsent = this.hasConsented();
  if (status=='allow') {
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    window['ga-disable-UA-xxxxxx-x'] = false;
    gtag('js', new Date());
    gtag('config', 'UA-xxxxxx-x', { 'anonymize_ip': true });
  }
  if (status=='dismiss') {
   
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    window['ga-disable-UA-xxxxxx-x'] = true;
    gtag('js', new Date());
    gtag('config', 'UA-xxxxxx-x', { 'anonymize_ip': true });
  }
},
 
onRevokeChoice: function() {
  var type = this.options.type;
  if (status=='allow') {
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    window['ga-disable-UA-xxxxxx-x'] = false;
    gtag('js', new Date());
    gtag('config', 'UA-xxxxxx-x', { 'anonymize_ip': true });
  }
  if (status=='dismiss') {
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    window['ga-disable-UA-xxxxxx-x'] = true;
    gtag('js', new Date());
    gtag('config', 'UA-xxxxxx-x', { 'anonymize_ip': true });
  }
},
})
});
</script>


Things that you need to set:

cookie: {expiryDays: 1},
Change the number of days to whatever you like.



window['ga-disable-UA-xxxxxx-x']
Replace this to your own Google analytic id



 href: 'https://YourWebsite.com/privacy-policy',
Add the link of your own privacy policy


If this helped you please leave us a message!