From ef5d5096a6b1e30be26bbc26eb6caee02822992c Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Thu, 3 Dec 2009 06:47:08 +0800 Subject: [PATCH] Better GA init code The official version fails if the document's first child is something like a comment. This tries to attach to and, if that doesn't exist, falls back to body. --- .../templates/google_analytics/analytics_template.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google_analytics/templates/google_analytics/analytics_template.html b/google_analytics/templates/google_analytics/analytics_template.html index 4bf6b69..4cf0288 100644 --- a/google_analytics/templates/google_analytics/analytics_template.html +++ b/google_analytics/templates/google_analytics/analytics_template.html @@ -7,6 +7,9 @@ _gaq.push(['_trackPageview']); var ga = document.createElement('script'); ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; ga.setAttribute('async', 'true'); -document.documentElement.firstChild.appendChild(ga); + +// Avoid problems if the document doesn't actually contain a HEAD element: +var container = document.getElementsByTagName("head")[0] || document.body; +container.appendChild(ga); })(); \ No newline at end of file -- 2.39.5