GA.JS: New Google Analytics Tracking Code
October 16, 2007 by Justin Cutroni
On October 16, 2007 Google announced a new version of the Google Analytics Tracking Code named ga.js. This means that there are now two versions of the GATC: the old urchin.js and the new ga.js. What makes the new ga.js different from urchin.js?
- The ability to track events
- New object oriented style; no more functions like
urchinTracker() - Smaller size for faster downloads
This post is all about why Google introduced a new GATC, what has changed and how this is going to affect your GA implementation.
Why the Change?
Simply put, Google changed the code to support event tracking. I’m not going to focus on event tracking here, this post is just about the style and usage of the new tracking code. Please see my posts on Event tracking for more information.
Did Google need to create a new tracking code? Maybe not. But I think the modifications necessary to implement event tracking were easier using a completely new set of code rather than the old code. If you look at the entire GA system, almost every part had been re-worked since the acquisition of Urchin in 2005 except the javascript tracking code. It was time for an update and, with the addition of event tracking, the timing was right.
What Exactly has Changed?
In short, everything has changed. All of the functions that we’ve been using are now gone. Functions like urchinTracker() and __utmSetVar() no longer exist. The reason is that the new tracking code is completely object oriented. For those of you that are unfamiliar with programming styles, object oriented programming is a more modular way of creating code.
Object oriented programming does not have functions and variables, it has objects, methods and properties. Each object has an associated set of methods and an associated set of properties. Think of an object as a car. The methods are the actions that you can do with a car, like start, stop. Properties are characteristics of the car, like the color, number of doors, etc.
Details of the New Page Tag
Let’s look at the old and new code to better understand the difference. I’ve numbered the lines so I can better explain what’s going on.
Code #1: Page Tag using urchin.js:

Code #2: Page tag using ga.js

