Google Analytics and WordPress

WordPress is fantastic. It’s grown well past it’s infancy of a simple blogging software into being capable of acting as a full fledged Content Management System.

Google Analytics is equally awesome. It’s an excellent way of tracking visitor flow throughout your website, identifying how people are arriving and why they leave and so much more. Best of all? They’re both free.

One potential downside of Google Analytics, however, is that it shows ALL traffic. That includes you and any other people that may be involved in editing your blog or business’s website! It does allow you to filter out by IP addresses, but that only works if you have static IP addresses which most people (especially if you aren’t a business) won’t have.

Thankfully if you’re using WordPress, there’s a simple fix! Traditionally you would install Google Analytics by creating your account with Google, setting up the site in their panel and then copying and pasting code that looks something like this into your footer:

<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', 'UA-XXXXXXXX-X', 'example.com');
ga('send', 'pageview');

</script>

Why the footer? Best practice right now says that all Javascript and other code functions should be placed as close to the bottom of the website as possible. This prevents the page from not appearing at all if scripts hang among other potential issues. Of course, you CAN place Javascript/jQuery references in the header or anywhere else on the page, but in most cases, it would be best if you didn’t.

So with WordPress, to avoid tracking anyone that is logged into your site (you, your editors, etc.) simple wrap the tracking script in a php ‘if’ statement:

<?php if ( !is_user_logged_in() ) {?>
<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', 'UA-XXXXXXXX-X', 'example.com');
  ga('send', 'pageview');

</script>        
<?php } ?>

The key here is the if statement. We’re using the

is_user_logged_in()

function provided by WordPress which does exactly what it sounds like…tests if the current user is logged in. If they are, it will not display the Google Analytics information and dilute your website traffic statistics. You can read more about it in the WordPress Codex.

This works great for our Google Analytics scenario, but what if we allow open registration on our website? In that case we’ll layer on another conditional statement using the user permissions test.

<?php if ( !is_user_logged_in() && !current_user_can( 'publish_posts' ) ) {?>
<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', 'UA-XXXXXXXX-X', 'example.com');
  ga('send', 'pageview');

</script>        
<?php } ?>

Clearly this is a little bulkier, but the basic principle remains the same. If the user is not logged AND the user can not publish a post, then display the tracking script!

Using conditional statements like these are what make WordPress so flexible and powerful. There are many other applications for the if statements used above, but refining your Google Analytics results is a good place to start. If you have any questions feel free to drop them in the comments below!

Leave a Reply

Your email address will not be published.