Blogger Blogs, ga.js, and XHTML
 If you’re having trouble getting the new Google Analytics, ga.js code to work on
 your Blogger Blog, we have a quick and easy fix for you.
 Actually, this fix isn’t specific to Blogger or even Google Analytics for that matter.Â
 To make your web pages XHTML-compliant, you should either do this on all of your
 scripts, or you should move your scripts to a separate file.
 Here’s an example: (Beware of smart quotes. WordPress keeps changing my formatting automagically.)
<script type=’text/javascript’>
//<![CDATA[
var gaJsHost = (("https:" == document.location.protocol)
? "https://ssl." : "http://www.");
document.write("\<script src='" + gaJsHost
+ "google-analytics.com/ga.js' type='text/javascript'>\<\/script>" );
//]]>
</script><script type=’text/javascript’>
//<![CDATA[
var pageTracker = _gat._getTracker("UA-XXXXXXX-1");
pageTracker._initData();
pageTracker._trackPageview();
//]]>
</script>
See how the first line after each script tag, we have: //<![CDATA[
And right before closing each script tag, we have: //]]>
The forward slashes are JavaScript comment indicators for older browsers that don’t understand CDATA. This post on About.com provides some additional information.  Note the author uses multiline comment characters rather than the // we use here. Both methods will work just fine.
So what have we done?
 We told the browser’s validator to ignore our script by wrapping it in the CDATA
 tag. So now Blogger and the W3C will happily accept your Google Analytics
 Tracking Code.
 - Jason Green
Â







December 22nd, 2007 at 5:32 pm
THANK YOU THANK YOU THANK YOU!
I’ve been looking for a way to get my new Google Analytics code to validate. The CDATA tag did the trick. I should have known.
December 29th, 2007 at 7:29 pm
[...] an Analytics Authorized Consultant. However, in all fairness, the exact fix I needed was written by Jason Green of LunaMetrics, who graciously allowed Mangold Sengers to reprint it. Thanks to [...]
February 25th, 2008 at 10:53 am
[...]you should either do this on all of your scripts, or you should move your scripts to a separate file.[...]
If I want to use second option, how would I do that?
February 25th, 2008 at 2:18 pm
You can put the entire GA script into a second file, we’ll name it mygacode.js, and save that on your server.
*Make sure to remove all tags from mygacode.js. This file should just be straight JavaScript.*
Then, on the page where that script would normally show up, put the code below:
w3schools.com has a great tutorial here: http://www.w3schools.com/js/js_whereto.asp
I hope this helps.
- Jason Green
March 30th, 2008 at 1:10 pm
Which is right code for a Blogger blog?
July 14th, 2008 at 5:34 am
Thank you, good work