Disable Google Analytics for logged in users

In case you’re using Google Analytics for website metrics, you probably only want to track real visitors, and not authors and writers.

This snippet automatically adds the Google Analytics code to your footer area (in case “wp_footer()” is called), so you don’t have to manually insert the Analytics code to all your files.

Place this snippet in your functions.php file located in your theme’s directory. If there’s no functions.php, create one. You have to insert your Google Analytics id at “XXXXXXXX“. Have fun with it!

Code Snippet:

<?php
// function for inserting Google Analytics into the wp_head
add_action('wp_footer', 'ga');
function ga() {
   if ( !is_user_logged_in() ) { // not for logged in users
?>
	<script type="text/javascript">
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-XXXXXXXX']); // insert your Google Analytics id here
	  _gaq.push(['_trackPageview']);
	  _gaq.push(['_trackPageLoadTime']);
	  (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
   }
}
?>

Posted in Conditional