From: Chris Adams Date: Wed, 2 Dec 2009 22:47:08 +0000 (+0800) Subject: Better GA init code X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ef5d5096a6b1e30be26bbc26eb6caee02822992c;p=django-google-tools.git 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. --- 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