How to filter your own visits from Google Analytics

FilterInternalVisitsfromGoogleAnalytics

One of the most important factors when using Google Analytics is to get your stats as accurate as possible.

One way to improve this accuracy is to ensure that any visits you or your development teams make to sites are filtered out from the results.

This guide is for the older, non-Universal Google Analytics.  We’ve got an updated guide for Universal Google Analytics.

Google Analytics provide some nice filtering tools for such work but unless you’re all nicely behind a single static IP address, learning how to prevent the results from being skewed can be hard work.

Even if your work place uses a static IP address, what if your team want to work on the site from other locations such as home or satellite offices?  The answer is cookies!

Essentially what we are going to look at in this tutorial is how to make a URL that you and your team can visit before working on the site to ensure you’re visit isn’t logged.  Although you will have to use a browser that has cookies enabled this process is much tidier than trying to list all the IP addresses or domain names that your development traffic will come from.

In simple terms, we need to do the following:

  1. Create a subfolder on your site that you will use to set the masking cookie.
  2. Decide on a key phrase you wish to set and check for.
  3. Create a default web document (probably index.html) inside this folder and configure it to set the masking cookie and then redirect back to the site root.
  4. Configure Google Analytics to filter both the key phase from the cookie and the subfolder you are using to set it

So let’s get started

1) Create a subfolder on your site that you will use to set the masking cookie.

This is the URL that you and your development team will access before opening the site.  Ideally it needs to be short, meaningful  and unique so that visitors won’t open it by mistake – if they do, their valuable tracking data will also be filtered out.

Something like http://www.sitename.com/mask would work well.

2) Decide on a key phrase you wish to set and check for.

This value goes into both the default web document you are about to write and in the Google Analytics settings.  Although the word doesn’t need to be meaningful, it would be best if it was human readable and contain no spaces or special characters.

Something like mask_visit would work well.

3) Create a default web document (probably index.html) inside this folder and configure it to set the masking cookie and then redirect back to the site root.

This is the most complex part of the operation.  To write the document, you’ll need to place it in the sub folder you’ve already created and name it to whatever your webserver supports as the default document – ideally index.html.  As I’m sure you’re aware – if using the naming suggestions so far, this means that you can enter http://www.sitename.com/mask instead of http://www.sitename.com/mask/filename.html

Once the document is created, you should copy and paste the following text into it (double click to select all for copy/paste):

<html>
<head>
<title>Google Analytics Masking</title>*** Paste your Google Analytics tracking code here ***<META HTTP-EQUIV="refresh" CONTENT="1;URL=/">
</head>
<body onLoad="javascript:pageTracker._setVar('mask_visit');">
Cookie added - visit excluded from Google Analytics....
</body>
</html>

As you can see, this follows a very basic HTML layout with some simple additions.  Firstly, you will need to include your Analytics tracking code you use on all of your pages.  This ensures that when you set the variable / key phrase, it’s set properly and the Analytics system picks it up so it can be filtered effectively.  The additional line is what is used to set the ‘user defined’ variable.  This is where you put your key phrase.

The script runs as the page loads, it sets the cookie variable and it then redirects to the main site.  The timing for the redirection can be set to anything you choose, but I’d recommend nothing more than perhaps 3 seconds.

Once this is complete, you can test the script by pointing your browser at http://www.sitename.com/mask and ensure that you get the re-direct.

4) Configure Google Analytics to filter both the key phase from the cookie and the subfolder you are using to set it

The final step is to configure Google Analytics itself.  Log onto your analytics account, and carry out the following:

  1. Go to settings (cog in the top right of the screen)
  2. Select your Account Name
  3. Click Filters
  4. Click + New Filter
  5. Enter a name for the filter (something like Mask Visit)
  6. Select Custom Filter
  7. Select Exclude
  8. Choose User-Defined from the Filter Field dropdown (its right at the bottom)
  9. Enter your key phrase in the Filter Pattern field, ie mask_visit
  10. At this point you can select all websites you want to link the filter to.  For testing, I’d suggest just the one you’re working on, you can always link to more as/when you’ve got them ready.
  11. Click Save

That’s the Mask Cookie filter setup, now we need to exclude the subdirectory you and your dev team will be visiting to activate the masking or you will still get the initial visit logged.

  1. Repeat steps 1-4 above
  2. Enter a name for the filter (something like Mask Subfolder)
  3. With Pre-Defined Filter  selected, use the drop downs to ensure the filter is set to Exclude – traffic to the subdirectories – that are equal to
  4. In the Subdirectory field, enter the name of your chosen sub directory (ie /mask/) ensure the line finishes with a trailing /
  5. At this point you can select all websites you want to link the filter to.  For testing, I’d suggest just the one you’re working on, you can always link to more as/when you’ve got them ready.
  6. Click Save

Additional Sites

This approach can be used on multiple sites in your Google Analytics portfolio.  By logging into the filtering section for any of your sites you can chose to create new filters or select from existing – if you select from existing you will see the two filters you have just created and they can be linked to every site.

The only thing that needs to be altered is the Google Analytics tracking code you paste into your index.html file.

Hopefully, assuming everything is working as it should – you’ll notice that your own visits now don’t appear in the stats.

At the moment, the filtering doesn’t apply to real-time stats which is a shame as it would provide an easy way to test that all is working as expected.

I hope this article has been useful and if you have any questions or comments, please post them below.

Author: BigGeek

Wayne is a self-proclaimed geek, IT Professional, father of two and husband who's still much closer to 40 than 50. When he's not busy in an otherwise hectic life he takes time to blog on all things that warrant an online opinion. Founded in 2012, ePINIONATED has been a guilty pleasure for Wayne, giving him an online voice in a very noisy Interweb – some of which might even be useful, maybe.

We would love to hear from you...