Sponsored Links:
Google’s Invalid New Tracking Code
Google recently came out with a new tracking code for its Analytics service. It has a cool new feature, but validators also choke on it.
Let’s Fix the Code
Virtually all browsers in use today understand that they should ignore what’s in between <script> tags. For the most part, we don’t have to insert comment tags <!-- --> any more. Unfortunately, most validators still choke on what they find in between script tags. So technically, you could leave comment tags out, but in practice it’s very annoying to have the validator spitting back a big pile of errors.
To “fix” Google’s tracking code (which in theory should be perfectly fine) so that it doesn’t result in validation errors, simply add comments around the contents of the script tag, like so:
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”><!--
var pageTracker = _gat._getTracker(”UA-0000000-0″);
pageTracker._initData();
pageTracker._trackPageview();
--></script>
A Cool New Feature
The nicest feature of the new Google Analytics tracking code is that it has Javascript code that automatically detects whether the page the code is on uses a secure, https:// connection or a normal, http:// connection. No more having to write your own detection code to include a different script.
Personally, I think adding comment tags is a small price to pay to get a tracking code that validates and automatically detects the transfer protocol. Thank you, Google.



