Updated GA.JS Tracking Code

December 17, 2007 by Justin Cutroni

I’ve gotten a lot of comments over the weekend about using the new ga.js code with ASP sites. It turns out that the code does not work with nested SP script tags. Also, the new tracking code that GA spits out is not XHTML compliant. These are unrelated issues that are causing a number of issues.

Google knows of these issues and will be pushing a solution soon. I’m not sure when, but soon. In the meantime, here is a copy of the new code that you should use on your site.


<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">
 var pageTracker = _gat._getTracker("UA-xxxxx-x");

 pageTracker._initData();
 pageTracker._trackPageview();
</script>


Remember, you need to replace “UA-xxxxx-x” with the appropriate information for your profile.

Sorry for the short post… I’m pretty tied up at work but wanted to post this information.

Props to Eivind Savio for sending me a nice email about the problem and a number of solutions.


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. 16 Responses to “Updated GA.JS Tracking Code”

  2. Hmm, the script block are gone. I meant this:

    <script type=”text/javascript” src=”http://www.google-analytics.com/ga.js”></script>

    and:

    <script type=”text/javascript” src=”https://ssl.google-analytics.com/ga.js”></script>

    By André Scholten on Dec 17, 2007

  3. I just switched over to GA.js this past Friday on my legacy ASP-based site and had the same issue. My solution was the same.. split up the word “script” into two pieces so the idiotic VBScript parser doesn’t fail on something that’s not really a problem.

    Thanks Microsloth for trying to protect us from ourselves!

    By Ian on Dec 17, 2007

  4. More like: thanks, Google for not testing your product better before you rolled it out. I love Google, but to say I’ve been disappointed with this rollout of the new code would be a big understatement. It doesn’t even work with Blogger! That’s a Google product! Amazing.

    Anyway, thanks Justin for posting this info.

    By Reid on Dec 17, 2007

  5. Appears crazy that Google would release this new analytics code before ensuring it was valid XHTML.

    Andre, can you explain what you meant in your post above about the script blocks? Are you saying these should be included?

    By Paul Galatis on Dec 18, 2007

  6. Andre,

    You’re absolutely correct. You can use those simplified script tags to include the ga.js. The new code that I posted should also work.

    Reid,

    I agree 100%. It’s kind of amazing that Google would make this change.

    Thanks for the comments and thanks for reading.

    Justin

    By Justin Cutroni on Dec 18, 2007

  7. @Paul, Justin explains what I meant. You can use those simplified script blocks in stead of the first script block Google suggests.

    @Justin: there’s an other problem with your solution: I miss the CDATA blocks, without them the new script is not XHTML Strict compliant.

    But, who cares about validation anyway ;)

    By André Scholten on Dec 18, 2007

  8. Hey André,

    You’re right, the code I posted is not XHTML compliant. I chatted with Google and they need to decide between XHTML compliant and compatible with MSFT ASP script blocks. They decided to go with the latter.

    If you need XHTML compliant script you can add the CDATA blocks.

    I believe they’re pushing the code in the next day or so.

    Justin

    By Justin Cutroni on Dec 18, 2007

  9. When will the new event tracking feature be released to the public? I wrote all the code to do this and then found out it was in closed beta and it’s not available in my reports :(

    Thanks,
    Michael

    By Michael on Dec 27, 2007

  10. Hi

    Good to see a post on this error by Google Analytics, has there been any New Year updates yet?

    Thanks

    By Andy S on Jan 4, 2008

  11. Hi Michael,

    I’m not sure when the reporting side of event tracking will be released to the public. It’s a pretty big change for GA and I think they’re going to take it slow.

    Andy,

    The tracking code has been updated and should be working now. Check your profile settings to retrieve the correct version of ga.js.

    Thanks for reading,

    Justin

    By Justin Cutroni on Jan 11, 2008

  12. Hi Justin,

    It looks like there is an extra space after “ssl” in the code you posted, which could lead to secured pages not beeing tracked:
    "https://ssl ." : "http://www.");

    Regards,

    Paul

    By Paul Bernier on Jan 18, 2008

  13. Paul,

    Thank you very much for the heads up. I made the change this morning. I hope this didn’t cause anyone an issue.

    Justin

    By Justin Cutroni on Jan 20, 2008

  1. 4 Trackback(s)

  2. Dec 21, 2007: WebmasterFarms - The Science of Knowing Your Customers » Blog Archive » Updated Google Analytics Tracking Code for ASP sites
  3. Jan 4, 2008: Updated GA.JS Tracking Code
  4. Jan 8, 2008: Analytics Talk » Blog Archive » 2008 Google Analytics Resolutions
  5. Mar 18, 2008: Updated Google Analytics Tracking Code for ASP sites | Pure Web Analytics

Post a Comment