Wordpress + Google Analytics: skipping administrator pageviews

Being the stat nerd that I am, I love to track visit to this blog (and other websites) with Google Analytics.

Problem is that I’ve noticed that also my visits couts, i don’t want this, at least, if i’m logged as an administrator i open the blog pages lots of times, to check the comments, to edit the layout of the pages and so on.

Edit: Marco from goanalytics.info suggested a smarter way to do that. See comment #1. ;-)

So after some researches, i’ve found the solution: you can filter Analytics pageviews basing on IP or cookies. The first one is useful when you don’t want a renge of users in a lan, but since I’m a freelance and work from home (but also from my parent’s home, and sometimes i open the blog at a friend house) the IP solution doesn’t work.

Cookies.

The Idea is to modify the wordpress template, so that can insert a cookie if you are logged in as the administrator. Wordpress already sets up some cookies for you, but using these will filter out every wordpress user, we just want to filter out ourselves.

To find if the user is an administrator, a good tip come from the Mark on Wordpress blog, you have to check user capabilities.

Next, reading GA Help tells you how to filter results based on a user defined keyword, you need to add a js function to the body onload event (assuming you don’t have any other, if it is the case, just add after the ;

Mixing the two things, something like this should came out:

<?php if ( current_user_can('manage_options') ) {
    $onload = "onLoad='javascript:pageTracker._setVar(\"custom_keyword\");'";
} ?>
<body <?php echo $onload; ?> ></body>

This should replace your body tag, so should go in the header.php (in my template, at least); remember to replace custom keyword with something unique, a md5 of something should work.

Notice that this javascript uses a function from google, so you have to include the GA js library in the head. But Analytics tells you to include the code at the bottom.. You can choose. It will work fine if you just paste the GA code in the head, or else you can split it (in that code, there are 2 <script> blocks, the first loads the .js from Google, the seconds tracks the visits), so that the first script block goes into the head, and the latter at the bottom of the page.

Next you have to setup a filter in GA. Easy easy, goto Analytics Setting (the firse page after the login), find the Filter Manager link, add a new filter,with these options

Filter Type: Custom filter > Exclude
Filter Field: User Defined
Filter Pattern:custom_keyword
Case Sensitive: No

Enjoy. ;-)

References:

Mark on Wordpress: How to check if a WordPress user is an “administrator”

Google Analytics Help: How do I exclude my internal traffic from reports?, How do i create a custom filter

3 Responses

  1. Marco Cilia:

    a little complicated, don’t you think? :)

    why not a simple IF administrator nothing ELSE place GA tracking code?

    otherwise Google Analyticator plugin by cavemonkey automagically exclude admin view ;)

  2. kajyr:

    Yeah, quite right. Early in the morning i tend to be a little complicated.
    I’ll edit.

  3. Eli:

    You want to include analytics code even when an admin is logged in so that you can use the site overlay feature of Google Analytics. It’s far better to exclude by cookie content as suggested here.

Do you want to leave a comment?