WordPress and Google Analytics

The Problem: I, like many others, manage WordPress sites that I monitor with Google Analytics–which is an awesome tool.  Sometimes, however, I like to visit my site and see how content looks live.  Because I do a bunch of work on the go, it isn’t always conducive to filter out all the different IP addresses, so sometimes my visits end up getting counted and throwing my statistics off.  This could also work if you have multiple bloggers and don’t want their visits to get confused with the overall traffic of the site.

The Tools:

  • A text editor.  I use Notepad ++, but you can use whatever you like.
  • Your Google Analytics Code
  • Access to your WordPress theme

The Solution:

The solution is fairly simple, though it does require two minor adjustments on the part of the user.

First off, it will require that you login as an administrator prior to viewing your live site. This might take a little bit of getting used to, but what I’ve found is helpful is updating any bookmarks on my browser to go directly to the login page–just as a helpful reminder.

Also, some WordPress themes may allow you to insert Google Analytics code via their option panel.  While this might be helpful in most cases, this particular workaround will not function correctly without further editing of the theme.

For starters, I recommend first creating a backup copy of your site files.  Though this process should not mess with anything else, it helps to have a backup copy handy, just in case of something weird.  Also, it might help to edit your theme offline and then upload it, so while I know that WordPress does have a feature to edit code, I prefer to test offline prior to uploading.

Open header.php and locate the </head> tag.  Found it? Great! Now, right above it we’re going to add the following lines:
< ?php global $user_ID; if (!current_user_can('administrator')){?>
< ?php } ?>

What this does:

  • Sets the $user_ID variable to global. This allows us to use the built in user functions.
  • The script then checks to see if the user is not an administrator using the “current_user_can” function.

Right now, this code doesn’t alter anything.  That’s about to change!  Copy your Google Analytics code and plug it in between right after the first ?>

What you have now (depending on your Google Analytics code) should look something like:

< ?php global $user_ID; if (!current_user_can('administrator')){?>
<script type="text/javascript" src=" http://www.google-analytics.com/urchin.js "></script>

<script type="text/javascript">
_uacct = "UA-xxxxxx-x"
urchinTracker();
</script>
<?php } ?>

</head>

This should eliminate any logged in administrative users from showing up on Google Analytics as the script will only run if the user is not an administrator!

Alternatively, you can check if the user is logged in, and run it only if the user is not logged in. This is done by replacing if (!current_user_can('administrator')) with if (!$user_ID). (In case you hadn’t guessed, in PHP ! means “not”.)

Let me know, as always, if this works out for you, or if ! how I can help.

2 replies
  1. Tom Strike
    Tom Strike says:

    Thanks for this post.

    I am now designing with a similar CMS. (Concrete5). Your post will also apply to my needs. I have a Google analytics account set up and have also given this concern some thought I will be applying this technique to my web sites.

Comments are closed.