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).
Will work with GA4?
Is there a way to make this apply only to logged in administrators, while tracking other logged in users?
Line 5 would need to be changed to:
if ( !is_user_logged_in() || !current_user_can(‘administrator’)) {
Hello,
I tried this but it didn’t work. I added added this code using the Code Snippets plugin, adding wp_header instead of wp_footer. I set it to run across the entire site:
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXX’]); // 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);
})();
<?php
}
}
Have I done something wrong? Please help.
It looks like the Google Analytics id is missing. UA-XXXXXX is just a placeholder. You may want to check out: https://www.isitwp.com/install-google-analytics-wordpress-site/
Can I use this code instead?
My google analytics code.
<?php
}
}
Yes, you would use your own Google Analytics ID.
Thanks, It worked!
Glad it helped 🙂
Misleading title. Where is the part where you show how to block logged in users ?
Line 5 sets of the conditional to not run the Google Analytics code if the user is logged in.
Thanks this is very useful 🙂
I used to do it all with tag manager but this is a much faster option!
There is a new script now from Google so you may want to update your post. Also, I changed wp_footer to wp_header as Google prefers the script in the header.