Wednesday, May 19, 2010

How to exclude your own visits from Google Analytics Reports




Owning or managing a site means you are using a traffic analysis tool. Well, at least it means you should. Google, as usual, has built such a tool and it provides it to the webmasters for free. They say it's "Powerful, flexible and easy-to-use" and, from my experience, it really is.
There is, however, a but. It doesn't provide a really quick way of excluding your own visits from its reports.


If you have a site to which you make often changes or reviews, you may find yourself in a situation where one of the main users of your site is actually you. 

This is a big D'Oh. Especially for small sites.
Because of this, you may get excited to see that someone has just spent 3 hours on your site, later to discover that it was actually you.


Well, Google does provide an answer to this:
http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55481

It basically instructs you how to build a filter in Analytics to exclude the visits that came either from a specific IP address or a computer with a certain cookie set. I don't know of many people who have a static IP address and I'm guessing you aren't one of them, so the cookie part is the one you probably want.

The cookie solution involves 2 steps.
  1. Create a filter on the analytics page.
  2. Run a JavaScript snippet on your browser to mark it as your own.

    javascript:pageTracker._setVar('your magic filter value');
Step 2 is where things can go wrong. The new analytics snippet(the one you have to include in your site) uses ga.js(the main analytics script) in a different way.
I'll show the two snippets, maybe you can spot the difference.

Here's the old snippet:

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"));
try {var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}


And here's a sample of the new snippet:

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-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);
  })();

The main difference is that the old script defines the pageTracker object which is then used in your step 2 to set the cookie. The new script doesn't define this object and if you try to go through step 2 the way Google tells you, you won't solve anything because you'll get an error.

I noticed that _setAccount and _trackPageview are called in a peculiar way in the new snippet. They are pushed into an object instead of being called directly like in the old script. Some other function makes the actual calls.
So here is my solution.
Instead of trying to call _setVar as a function of the pageTracker object (which is not defined), call it asynchronously like the new script calls _setAccount and _trackPageview:

_gaq.push(['_setVar', 'your_magic_filter_value']);

So let's go through whole the solution again.
Step 1.
Go to your Analytics Settings -> Filter Manager(bottom right) -> Add Filter

Filter Type: Custom filter > Exclude
Filter Field: User Defined
Filter Pattern: your_magic_filter_value
Case Sensitive: No

Step 2.
Open your site in the browser and type this in the address bar:

javascript:_gaq.push(['_setVar', 'your_magic_filter_value']);alert('done');

If you get a message that says "done" then you're all set.
Repeat step 2 for each browser you use.

That's it. Oh, you can replace "your_magic_filter_value" with any text you want just as long as it's the same text in the filter and in the script you type in the address bar.

Enjoy!

Wednesday, May 12, 2010

BlockScape - levelpack 4


This pack has 20 new levels.

I've also built a level picker so you can jump to a desired level more easily.
Harder levels are initially locked(gray) and get unlocked(red) as you complete(green) the easier ones. If you've been here before and managed to finish all the previous 30 levels, most of the new 20 should be unlocked.
I challenge you to turn the board GREEN.
There's at least one more levelpack coming up.

I have an idea about a new game, I'll start coding it as soon as time presents itself. It will probably be based on the HTML5 <canvas> tag. I won't give any more hints as I'm not sure exactly what it will be but keep in touch, you may like it.
Enjoy!(and don't forget to share)