WordPress: Excluding yourself from Google Analytics reports

I hate it when I'm working hard on a site, going from page to page, and my clicks and such distort the Google Analytics reports. The clicks and page views themselves don't do a whole lot but when you look at average time on site and average page views per visitor it will be completely out of whack. We need to exclude ourselves from the reports and we'll use WordPress as an example.

Excluding via IP address can be a pain as your IP address will probably change every once in awhile. We'll go ahead and create a Google Analytics filter to allow us to exclude by cookie instead. This will require two steps. First we'll have to create the filter in Google Analytics and second, we'll have to add code to the WordPress theme to set the cookie.

To add the filter, log into Google Analytics and make sure you're on the "Analytics Settings" page. At the very bottom of the page you should see a link near the right that says "Filter Manager". Go there...

In the Filter Manager screen you will need to click "Add Filter". Enter a name for the new filter. Under "Filter Type:" we need to select "Custom" and then "Exclude". "Filter Field" should be user defined and "Filter Pattern" should be the name of the cookie. In this case we're using "test_value". Make sure the name you use is alphanumeric, void of spaces and any special characters. Near the bottom of this screen you will need to add the filter to the site profiles you want to use it on and then save the new filter.

At this point, any time a user browses the sites you selected with the test_value set by Google Analytics they will be filtered from the reports. Now we need to actually set the cookie though. For this particular post we are going to set the cookie dynamically in a WordPress blog depending on the user name and a couple other possibilities.

Warning: This is the tricky part for some people. I'm going to assume you're familiar with editing the files in your WordPress theme. If you know how but aren't entirely comfortable then you should probably back everything up before trying this. You could do this in a million different ways actually so this post is more of a soft push in the right direction than anything.

You need to open your theme's header.php file in an editor. Find the opening body tag and add some JavaScript...

<?php  global $current_user; ?>
<body <?php if($current_user->data->user_login == 'myusername') echo 'onLoad="javascript:pageTracker._setVar(\'test_value\');"'; ?>>

This checks if the current user's username is "myusername". If it is then Google Analytics pageTracker is called to set out cookie. In this example the user "myusername" will be excluded from the Google Analytics reports.

Put whatever you want in the if statement to qualify the cookie. Be aware that once the cookie is set it will need deleted or it will need to expire before you can get through the filter again.

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
*