Disable Google Analytics tracking for logged in users

Google Analytics tracking counts the visit,clicks,time on site and other parameters of logged in users.It is required to disable Google Analytics tracking for logged in users to get accurate information.Webmasters often preview their posts before publishing them.After making any changes to website webmasters browse their website to check whether the change is working or not.While you are traversing your website Analytics is logs the data.This impacts overall metrics and you get inaccurate information.

Now to disable Google Analytics tracking for WordPress logged in users follow below steps

  • Log into WordPress dashboard
  • Open your theme header.php file.You can do so from Appearance –> Editor and choosing header.php as file to edit
  • If you have already placed your Google Analytics code then it is fine else you can get your analytics code.
  • Open notepad (if you are using Windows) or Text editor (gedit) if using Linux.I do not know about other operating system text editors.Feel free to open any text editor and paste the code.
  • Add
    <?php if ( is_user_logged_in() ) { }else { ?>

    before the starting line of the code.This code checks whether the user is logged in or not.It is standard WordPress function.

  • Add
    <?php } ?> 

    after the last line of code.

  • Locate </head> tag in your theme’s header.php file
  • Paste the code before the </head> tag.
  • So your final analytics tracking code becomes as below
<?php if ( is_user_logged_in() ) { }else { ?>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<?php } ?>
</head>
  • Save the file and you are done.
  • You can check whether the above code works by visiting the site and checking your site in Real Time option of Google Analytics.

Leave a Comment