From b2b5a7c840b7b9ac5dc38c6745e883b2e3fbadbe Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 2 Dec 2009 17:47:08 -0500 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 @@ 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