Google Analyitics – fix the ‘_gat’ is undefined problem in IE6 with Mootools

Google Analytics is a fantastic, totally free, web tracking service provided by Google. It works by embedding a Javascript include at the bottom of your page which loads Google’s tracking code. The code then passes all your tracking information to the Analytics cloud where it is digested and returned in the web-app.

The Problem

The code has to be included just before your closing body tag, this is because Google doesn’t want to hold up your page loading while it crunches this data, by placing it there it guarantees 99% of your page has finished loading. This works perfectly in almost all cases but there have been exceptions. The only time i have come across a problem was when i was testing a site on Internet Explorer 6. I kept receiving this error:
'_gat' is undefined
This can only be created when the code tries to call the Analytics code before it is launched. Basically, it’s is trying to call the function _gat too soon. Google have tried to fix this by adding a Javascript ‘try’ function.
<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("UX-XXXXXX-X");
		pageTracker._trackPageview();
	} catch(err) {}
</script>
This helps as the code ‘tries’ to run the function and if it can’t then it runs the catch command. This works because it doesn’t create any other errors and the site will continue operating normally. The only problem is no tracking information will be passed.

Fixing it with Mootools

Firstly, more and more websites are using Javascript to provide better interactivity and functionality so there is a good chance you are already using a library in your site. Secondly they provide core functions which can detect when the page’s DOM is ready and when the page itself has actually finished loading. There a couple of things that can improved by converting this code to use a popular Javascript framework such as Mootools. Page load speed should be improved as we try to insert the tracking code after the page has completely finished loading and has been initiated. As a result we can also hope to improve the code performance by making sure the code has been downloaded before trying to run it. Using Mootools we simply add a new Javascript assets into the page using the ‘addEvent’ function and then run the analyitics function calls when it has loaded.
window.addEvent('load', function() {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
		onload: function() {
			try {
			var pageTracker = _gat._getTracker("UX-XXXXXX-X");
			pageTracker._trackPageview();
			} catch(err) {}
		}
	});
});
As you can see, I have left the try function in, this is to protect against the same problem we faced initially. Only this time the chance of it happening has greatly reduced. Another benefit of using this method is that you can remove the tracking code from your HTML template or masterpage. Now it is simply added into your Javascript include instead. If you use analytics to track your AJAX calls then it helps that all your code is in the same place! I hope you find this useful, please leave a comment if you know of any improvements or think you know a better method. Tags: , , ,

iGoogle gets an update and now i can check Gmail at work!

Land Here

Your browser homepage is more important than you think. The days of the old Google are long gone. You can personalise a homepage to include many optional extras such as email, RSS aggregators, calendars, ebay listings and even weather forecasts. Not only that but there are a multitude of similar solutions available. Live.com, yahoo, netvibes to name a few. I have experimented with netvibes and live.com but every time I find myself back with Google.

Why?

Well, these solutions are limited by two problems. Compatibility and speed. A huge percentage of the add-ons are made by independent programmers and as such they sometimes don’t include all the features you would expect, have compatibility issues and sometimes they are not maintained. The second issue occurs when you add many add-ons to your page thus increasing the load times. Some solutions offer a tab system which only load the visible add-ons but this can still be a problem. As a result I now only have three add-ons. Gmail, google calendar and a to-do list. Personally I have found anything else is worthless to me as I find it easier to go the site. The only other one I tried was the Google Reader add-on. Again, this simply did not perform well enough to stop me needing to view the actual web app. I now have a streamlined and efficient landing page. It shows me the information I need to know when I first start surfing and ONLY the information I would automatically check every time.

What’s changed

Google has updated iGoogle! Some would say it’s long over due, I would have to agree. The minimal Google look is surprisingly looking a little bit dated! (strange i know) It’s looking a lot more like netvibes, round corners and more effective tabbing. Check out the screenshot.. It gives it a more friendly feel. I much prefer it. There are practical benefits too; you have the option of viewing your add-on at full width and with the more developed add-ons you get much more functionality. The two which have impressed me the most is Gmail and Google Calendar. Click on the side nav and you basically get these two as fully functional sites. I now can cancel Gmail from forwarding all my mail to my work email account and can receive it right here in the browser, even though it is blocked. Fantastic. Good work Google. Thanks! (ps my iGoogle page has the radiohead theme) Tags: