Cookie warning script

My friend developer coded a simple script for me to disable Google Analytics with Cookie Consent by Insites.
Here is a simple solution:

  1. Go to your Project Settings, Custom Code tab

  2. Place this in the Head Code
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />

  3. Place this in the Footer Code and make sure to replace GOOGLE_ANALYTICS_ID with the proper ID (find your ID)
    If you have this ID placed in the “Integrations” tab in your Project Settings, remove it from there.

     <script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
     <script>
         var enableGA = function() {
           (function(i, s, o, g, r, a, m) {
               i['GoogleAnalyticsObject'] = r;
               i[r] = i[r] || function() {
                   (i[r].q = i[r].q || []).push(arguments)
               }, i[r].l = 1 * new Date();
               a = s.createElement(o), m = s.getElementsByTagName(o)[0];
               a.async = 1;
               a.src = g;
               m.parentNode.insertBefore(a, m)
           })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
           ga('create', 'GOOGLE_ANALYTICS_ID', 'auto');
           ga('send', 'pageview');
         };
         window.addEventListener("load", function() {
             window.cookieconsent.initialise({
                 "palette": {
                     "popup": {
                         "background": "#EEEEEE"
                     },
                     "button": {
                         "background": "#F89406",
                         "text": "#ffffff"
                     }
                  },
                 revokable:true,
                 "theme": "classic",
                 "type": "opt-in",
                 "content": {
                   "message": "We use cookies to provide you best experience on our website. We also use Google analytics for our internal statistics. You may choose to disable analytics if you prefer. ",
                   "dismiss": "Disable analytics",
                   "allow": "Got It!",
                   "link": "Learn More",
                   "href": "http://dsign.site/privacy-policy"
                   },
               
                 onInitialise: function (status) {
                   var type = this.options.type;
                   var didConsent = this.hasConsented();
                   if (type == 'opt-in' && didConsent) {
                     // enable cookies
                     enableGA();
                   }
                   if (type == 'opt-out' && !didConsent) {
                     // disable cookies
                     // NOTHING TO DO
                   }
                 },
                 onStatusChange: function(status, chosenBefore) {
                   var type = this.options.type;
                   var didConsent = this.hasConsented();
                   if (type == 'opt-in' && didConsent) {
                     // enable cookies
                     enableGA();
                   }
                   if (type == 'opt-out' && !didConsent) {
                     // disable cookies
                     // NOTHING TO DO
                   }
                 },
                 onRevokeChoice: function() {
                   var type = this.options.type;
                   if (type == 'opt-in') {
                     // disable cookies
                     // NOTHING TO DO
                   }
                   if (type == 'opt-out') {
                     // enable cookies
                     enableGA();
                   }
                 }
             }, function (popup) {
                 $('#revoke-cc-btn').on('click', function() {
                     popup.revokeChoice();
                 });
             })
         });
     </script>
    

This script also includes a function to revoke consents.
If you wish to do so simply add a button on your page and give it an ID: revoke-cc-btn

You can see a demo here https://www.dsign.site/cookie-consent

I’ve tested this by checking what cookies are stored in the browser’s inspector and also simply by going to Real-Time reports in Google Analytics.

This solution works just fine for me.

If anyone is interested I will share a more complex code which will allow you to include several functions, not only google analytics. For instance if you would want to disable google maps or iframe embed additionally to analytics.

Good luck!

6 Likes