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’>
var gaJsHost = ((“https:” == document.location.protocol)
? “https://ssl.” : “http://www.”);
document.write(“<script src='” + gaJsHost
+ “’ type=’text/javascript’></script>” );

<script type=’text/javascript’>
var pageTracker = _gat._getTracker(“UA-XXXXXXX-1”);

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


    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.

  • Pingback: Google Analytics Script does not Validate in XHTML 1.0 Strict — Lawrence Salberg()

  • […]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?

  • @John,

    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:

       <script src="mygacode.js"></script>
 has a great tutorial here:

    I hope this helps.

    – Jason Green

  • Which is right code for a Blogger blog?

  • Thank you, good work 🙂

  • Thank you for this, it’s wonderful!

    Unfortunately, my Blogger blog tells me:

    Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly. XML error message: Open quote is expected for attribute “{1}” associated with an element type “type”.

    Would you happen to know what’s going on?

  • Great post …..
    thanks for posting, keep posting

  • Thank you John, as i was facing this problem from many a days, i try to solve this, but its was not on that track where u make us understand….

  • nice, i can see this is very effective & helping, adam got solution from this, i am looking to get some good info about new windows format,

  • great post make easy to get come up this solution. i hope to have some of more in the future too

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187