X

How to Disable Google Analytics for Logged in Users in WordPress

Snippets by IsItWP

Are you using Google Analytics for tracking your audience? Then, you probably only want to track real visitors, and not your authors and writers.

The easiest way to track real visitors is by using MonsterInsights on your site, which is the best Google Analytics plugin for WordPress.

If you’d rather use a code snippet, then follow the below instructions. Keep in mind that 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.

Instructions:

Place this snippet in your functions.php file located in your theme’s directory or in a site-specific plugin. You have to insert your Google Analytics ID at “XXXXXXXX“. Have fun with it!

<?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
   }
}
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!