WordPress & Google Analytics Tip

One thing that always used to bug me when I was surf­ing my own sites was that I was inflat­ing my site sta­tis­tics. This is espe­cial­ly the case when I go through a bunch of posts to check for fac­tu­al and gram­mat­i­cal issues. I’m inter­est­ed in know­ing when oth­er peo­ple are access­ing my old posts, but I don’t my ana­lyt­ics pack­age to mis­take me for them! So, I hacked togeth­er a bit of Word­Press solu­tion that will ensure that I don’t get served the Google Ana­lyt­ics track­ing script when I view the pages. Here’s the code: expla­na­tion to follow.

<?php
if ( !is_user_logged_in() && (get_bloginfo('home') == "http://yourblogurl.com") ) { ?>
 
Google Analytics code goes here.
 
<?php } ?>

The if() state­ment is what dri­ves this code, check­ing first­ly that the per­son view­ing the page is not a logged-in user. Then it adds a sec­ond line of defense against serv­ing the ana­lyt­ics code when I’m devel­op­ing on my local machine. It ensures that the page is being served by your pro­duc­tion serv­er (fill in what­ev­er your blog’s home url hap­pens to be) so that your local devel­op­ment also does­n’t inflate your pageviews.

Of course, this would work with any ana­lyt­ics code. Just insert this snip­pet wher­ev­er the track­ing code is sup­posed to go, and you should be all set!

One Comment

  1. Posted October 3, 2010 at 8:33 pm | Permalink

    Sweet tip! I was won­der­ing how to do that.