The Best Way to Exclude Yourself From Google Analytics Data

Update: November 2016
I’ve published a follow-up to this article on how to exclude yourself from Google Analytics on WordPress specifically.

Alternate titles:

  • Better Way to Exclude Internal Traffic
  • Cookies vs. IP Addresses, The Showdown
  • Non-destructive Google Analytics Filtering

The What and Why

We all use analytics data to monitor our site performance and track visits, or at least we should. The most popular option is Google Analytics. It’s free, well supported, and extremely customizable.

We also visit our own sites quite often. Especially when we’re proof-reading a blog post or testing new features, we can bump up the stats quite a bit. When those of us with very little traffic do this, our pageviews could be swayed by as much as double. This isn’t good.

If we’re to make decisions based on our Google Analytics data, we need it to be accurate data from our real users.

How can we keep our own visits from affecting the stats?

We can make an effort to never load our own site – yeah right. A more realistic way is to filter out our own visits from the data. That’s what I do.

There’s more than one option to choose from. Let’s compare them…

Approaches

Note: The following solutions all reference the new Universal Analytics (analytics.js). If you’re using the old analytics (ga.js), you should upgrade.

IP Address Filtering

This option is the one Google Analytics recommends. You’ll even receive notifications in your analytics account if your views aren’t filtered with an IP address.

All you need to do is find out what IP address(es) your network(s) lives in and filter them out. You can even filter based on a range of IP addresses. This option is simple and effective, but heavy handed.

  • This works because:
    • You and your employees don’t have to do anything to be filtered while under the known IP address(es).
    • All browsers will be filtered without any special steps.
  • This doesn’t work because:
    • It most likely requires a static IP address.
    • A mobile device that switches to a mobile data network will not be filtered.
    • An employee working from home (or on the road) isn’t filtered easily.
    • It filters everyone under that IP, even if you only want to filter specific users.

Special Page Filtering

Some people have been using this option for a while now. They place their analytics tracking code on a special page that only they know about and set a custom variable. Then when they visit that page, Google Analytics associates them with that variable. From then on, they can be filtered out of their data.

  • This works because:
    • It doesn’t require a static IP address.
    • Mobile devices, even when changing networks, can easily be filtered.
    • Employees working from home (or on the road), can easily be filtered.
    • Filtering of specific users is possible by having only those users visit the special page.
  • This doesn’t work because:
    • All users that you want filtered need to know and visit the special page, on each device/browser they use to access the site. This is a tall order, especially for a large operation.
    • Data about the special page will be tracked on any non-filtered views.
    • The new Universal Analytics replaces custom variables with dimensions. From my testing, setting a dimension’s scope to user and filtering based on the dimension’s value doesn’t appear to persist (though the documentation states that it should). This is probably user error (so set me straight in the comments).

Harder, Better, Faster, Cookie-er

I liked the special page option, but wanted to almost automate it. Making it less of an extra step for you and your employees means it may not be something you even need to discuss. Instead it could be something that happens whenever a user logs into the admin area.

I also wanted to eliminate the tracking code on a special page or in my admin. I keep an unfiltered view in my Analytics account along with my filtered views (which is a good practice). I don’t want data from my special page or admin to show up in that view. I figured I could use a cookie to bridge the gap.

So what’s my improved method? First we set a cookie when a user enters the admin area. We’re identifying these users as worthy of being filtered without relying on them visiting a special page. Now, whenever we include the tracking code on the front end of the site, we also include a custom dimension if the cookie exists. We can filter based on this dimension and for most users, no special step was required.

  • This works because:
    • All of the special page option’s positives apply here too.
    • No one that does admin tasks (heaviest traffic) will need to remember to visit the special page.
    • If someone uses a public computer to temporarily access the admin area, that computer will stop being filtered as soon as the cookie expires.
    • You can also clear the browser’s cookies to stop filtering its traffic.
    • Whether you use a special page or the admin, the tracking code isn’t placed anywhere but the front-end, so you won’t have to worry about that data in your unfiltered views.
  • This doesn’t work because:
    • It requires each browser to log-in to the admin at least once to be filtered. (Slightly more annoying than visiting a special page.)

How Do We Do It?

