]> git.parisson.com Git - django-google-tools.git/commitdiff
Better GA init code
authorChris Adams <chris.adams@nasa.gov>
Wed, 2 Dec 2009 22:47:08 +0000 (06:47 +0800)
committerClint Ecker <clint@arstechnica.com>
Fri, 4 Dec 2009 22:08:45 +0000 (06:08 +0800)
The official version fails if the document's first child is something
like a comment. This tries to attach to <head> and, if that doesn't
exist, falls back to body.

google_analytics/templates/google_analytics/analytics_template.html

index 4bf6b697c31b7bdd487366d60a274461afaa0dbc..4cf0288e1c74ffabdaa3216fefa496592e22f4a2 100644 (file)
@@ -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);
 })();
 </script>
\ No newline at end of file