GA.JS: New Google Analytics Tracking Code

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!

Be Sociable, Share!

    Like this post? Sign up to get posts delivered to your inbox.

    Comments

    1. Tim Leighton-Boyce says

      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)?

    2. Adam says

      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?

    3. says

      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

    4. says

      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

    5. says

      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

    6. says

      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

    7. says

      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.

    8. says

      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?

    9. Harley says

      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.

    10. says

      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

    11. says

      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.

    12. says

      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

    13. says

      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

    14. Harley says

      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.

    15. says

      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

    16. says

      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

    17. says

      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

    18. Jenny says

      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?

    19. says

      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

    20. Brian says

      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

    21. says

      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

    22. Nathon Jones says

      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.

    23. says

      Hi

      My question is this….. at the time of this post, the tracking code was in beta…. is it still, or is this now official? I’d like to migrate but I’m scared of things breaking….. or loss of data, although you said I shouldn’t loose any data.

    24. David Wang says

      hi Justin, I bought the shortcut ebook too and I’m still waiting for the updated version.. any news on that?

    25. says

      Hi – we were using urchin tracker and migrated to the ga code. However, we are no longer receiving 404 hits. How do I remedy and/or troubleshoot this? Any information would be greatly appreciated.

    26. says

      Hi Jason,

      ga.js does not contain urchinTracker() or any of the other functions or parameters you referenced in your comments (both of them :) ). If you want to use ga.js then you need to re-write any existing JavaScript that uses GA code.

      Hope that helps put you on the right track,

      Justin

    27. says

      Hi Patricia,

      There has been no official date set for the termination of urchin.js tracking. My guess is that they will let people use it for some time to come.

      However, I still think it’s a good idea to migrate in the near future.

      Justin

    28. velmurugan says

      I have one question about GATC?

      we have used the urchin.js, now i want migrate to the ga.js. now i have paste that code in every page. if i want to change my GA code, i have to change in every page, so, i create the separate .js file within my folder and can call from that script, its possible?

    29. says

      Velmurugan,

      I’m not exactly sure that I understand the question, but if you want to migrate from the old tracking code to the new tracking code you need to replace the old GA page tag with the new GA page tag. The new code no longer contains the old functions. So you can’t just swap the urchin.js file with the ga.js file.

      Also be aware that if you are creating any virtual pageviews, of using ecommerce tracking, then other parts of your implementation will also need to change.

      Hope that helps.

      Justin

    30. says

      Hi,
      We’re quite new to GA and unfortunately implemented the old urchin.js code by mistake. Is there likely to be any issues or loss of data if we simply replace the old code with the new GA.js code that is available from our profile settings?
      Cheers,
      Kylie

    31. says

      Kylie,

      There shouldn’t be any issues if you just swap out the old code for the new code. Some things to keep in mind though:

      1. If you’re making any additional calls to utchinTracker() then you need to change those to trackPageview().

      2. If you’re using ecommerce tracking you’ll need to change that code.

      The best advice is to test the new code on a dev site just to make sure everything works as expected.

      Justin

    Trackbacks

    1. [...] Well, now that I sort of understood how the Google Analytics code worked, the guys over at GA thought: heck, let’s throw it all out (well not just yet, but we’ll get to that in a second), and come with some new stuff. Justin has done a great job, as usual, covering all this new stuff on Epikone, and you specifically want to read his post about the new tracking code. [...]

    2. [...] Google Analytics – nadchodz± du¿e zmiany, nowy GATC i event tracing Google Analytics przechodzi du¿e zmiany. Jeszcze tej informacji oficjalnie nie ma, ale ju¿ nied³ugo dostêpny bêdzie w ca³kowicie nowy Google Analytics Tracing Code – ga.js. Oznacza to, ¿e bêd± dwie wersje – stary urchin.js i nowy ga.js. Czym siê ró¿ni±: – mo¿liwo¶æ ¶ledzenia zdarzeñ – pe³na orientacja obiektowa – mniejszy plik – czyli szybszy downloading Ca³a zmiana s³u¿y³a jednemu – wsparciu ¶ledzenia zdarzeñ. Dziêki temu mamy mo¿liwo¶æ ¶ledzenia stron ajax i reach media, bez robienia protez typu urchintracker() i tworzenia wirtualnych page views. Koniec z mierzeniem klikniêæ na zewn±trz poprzez dodawanie onclick…. ¦ledzenie zdarzeñ daje nowy poziom w hierarchii danych: – odwiedzaj±cy (visitors) – odwiedziny / wizyty (visits) – ods³ony (pageviews) – zdarzenia (events) Oto jak bêdzie wygl±d±³ nowy kod wklejany na strony, do porównania ze starym: Stary Nowy: ¬ród³o i wiêcej przeczytacie tutaj: Analytics Talk » Blog Archive » GA.JS: New Google Analytics Tracking Code Tak a propo, to http://www.epikone.com/blog/ to jeden z najlepszych blogów o GA dla zaawanasowanych, polecam ju¿ chyba którys raz z rzêdu, równie¿ ich ebook za jedynie 10$ do kupienia w ORelly. Nowy GATC to odpowied¼ na reach media. Warto przeczytaæ ten post, jak ju¿ zaraz bêdzie mo¿na mierzyæ interakcjê u¿ytkonika na stronie, na stronie gdzie nie ma kolejnego page view: Analytics Talk » Blog Archive » Event Tracking Pt. 1: Overview & Data Model Poza tym nowe w Analytics – mierzenie wewnêtrznej wyszukiwarki, wiêcej u Mariusza: Zapytania z wyszukiwarki wewnêtrznej w Google Analytics __________________ pozdrawiam: Tomek [...]

    3. [...] Google announced a new version of the tracking code, ga.js, in October 2007 and launched the new code in December. After some minor launch problems things seem to be running smoothly. While you don’t need to migrate to ga.js, you should start to think about it because Google will no longer add features to urchin.js. In my opinion, you should tackle this problem sooner rather than later. [...]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>