Irrsinn.net: taking joy in human unreason

Excluding Hits from Google Analytics

If you want to exclude your own visits to your site from Google Analytics on a per-computer or per-session basis, searching will land you on Google’s help page: How do I exclude internal traffic from reports?. Problem is, the code there doesn’t work with Google’s new-ish asynchronous tracking code. There is no “pageTracker” object any longer, so that’ll throw a nice little error.

The replacement for “pageTracker” is to push the custom variable onto the _gaq object, per the new standards.

To get this working, make a new, simple HTML page, just including the basics to make the page validate. Include your standard-issue Google tracking code in the head, like so:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxx-x']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ?
      'https://ssl' : 'http://www') +
       '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
  })();
</script>

(Reformatted to show nicely on the blagowub.)

Then you need to add your custom variable. It needs to go right before the trackPageview statement:
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxx-x']);
  _gaq.push(['_setCustomVar', 1, 'supa_secret_fly', 'true', 2]);
  _gaq.push(['_trackPageview']);
...//Same as above

Toss this page up on your server, add it to your robots.txt (for what that’s worth), and visit it. To verify that it’s working, use the Web Developer toolbar in Firefox to examine your cookies after visiting the page. You should see a cooked named “__utmv” with a value of something like “268229078.|1=supa_secret_fly=true=1,”.

Now you need to tell Google Analytics to ignore folks with that flag set. I was wrong about being able to set up a filter.. It looks like you can’t filter on custom variables with the new asynchronous code, so Advanced Segments are the way to go. In this case, set up an Advanced Segment called something like “Exclude Me”, and set the Custom Variable 1 to not contain “supa_secret_fly”. You won’t see the results filtered on the Analytics landing page, but once you go into your reports, you can pick it.

Then have fun hammering some obscure page of your site and checking your stats, although be prepared to be patient with the lag time in Analytics picking up visits in the same day.

(Sources: “Google support“, “Exclude your visits in Google Analytics: get better data“)