简体   繁体   中英

How to disable google tag manager and google analytics

Hi I used following code for disable google analytics and google tag manager but its not worked.

<script>
var gaoop_property    = 'UA-xxxxxxxx-x';
var gaoop_disable_str = 'ga-disable-' + gaoop_property;
function gtmOptOut() {
    document.cookie = gaoop_disable_str + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';       
    window[gaoop_disable_str] = true;        
    window.location.reload();
}
</script>

Does anyone know how I can fix this? Thank you.

<script>
   var gaoop_property = 'UA-XXXXXXXX-X';
   var gaoop_disable_str = 'ga-disable-' + gaoop_property;
   console.log(gaoop_disable_str + '=true');
   if (document.cookie.indexOf(gaoop_disable_str + '=true') > -1) {
       window[gaoop_disable_str] = true;
   }

   function gtmOptOut() {
       delete_cookie("_ga");
       delete_cookie("_gat_gtag_UA_43169816_3");
       delete_cookie("_gid");
       document.cookie = gaoop_disable_str + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
       window[gaoop_disable_str] = true;
       window.location.reload();
   }

   function delete_cookie(name) {
       var expires = new Date(0).toUTCString();
       var domain = location.hostname.replace(/^www\./i, "");
       document.cookie = name + "=; expires=" + expires + "; path=/; domain=." + domain;
   }
</script>

Add this script and Add Below Condition in the Header

<?php if(empty($_COOKIE['ga-disable-UA-43169816-3'])){ ?>
//google analytics script
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM