JavaScript: Double script tags in Google Analytics tracking code

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-05-13T23:56:49Z Indexed on 2010/05/14 0:04 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

This is more a curiosity question than anything else...

Google instructs to add the analytics tracking code as follows:

<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">
   try{
      var pageTracker = _gat._getTracker("UA-xxxxxx-x");
      pageTracker._trackPageview();
   } catch(err) {}
</script>

I'm wondering some JS guru here could tell me why they're separating it into two script tags instead of sticking it all inside one. I know that the top part could be put in the header and the bottom part just before body tag to ensure the page loaded before it's tracked, but I'm wondering if there's something more to it. Anyone who'd know that would likely know how to separate the code into two tags anyway.

I'm only asking as this is coming from the Goog and is being used by millions of sites...

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about google