While there are some similarities, you can see that the new code is different. Let me walk you through what it does.
The first part of the code is the reference to ga.js (line #1-#4). This is analogous to the call for urchin.js in the old code (line #1). The big change here is that the code will automatically detect if the current page is secure or non-secure (i.e. using HTTP or HTTPS). It will then dynamically request the appropriate ga.js file from the server. This is a little piece of code that was borrowed from website optimizer.
Next section of code, lines #6 - #8, is where all the work happens. First, we tell GA the account number using pageTracker._gat.__getTracker() (line #6). This is similar to setting the account number variable in the old code (line #4). Then we call the pageTracker._initData() method. This method collects information about the browser, the referral information, etc. And finally, on line #8, we call pageTracker._trackPageview(). This is the new urchinTracker(). This is how we send the data to GA and create pageviews.
The new code does the same thing as the old code, but in a very different way. Again, all of the old functions have changed and in some cases they’ve gone away. If you were using urchinTracker() to generate pageviews (like I was) then you will eventually need to replace that code with pageTracker._trackPageview().
Should you Upgrade?
This is the question that everyone is going to ask and there is no right or wrong answer. Eventually Google will kill urchin.js and you will have to change. But I don’t think that will happen for at least 18 months.
Here’s how I would approach the decision making process. First, do you absolutely need event tracking? If yes, then you should upgrade. You can read all about event tracking in this series of posts.
If you don’t need event tracking then my suggestion is to evaluate the complexity of your current GA implementation and then decide. If it’s fairly straight forward, i.e. you’re not an e-commerce site or you don’t have additional urchinTraker() calls, then I would suggest migrating.
However, if you have a complex implementation, one where you’re calling urchinTracker() a lot, you’re doing cross domain tracking, or you’ve implemented e-commerce tracking, then I would take my time. Plan your migration. There is no rush and you want to get it right.
No matter when you choose to upgrade, make sure you test your implementation before migrating. You don’t want a big hole in your data due to a mistake.
And Finally…
There is a lot more to the ga.js. As I mentioned before, all of the old functions have been replaced. I’ll have a listing of all the new methods soon. Until then, start thinking about your migration!
Subscribe:









36 Responses to “GA.JS: New Google Analytics Tracking Code”
Thanks for a great series of posts, Justin. This is really useful.
But I have one question relating to the ’should you upgrade’ issue.
You suggest that if there is no urgent need to track events, then the new version is not an immediate ‘must have’ upgrade.
But what about the new internal search features? Those seem extremely valuable to me. Or have I missed something (such as those being available using the old tracking code)?
By Tim Leighton-Boyce on Oct 16, 2007
Guess you forgot to tell the Webmaster.
New accounts still get the old code.
Arrggghhhh!
By Douglas on Oct 17, 2007
Fantastic! It’s about time they upgraded this part of GA.
Do you think they’ve done a good job of the re-write?
By Christopher Kata on Oct 17, 2007
Thanks a lot for the info. I just bought your O’Reilly short cut, and was planning some cross domain tracking implementation. So 2 questions:
1. Where can I find info on how to implement cross domain tracking with the new ga.js?
2. When will an updated short cut be published?
By Adam on Oct 17, 2007
Hi Adam,
The official Google documentation should be out soon, I’m not sure when. I’ll try to post some information later this weekend.
As for an updated Short Cut, look for that in a few weeks.
Thanks for reading!
Justin
By Justin on Oct 17, 2007
Hi Douglas,
The new changes are not live yet so GA will still spit out the old code. However, if you’re feeling adventurous you should be able to manually install the new code.
Be sure to test any changes, you don’t want to eliminate data.
Justin
By Justin on Oct 17, 2007
Hi Tim,
The site search reports will be available to all users, even those that do not upgrade to ga.js. Sorry if that was not clear in my post.
Justin
By Justin on Oct 17, 2007
Hi Christopher,
Yes, I think Google did a great job with the rewrite of ga.js. They created a nice framework that will be easy to expand in the future. The event tracking feature is amazing and I think developers will really like the new object oriented style.
Justin
By Justin on Oct 17, 2007
A very informative post, thanks for the detailed analysis of the difference between the two codes. I am eager to see what Google puts out regarding implementation etc.
By AdamG on Oct 17, 2007
Hi Justin,
thanks for this very interesting report. The big question i aks myself: “Should I upgrade”?
You write:
“However, if you have a complex implementation, one where you’re calling urchinTracker() a lot, you’re doing cross domain tracking….”
-> we don’t have a complicated implementation, but our shopping cart is hosted on a different domain, so we had to make minor changes, so GA would store the conversions correctly. If this is the only add. configuration we did, yould you recommend us to upgrade to the new code?
By Kolumbien-Andre on Oct 17, 2007
Wont´t work!
Cannot place a script-tag inside a script-tag like the above image.
So writing the image off will generate a error “Nested Script Block” - Sorry.
By Harley on Oct 18, 2007
Hi Kolumbien-Andre,
If you’re using cross domain tracking on your current site then the migration should be fairly simple. All you’ll need to do is update the function calls and tracking code.
Thanks for reading and thanks for the comment.
Justin
By Justin on Oct 22, 2007
Will there be a gap in the data when we upgrade to the new GA.JS code? Or will Urchin continue tracking data until GA.JS is activated. What should I anticipate? Thanks.
By Nhu-Chi on Oct 23, 2007
Hi Nhu-Chi,
There should not be any gap in data collection. The new tracking code does the same thing as the old tracking code. It requests the __utm.gif file from a server. As long as the code is added correctly, and the __utm.gif is requested from the GA server, there should be no loss in data.
Justin
By Justin on Oct 23, 2007
Hi Mike,
I’m not sure which site you’re trying to use ga.js on, but I ran a few tests and it seems to be working. Remember, this is all in beta, so stuff could break unexpectedly.
Justin
By Justin on Oct 29, 2007
GA does not consider a flash ad on adtech.panthercustomer.com as a referral :(
Adtech is a major advertisement partner for large portals, magazines etc.
GA identifies these as (direct) even though we in our servers log-files identify the complete url as a referral, GA choose to strip these out.
Is there a reason that GA choose to NOT display large ad-companies? How about beeing independent. I other word you should seriously consider buying a tracking application that displays all referrals, and not just Google.
By Harley on Nov 9, 2007
Looking around a little, there were some interesting answers on a Google help page:
http://adwords.google.com/support/bin/answer.py?hl=jp&answer=55585
and in english:
http://adwords.google.com/support/bin/answer.py?hl=en&answer=55585
With the english help text still referring to the older urchinTracker. Some different topics are handled in the japanese version.
PS! The _udn=”example.com”; is changed into pageTracker._setDomainName(”example.com”);
Regards,
Rune Karlsen
By Rune Karlsen -VISITE- on Nov 12, 2007
Hi Harley,
GA does not discriminate against any referring site. What’s probably happening is that there is some type of redirect that is altering the original referral information.
I would examine the GA tracking cookies to verify that the values, which identify the visit source, are correct. If they’re not correct then there is probably a server or programatic redirect betweenthe visitor’s click and the landing page.
Thanks for reading and I hope that helps.
Justin
By Justin on Nov 17, 2007
Hi Justin, For the new ga.js. How would this affect the 404 error page code tracking?
We have the following on our custom 404 error page
urchinTracker(”/404.html?page=” + _udl.pathname + _udl.search). Are there functions with the new ga.js that can be used to track the source path of the 404 errors?
I am still looking for any documentation on the Google Analytics Help Center, do you happen to know when any might become available? Or am I just not looking in the right spots.
Thanks!
Wendi
By Wendi on Nov 20, 2007
Will the new script function on anyone’s account? I noticed it’s not yet in my Analytics settings. If it works, why has Google not put it in yet? Wasn’t there a big hoo-haw about it?
By Jenny on Nov 22, 2007
Hi Wendi,
New documentation should be released when the new tracking code is pushed out to all accounts. I’m not sure when that will be, but I think it will be soon.
Regarding your 404 question, yes you will still be able to track 404s. However, the urchinTracker() function does not exist in the new version of the tracking code. If you choose to migrate to the new tracking code you will need to replace all instances of urchinTracker() with the new function pageTracker.trackPageview().
You’ll also need to remove the _udl variable references because those variables no longer exist in ga.js.
I know I’m short on answers, but thing will be more clear when the code is live and you start digging into it.
Thanks for reading and thanks for the question!
Justin
By Justin on Nov 26, 2007
Hi Justin
Thanks alot for the info.
What is the answer to the nestet-script-error? Code #1 works. Code #2 does not. Is it just not possible to implement ga.js yet or have i missed something?
Thanks.
Brian
By Brian on Nov 27, 2007
Hey Jenny,
The new JS code should work on any account. It has not been rolled out yet because there is no documentation to support it. Once there is some documentation to help user migrate it will be released.
Justin
By Justin Cutroni on Dec 2, 2007
hello!
i’ve just tried but doesn’t work in my site!!
i’ve got an asp error
By simmons on Dec 16, 2007
I get the following error when I place the new Google Analytics code on my pages:
Active Server Pages error ‘ASP 0138′
Nested Script Block
/an-asp-page.asp, line 87
A script block cannot be placed inside another script block.
Is this a known bug because I’m just cutting and pasting out of Analytics into my wbe pages, just above the closing body tag, as instructed. Would appreciate any solutions to this. Thanks.
By Nathon Jones on Dec 17, 2007