Let’s apply the special cookie-based filter. It’s not difficult and you won’t have to change much of the following code if you’re using PHP.

Note: I recommend you consider using the IP address option as a backup. If your IP address is fixed, or your office(s) have a set IP range that you can filter, you should probably do that first.

1. Create a Dimension in Google Analytics

screenshot of adding a dimension in Google Analytics

These instructions are pulled from here.

  1. Select the Admin tab and navigate to the property to which you want to add custom dimensions.
  2. In the PROPERTY column, click Custom Definitions, then click Custom Dimensions.
  3. Click New Custom Dimension.
  4. Add a Name.
  5. Select the Scope. I recommend User, since that’s what we’re filtering.
  6. Check the Active box.
  7. Click Create.

2. Create a Filter in Google Analytics

We want to filter our special users, but it’s a good practice to keep an unfiltered view as well. I recommend you add a new view and apply the filter to it. If you do this, your default view will be untouched and include all possible data.

screenshot of adding a filter in Google Analytics

These instructions are pulled from here.

  1. Select the Admin tab and navigate to the view in which you want to create the filter.
  2. In the VIEW column, click Filters.
  3. Click + New Filter.
  4. Select Create new Filter.
  5. Enter a name for the filter.
  6. Select Custom filter and pick the Exclude option.
  7. For Filter Field, select your dimension at the bottom of the select box.
  8. For Filter Pattern, enter true.
  9. Click Save.

Now your Analytics view is ready to filter based on your custom dimension.

3. Set a Cookie for Any Access to the Admin / Special Page

Open up a file that is only loaded when you’re logged into your admin. Preferably this file is called before any HTML is output so that it will work with cookies regardless of your PHP version. If using a different scripting language, your results may vary. Alternatively, you may create a special page like yourdomain.com/shh-its-a-secret.php or something slightly less ridiculous.

At the top of this file, add the following code:

<?php
    $expire_time = time() + 60 * 60 * 24 * 180;
    setcookie( 'user_is_admin', 'true', $expire_time, '/' );
?>

Change the math in your $expire_time variable to set the time your cookie should expire in seconds from the current time. Here I’ve set it to 180 days worth of seconds past the current time. Remember, when the cookie expires, you won’t be filtered again until you access your admin/special page. So set this according to your needs.

The setcookie() function in PHP takes a bunch of optional arguments but we need to set four of them. The first is the name of your cookie (pick anything). The second is the value for the cookie (not important but set it to something). The third is when the cookie should expire. And fourth we set to '/' because we want the cookie to apply to our entire domain, not just the current folder.

4. Set the Dimension When the Cookie Exists

Find where you’re outputting your Google Analytics tracking code on your front-end. Insert the following directly above the ga('send', 'pageview'); line:

<?php
if( isset( $_COOKIE['user_is_admin'] ) ) {
    echo 'ga(\'set\', \'dimension1\', \'true\');';
}
?>

We’re checking to see if the cookie is set (and not expired). So change user_is_admin to whatever you named your cookie, dimension1 to whatever dimension index Google gave you when you created your dimension, and true to whatever your filter pattern is set to.

When you’ve added this to your tracking code, it will look like this:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXX-X', 'auto');
    <?php
    if( isset( $_COOKIE['user_is_admin'] ) ) {
        echo 'ga(\'set\', \'dimension1\', \'true\');';
    }
    ?>
    ga('send', 'pageview');
</script>

This sets a value our filter is expecting to our dimension and then sends that data along with the standard pageview. So when the cookie exists, we are a special user, and when it doesn’t, we are a regular user.

Final Thoughts

These instructions are generic and can be applied to any CMS or custom site architecture. If you need help with your specific application, let me know in the comments. For WordPress and Anchor CMS, I already know how to apply this solution, but I’m sure I can assist with most other setups.

Like I said earlier in the article, I think IP address filtering is a great idea if it can work for you. In the case where you have some employees who are admins and some who aren’t, you might want to track the non-admins as organic traffic. Then you’ll want to avoid IP address filtering and stick to the cookie / special page approach.

If you have suggestions for improvements to the methods described in this article, please share! I would love to know how I can filter my Google Analytics data even better.