Monday, September 21, 2009

So I recently added Google Analytics code to this blog...

Mostly to play with the functionality and so on. But a funny thing happened on the way...
You are given a code snippet to add to your page's HTML; a portion of which is copied here:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
So I copied and pasted the above  as is  to the window of the Blogger editor (for editing blog posts). The Blogger editor has two editing modes: Edit HTML  which is supposed to let you directly edit the page's HTML  and Compose  which is more like an MS Word interface to editing a blog post; one can easily bold, italicize, change font, create bullets, and so on in this mode (using buttons rather than specifying HTML markup tags). Then that editor transforms your entered text into HTML.

Because I was given HTML code though (for analytics tracking), I used HTML mode and copied the given JavaScript scripts (as above) directly into the bottom of the page then saved and published. However, some *additional* code was added  by the Blogger editor  to the scripts that I inserted. In particular, XHTML line breaks were inserted within the JavaScript script. So, the above became:

<script type="text/javascript"> <br/>
var gaJsHost = (("https:" == document.location.protocol)? "https://ssl." : "http://www."); <br/>
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
So now I have XHTML line breaks (<br/>) within the analytics tracking scripts, which caused any JavaScript interpreter (parsing the above script) to choke and stop processing the script  and hence not to perform the analytics-related tasks. This explains why I wasn't seeing tracking information for clicks that I was sure had landed on my pages. The fix was simple of course: remove all line breaks in the analytics scripts. Interesting all the same.

(How did I verify that a JavaScript interpreter chokes when it sees <br/> mid-script? Using this sandbox.) (Note also that as far as Google Analytics was concerned, my tracking code was installed properly, which means their checker needs to be fixed.)

Another curiosity is whether I should create separate Analytics "profiles" for each different page of my blog. And whether visiting my blog homepage will cause the analytics tracking scripts (which are inserted within every individual blog post) to *all be executed*. That is, I added analytics tracking scripts to each individual blog post. And when you visit my blog, you automatically see all posts. So presumably, every time I get a visitor to my homepage, all N analytics scripts will execute (where N is the number of blog posts) and so it will seem as if I've had N * x visitors (where x is the number of people who visit

No comments:

Post a Comment