GA.JS: New Google Analytics Tracking Code

October 16, 2007 by Justin Cutroni

ga.js - Google Analytics tracking codeOn 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:

urchin.js code

Code #2: Page tag using ga.js

ga.js code

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!


Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Technorati
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Ma.gnolia
  • YahooMyWeb
  • co.mments
  • Reddit
Subscribe:
  1. 36 Responses to “GA.JS: New Google Analytics Tracking Code”

  2. 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

  3. Guess you forgot to tell the Webmaster.
    New accounts still get the old code.

    Arrggghhhh!

    By Douglas on Oct 17, 2007

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. hello!
    i’ve just tried but doesn’t work in my site!!
    i’ve got an asp error

    By simmons on Dec 16, 2007

  26. 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

  1. 11 Trackback(s)

  2. Oct 16, 2007: Analytics Talk » Blog Archive » Outbound Link Tracking
  3. Oct 16, 2007: Webanalyse - Ny funksjonalitet i Google Analytics
  4. Oct 17, 2007: Cool new stuff in Google Analytics - Analytics - Joost de Valk’s SEO Blog
  5. Oct 22, 2007: WebAudit Blog » NowoÅ›ci z dziaÅ‚ki Google Analytics
  6. Nov 22, 2007: Google Analytics - nadchodz± du¿e zmiany, nowy GATC i event tracing - Sprawny Marketing Forum
  7. Dec 13, 2007: Analytics Talk » Blog Archive » Surprise! New Google Analytics Features
  8. Dec 13, 2007: Nouveau code de suivi Google Analytics (GA.js) | Le blog du Studio ALCD
  9. Dec 20, 2007: Canvis a Google Analytics | Bitacolitis Aguda
  10. Dec 21, 2007: New Google Analytics Tracking Code - GA.JS | /*SpiderSavvy Blog*/
  11. Jan 8, 2008: Analytics Talk » Blog Archive » 2008 Google Analytics Resolutions
  12. Jan 26, 2008: E-commerce tracking in osCommerce with new Google Analytics Tracking Code (ga.js)

Post a Comment