Keeping yourself out of your Google Analytics statistics

31 . January 2008
written by Clemens Lang at Jan 31st 2008, 22:54

Using Google Analytics and wondered how many clicks you add to your site(s) on your own? Here’s an old way to keep yourself out updated for the new Google Analytics JavaScript.

Based on Count me out on Analytics Talk I’m keeping myself out of my sites’ statistics. However Google released a new version of their tracking JavaScript in December and they’re no longer adding new features to the old urchin.js. So updating is a god idea, but I still want to keep my hits out of my site statistics.

A quick glance at the Google Analytics help center told me there was a way to set custom variables in the new JavaScript, too, so I modified my keep-me-out files:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <title>removing you from GoogleAnalytics...</title>
  3. <script type="text/javascript">
  4. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  5. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  6. <script type="text/javascript">
  7. var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
  8. pageTracker._initData();
  9. pageTracker._trackPageview();
  10. <script type="text/javascript">
  11. function setSegment(next) {
  12. pageTracker._setVar('someSpecialTextYoullUseInACustomFilterInGoogleAnalytics');
  13. if (next.length > 0) {
  14. document.location.href = next;
  15. } else {
  16. window.close();
  17. }
  18. }
  19. </head>
  20. <body onload="setSegment('http://location.of/the.same.script.on.another.site.of.yours.to.redirect')">
  21. </body>
  22. </html>

So what is this doing? First, it loads the Google Analytics JavaScript (lines 5-8), then it initializes the tracking (lines 9-13) and last but not least the function (lines 14-23) that sets the custom value in the tracking class and redirects if you passed another URL to it (useful for chaining all your sites). Finally in line 25, the function gets called on page load.
Combined with “dom.allow_scripts_to_close_windows” in Firefox’ about:config you can add this page as a new start page tab and have it exclude you automagically every time you press the home button (and the window closes automatically after this, which keeps your tab bar clean).