Sunday, 31 January 2010

Filtering out specific known visitors from Google Analytics traffic

Many times you want to filter out from your Google Analytics data, traffic from specific users or computers. The typical example is traffic originating from company employees or web designers working on new pages.
Normally you would create a filter excluding specific IP addresses (of your known visitors), or using the __utmv cookie setting a custom variable (in case the visitors you want to exclude have dynamic IP addresses). To set the __utmv cookie we will have to use the _setVar() method. As of December 2009 the setVar method wont be deprecated but the _setCustomVar() method will enrich the toolbox.
This is great news as the _setCustomVar() method offers a far richer way to create custom variables. The new syntax is:
_setCustomVar(index, name, value, opt_scope)
Where:
  • Int      index       The slot used for the custom variable. Possible values are 1-5, inclusive.
  • String   name        The name for the custom variable.
  • String   value       The value for the custom variable.
  • Int      opt_scope   The scope used for the custom variable. Possible values are 1 for visitor-level, 2 for sesson-level, and 3 for page-level.
This means that we will be able to define several custom variables and create very sophisticated user classes and filters.
Unfortunately the new variables didn’t make it yet to the filters therefore we will be forced to use the old _setVar() method.

Filtering a specific IP address

This is quite straightforward. Just go to the “Create new filter” page, select “predefined filter”, and set the options like in the figure below, entering the IP address that you want to exclude.

Filtering traffic based on a permanent cookie

The process is a bit more articulated.

Step 1 – Create a hidden page that will set the cookie

Create a static html page in your domain that will contain the following code:
<html>
<head> <title>Google Analytics cookie setting for traffic filtering</title>
</head>
<!-- Note: 'filter_visitor' is the content of the cookie remember this for later -->
<body onLoad="javascript:pageTracker._setVar('filter_visitor');">
<h1>Google Analytics cookie setting</h1>
<p>Put any HTML code you want here</p>
<!-- Insert regular Google Analytics Tracking Code used on your site here: -->
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-XXXXXXX-1"); pageTracker._trackPageview(); } catch(err) {}</script>
</body>
</html>
Of course ‘filter_visitor’ is your custom value and you will be able to set it as you want. You will need to replace UA-XXXXXXX-1 with your proper tracking ID.

Step 2 – Save the page in your server

Name the previous page with a name like GAfilter.html and save it in your web server (of course in the same domain name).

Step 3 – Create the exclude filter

Go to Google Analytics and create a new filter.
Using the settings in the picture. You will have to fill the “filter pattern” field with the value we defined before. In our example filter_visitor.

Step 4 – Set the cookies on the visitor computers you want to filter

Ask all the people you want to exclude to visit the page you created before:
http://www.example.com/GAfilter.html
This will set the custom cookie in their browsers that you will be able later on to filter in your reports. It is a permanent cookie that has a lifetime of exactly two years. Naturally it will be necessary to revisit the page every time the cookies are deleted in the browser.

0 comments:

Post a Comment