Stop Google Analytics From WordPress User Admin

Posted by: Alex on June 19, 2013

As a WordPress admin/blogger, you will no doubt spend a lot of time browsing throughout your own site. If you are using Google Analytics this can create false results, and although you can configure Google Analytics to ignore certain cookies/IPs etc, I find it easier to just not load the Google Analytics script for Admins/Editors.

To hide Google Analytics from WordPress Editors, use the following:

<?php
if (!is_user_logged_in() ) {
  echo "<script>
  (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','//www.google-analytics.com/analytics.js','ga');

  ga('create', '# Change to your Google UA Code #', '# Change to your Domain #');
  ga('send', 'pageview');
  </script>";
}
?>

Just ensure to change the line with comments on so that the Analytics snippet has your UA and URL in it.