One thing that always used to bug me when I was surfing my own sites was that I was inflating my site statistics. This is especially the case when I go through a bunch of posts to check for factual and grammatical issues. I’m interested in knowing when other people are accessing my old posts, but I don’t my analytics package to mistake me for them! So, I hacked together a bit of WordPress solution that will ensure that I don’t get served the Google Analytics tracking script when I view the pages. Here’s the code: explanation to follow.
<?php
if ( !is_user_logged_in() && (get_bloginfo('home') == "http://yourblogurl.com") ) { ?>
Google Analytics code goes here.
<?php } ?>
The if()
statement is what drives this code, checking firstly that the person viewing the page is not a logged-in user. Then it adds a second line of defense against serving the analytics code when I’m developing on my local machine. It ensures that the page is being served by your production server (fill in whatever your blog’s home url happens to be) so that your local development also doesn’t inflate your pageviews.
Of course, this would work with any analytics code. Just insert this snippet wherever the tracking code is supposed to go, and you should be all set!
One Comment
Sweet tip! I was wondering how to do that.