Monday, 2 June 2008

Google Analytics Tracking Code Issue with Contribute CS3 - _gat is not defined,

You may have noticed in Google Analytics that there is a new code and a legacy code. We recently experienced an issue where a client updating their content using Contribute CS3 repeatedly got script errors on their pages as soon as they published changes. The site reported that there was a closing issue with the script and it added ")); below the footer on each published page. The site was written using the new Google tracking code:

The solution, after trying several alternative template changes, seems to be using the legacy analytics code as it seems to work without issue with Contribute CS3.

Please note: the legacy tracking code is urchin.js and the new tracking code is ga.js

There is a much longer explanation of the issue, but for now, the simple fix saves lots of time.

1 comments:

Peter said...

Switching to the legacy analytics code is not a true solution, because the new ga.js code will eventually be the standard and allow sites using google analytics to make use of new features Google develops.

This error can display from the way javascript is rendered by the browser, most browsers don't show this error when it happens, but Contribute will display it every time you navigate to the page.

From google:
you can modify the tracking code to use the "typeof" javascript operator to check that "_gat" object is valid before executing the tracking code.

For example:

if (typeof(_gat) == 'object') {
var pageTracker = _gat._getTracker("UA-XXXXX-X");
pageTracker._initData();
pageTracker._trackPageview();
}


please see
Trouble installing Google analytics

and
How-To Solve: "_gat is not defined", a google analytics workaround

for more info