I've recently fallen hit a snag with the Google Analytics API, that's really put a dint in my day. The suggested script comes with a caveat, that it should be included in the header of the page. But this goes against current performance guidelines, that you want all of your scripts to go at the bottom of the page, particularly if they're loading in third-party scripts.
So why does that script need to go at the top of the page? Is it anything to do with page load timings? Nope, the APIs used to time pages are built into the browser, or on some versions of IE they're built into the Google toolbar where it's present. So it ain't that. Could it be because if you put anything into the global "_gaq" array before the account number, those instructions go in with a bogus made-up account number?
We don't get a nice clear JS error telling us something's wrong. the analytics JS file doesn't look for the account number first before sending the rest of the traffic, it doesn't refuse to send the commands (so the bug might be more obvious if you're monitoing HTTP traffic using Fiddler or Firebug, etc), it sends the commands with a bogus account number. Ouch.
I propose a minor tweak to the Analytics code block that fixes this problem, but first some background.