Measure twice…


Some time ago I was twiddling with my blog template, when I had the “great” idea of modifying the Google Analytics tracking code such that it checks the successful loading the external script before calling the logging function, to avoid generating errors when the script failed to load (because of NoScript, hosts file entry or other reasons). So I modified it like this (warning! this contains errors!):

<script type="text/javascript">
_uacct = &quot;UA-432874-2&quot; if (urchinTracker) urchinTracker();
</script>

The “&quot;” encoding was needed because the blogger template needs to be valid XML. So what did I miss? The little fact that the two statements (assignment and if) were not separated by “;” or a newline, knocking out my analytics. This is not a big problem, since I’m not very interested in them, but it is still nice to have. Lesson learned (hopefully): measure twice, cut once. Now for the correct code if you wish to insert similar safeguards in your Blogger template:

The “old” style code:

<script src='http://www.google-analytics.com/urchin.js' type='text/javascript'>
</script>
<script type='text/javascript'>
_uacct = &quot;...your tracking code here...&quot;;
if (urchinTracker) urchinTracker();
</script>

The “new” style code:

<script type='text/javascript'>
var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
document.write(unescape(&quot;%3Cscript src='&quot; + gaJsHost + &quot;google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E&quot;));
</script>
<script type='text/javascript'>
if (_gat) {
  var pageTracker = _gat._getTracker(&quot;...your tracking code here...&quot;);
  if (pageTracker) pageTracker._trackPageview();
}
</script>

,

Leave a Reply

Your email address will not be published. Required fields are marked *