Excluding Visitors from Google Analytics via Cookie

Normally when you are setting up Google Analytics (or any other web analytics program for that matter), you want to set it up so that it doesn’t count traffic from visitors using the site on behalf of the site owner. This may include employees of the company that owns the site, web developers or online marketers contracted by the site owner, etc.

Usually this is done quite simply by using a filter to exclude the IP addresses of the visitors in question. There is a twist, in that you need to use regular expressions (which in this case means using the ‘\’ in front of the ‘.’s in the IP addresses so they are not treated as wildcards), but the process is pretty well explained in the Google Analytics help topic called ‘How do I exclude my internal traffic from reports?’. Google even provides an IP range tool to help with getting the right regular expression to cover a range of IP addresses.

Of course, this process assumes that the visitors that you want to screen out are operating from fixed IP addresses. For people working from home, or even small businesses, this may be not be the case, as they may using IP addresses dynamically provided by their ISP. The way to address this is through use of a cookie that can be set upon visiting a page dedicated for this purpose and then filtering out visitors based on the presence of the cookie.

This solution is addressed at the bottom of Google’s ‘How do I exclude my internal traffic…’ page, but I thought it was worth elaborating on. The information below is derived from the Google page but with some enlightening information from Brian Clifton’s excellent book ‘Advanced Web Metrics with Google Analytics.’

There are two stages to the process:

  1. Set up a dedicated page that will set a cookie on the visitor’s browser using Google Analytics code.
  2. Set up an exclude filter in Google Analytics that will cause GA to ignore traffic from visitors carrying the cookie

Step 1: Set a Cookie

  1. Create a new blank html page.
  2. Add a snippet of javascript to the body tag as shown in example 1 below.
  3. Insert the usual GA tracking code for the site on the page between the <body></body> tags.
  4. Save the page on your server with a name like ‘ga-exclude.html’.

Example 1: <body onLoad=”javascript:pageTracker._setVar(‘exclude_visitor’);”>

This _setVar() value is then stored in the _utmv cookie when a visitor visits the page.

Step 2. Add an Exclude Filter in GA

Analytics>Profile Settings>Edit Filter
Filter Type: Custom filter &gt Exclude
Filter Field: User Defined
Filter Pattern: exclude_visitor (must match the value of _setVar)
Case Sensitive: No

Google Analytics Filter for Exclusion by Cookie

Google Analytics Filter for Exclusion by Cookie

Brian says in ‘Advanced Web Metrics’ that the “value of setVar() is stored in the Google Analytics field labeled User Defined.” (p. 70) I take this to mean that the field labeled User Defined references the _utmv cookie (which holds the value of _setVar() ), but in any case, this explains the use of ‘User Defined’ in the filter field.

All that’s left to do is to point the visitors that you don’t want to track to the cookie page (‘www.yoursite.com/ga-exclude.html’). Once the cookie is set on their browser, it will be there for 24 months, during which time their visits to the site will not show up in Google Analytics. Voila!

Resources

To streamline the process, I have created a basic html template file that can be used as the file that sets the cookie.  To use this file:

  1. Download it and open it in a web editor/text editor.
  2. Insert the site GA tracking code above the </body> tag.
  3. If necessary, review the instructions in the file.
  4. Upload to the server and set up the exclude filter in GA.
Advertisements

6 Responses

  1. Would this exclude only the browser that was used when the original cookie was set? To exclude all browsers types (IE, firefox, chrome) would I need to be sure have employees visit the cookie page from all browsers?

  2. One more question, would this only effect future traffic? Is there any way to filter the historical traffic from before the cookie placements?

    • Hi Lucas,

      Thanks for your questions! Sorry for my slow reply…issue with comment notifications that I’ve now fixed. 🙂

      Question 1:
      Indeed, this would only apply to the browser that was used when the original cookie was set. So users should visit the designated page with each browser – and each computer – they are using.

      Question 2:
      This will only screen out future traffic. GA has no way to distinguish visits from those people in the past, so there is no way to filter the historical traffic. You might be able to make some reasonable estimates based on what you are seeing after the cookie placements.

      cheers,
      John

  3. […] changed some things in googele analytics code in a certain time so other tutorials like this or this those not work. While I was exploring the problem I notice the new things are added to google […]

  4. It seems method works, but not like a charm. Of course there will be light delay before filter actually exclude self visit. First visited exclude.html page. Then tried 3 times to visit my page. 1 visit of 3 have been counted by GA.

    And I like that old design GA screenshot 🙂

  5. Anyway to set it permanently? I fear people won’t get the cookie every 24 hours…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: