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.

  • Seth

    Thanks! Just set this up and can confirm that my own traffic doesn’t show up in Real Time or Audience. Easy to set up across all my devices and I can just direct my non-tech-savvy business partner to the page with the cookie as well.

    • Andrew Miguelez

      That’s fantastic! I’m glad I could help.

      • Seth

        Hmm, looks like I spoke a little too soon though. I don’t show up in Real Time stats and the cookie definitely works because ga(‘set’, ‘dimension1’, ‘true’); shows up in the source code, but I think I appear under Audience stats.

        To test this out I set up a testpage which nobody can access and I have noticed some hits. Just turned on Exclude all hits from known bots and spiders; maybe that was the reason?

        • Andrew Miguelez

          Are you looking at the correct view? If you set up your filter on a different view, then the default view will see your traffic still.

          Also, don’t include the tracking code on your secret page so no views can store any data on that page (unless you want to).

          • Seth

            Yes I have set the filter on the view I am looking at. The annoying thing about GA is that it doesn’t seem to update very often, so it’s quite hard for me to troubleshoot it.

            Given that the cookie works in showing that extra line of code, I might just want to make it such that if the cookie is set, I’ll just exclude the line ga(‘send’, ‘pageview’); so that none of my views are sent in the first place. That’d work right?

          • Andrew Miguelez

            You can test immediately with the Real Time view. If you don’t show up there, you won’t be anywhere else. It works the same way as a simple IP address filter.

            I can confirm this works for other sections of the Analytics as well, because when I switch between “All” and “Real Users” views, there is a difference in number of sessions.

            If you don’t want to gather data, you shouldn’t include the entire script. You’re right that removing the send pageview line will work, but better safe than sorry.

          • Seth

            I definitely do not show up in Real Time view. The curious thing is that I set up and visited a test page which no one else knows about except myself, and the hits on this page are being recorded.

            When I go to Audience > Custom > User Defined, I see 72 sessions of (not set). When I view Secondary Dimension: User is Admin, there is no hits recorded.

            I checked the filter again and again and it seems fine. Guess I should just try to exclude the entire script if the cookie is set.

          • Andrew Miguelez

            You SHOULD see all sessions set to (not set). That means 72 sessions who were not “User is Admin”. And there will be no records for “User is Admin” in your filtered view. So that is also a desired output.

            If the only thing being recorded is your special page, then either remove the tracking code from that page (because it’s pointless to be there), or make sure you’re setting the dimension on that page like you are the rest of your site.

            If you include the tracking code on your special page, you can expect the first hit of that page before the cookie has been set to be tracked.

      • Seth

        Oh, I went to a page with ?=randomstringofwords and that showed up too, so I don’t think it was robots.

  • Cookie method worked like a charm. Thanks for sharing, very useful.

  • This is cool, thanks for the tip. I modified your solution slightly. Instead of having “user_is_admin” flag with only true/false, I use “user_class” and set it to “admin” instead. This gives me a bit of flexibility in case I want to play with this dimension in the future.

    • Andrew Miguelez

      That is a fantastic idea! Thanks for sharing that improvement to my method, and I’m glad you found this article helpful.

  • Darakhshan Saeed

    I’m not clear on what you mean by “Open up a file that is only loaded when you’re logged into your admin.” I have a WordPress website. Can you explain what you mean by this? I understand the method of creating a hidden page and putting the code in the head of that page. But is there another file that I can use that only loads when I’m logged into WordPress admin? Thanks for your help.

    • Andrew Miguelez

      Good question. I could have explained that better.

      What I meant was, find a file that is loaded when you’re in the admin area of your site. This could be the file that builds the header or the main index file. You just want the cookie code to be set before the HTML tag is opened.

      For WordPress, I’d recommend setting the cookie in your theme’s functions.php file. That way when WP is updated, you won’t lose your code. You’ll need to wrap it in a conditional that checks that you’re on an admin page:

      if( is_admin ) { …cookie code… }

      • Karlo Karlo

        I am still confused, where should I look for that file? I have linked youtube channel with my google analytics

        • Andrew Miguelez

          You can’t do this with a YouTube channel. YouTube doesn’t give you any code access so there’s no place to include the script. You can do this on your own website though.

  • As a Google Tag Manager user, how would I go about Step #4? I’m not sure if I can edit the GA code from GTM…

    • Andrew Miguelez

      I don’t use Google Tag Manager for performance reasons. So, unfortunately, I can offer no advice on that topic. Sorry.

  • Susan Rawlings

    Hi,

    Does your method work only if there is a login required for the page? I am trying to parse what type of page to create here? Can it just be a standard page on the site that users are directed to? Or do we need to set up a login? Thanks!

    • Andrew Miguelez

      If you’re creating a secret page for only admins to hit and be excluded, then yes, you can create any page you like. Make sure no true users can ever find that secret page though. Otherwise your analytics will be severely skewed.

      • Susan Rawlings

        does there have to be a login required?

  • Graham O’Shea

    Excellent article. Works great since we have a dynamic IP in the seopler.com office

    • Andrew Miguelez

      That is the perfect application for this method. I’m glad it was useful for you.

  • Kristina

    After testing numerous methods, this one works like a charm. Thank you!

  • Andrew Jonathan

    could you please write an article on how to do this specifically with WordPress and Google Tag Manager ?

    • Andrew Miguelez

      Hey, thanks for your interest. I am planning an article on implementing this in WordPress, as well as an alternative to setting a cookie at all. However, I have no experience with Google Tag Manager. As that is a great tool for adding specific tracking when you don’t have code access, I’ve never needed it as a developer. Hopefully you’ll be able to apply my upcoming tutorial to your needs, but my alternative solution is even easier and will definitely work with Google Tag Manager!

      Bookmark this page and check back for an update or follow me on Twitter to be notified of upcoming articles.

  • Pingback: It's Been a Year — AndrewMiguelez.com()

  • Pingback: Exclude WordPress Admins from Google Analytics — AndrewMiguelez.com()

  • For people trying this in ruby;

    Add to a controller action that triggers when you visit your dashboard;


    cookies['is_admin?'] ||= {
    value: true,
    expires: 10.years.from_now
    }
    Add to your google analytics script tag;

    ga('set','dimension1','true');

    • Andrew Miguelez

      Thank you for contributing this Ruby version! Much appreciated.

  • Thanks for the great article!