]> git.parisson.com Git - django-google-tools.git/commitdiff
Make new asynchronous google analytics optional.
authorJ. Cliff Dyer <jcdyer@aalcdl07.(none)>
Wed, 2 Dec 2009 22:07:09 +0000 (17:07 -0500)
committerJ. Cliff Dyer <jcdyer@aalcdl07.(none)>
Wed, 2 Dec 2009 22:07:09 +0000 (17:07 -0500)
The Asynchronous tracking snippet is not exactly a drop it replacement for the old
synchronous tracking snippet.  It is intended to go at a different place in the template,
and it doesn't work with some documents (see README.mdown for details).  Therefore, it
makes sense for it to be rendered using a different template tag.  It could have been an
option passed to the template tag, but template tags should be simple to use, and simple
to parse.

README.mdown
google_analytics/templates/google_analytics/analytics_async_template.html [new file with mode: 0644]
google_analytics/templates/google_analytics/analytics_template.html
google_analytics/templatetags/analytics.py

index 47d53598bc77e286c11af76c33996efe6c7e53b8..675f90ad48e82109561ab2d08ed7366f11ad5ab1 100644 (file)
@@ -12,6 +12,7 @@ be able to manage these Google analytics accounts from the Django admin page.
 I also added a mode of operation that excludes the admin interface altogether 
 (you can just use the template tag)
 
+
 ## Two modes of operation ##
 
 ### Administering and associating codes with Django `Sites` framework ###
@@ -31,6 +32,25 @@ I also added a mode of operation that excludes the admin interface altogether
 2. In your base template, usually a `base.html`, insert this tag at the very top: `{% load analytics %}`
 3. In the same template, insert the following code right before the closing body tag: `{% analytics "UA-xxxxxx-x" %}` the `UA-xxxxxx-x` is a unique Google Analytics code for you domain when you sign up for a new account.
 
+
+## Asynchronous Tracking ##
+
+Google's recent asynchronous tracking API is also supported, as of v0.2.  To use it,
+simply call the templatetag `{% analytics_async %}` in the document head instead
+of calling `{% analytics %}` just before the end of the body.  You can also use
+`{% analytics_async "UA-xxxxxx-x" %}` as with the old templatetags.
+
+This is being added as an option rather than a replacement for two reasons:
+
+1. Google recommends the Asynchronous Tracking snippet goes in the <head> tag, while
+   the old snippet goes at the end of the <body> tag, so as not to disrupt page loading.
+   Therefore it is not a drop in replacement in your template
+2. The new snippet is reported to [break sites that have a comment before the head tag](http://www.stevesouders.com/blog/2009/12/01/google-analytics-goes-async/#comment-1171). 
+   Adding the asynchronous tracking to existing code would cause backwards 
+   incompatiblity.
+
+
+
 ## License ##
 
 The MIT License
diff --git a/google_analytics/templates/google_analytics/analytics_async_template.html b/google_analytics/templates/google_analytics/analytics_async_template.html
new file mode 100644 (file)
index 0000000..2c255c3
--- /dev/null
@@ -0,0 +1,11 @@
+<script type="text/javascript">
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', '{{ analytics_code }}']);
+_gaq.push(['_trackPageview']);
+(function() {
+ 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);})();
+</script>
index 4bf6b697c31b7bdd487366d60a274461afaa0dbc..5fb5df14bbd49f3cee89f85f9f0b0d4498b479e2 100644 (file)
@@ -1,12 +1,9 @@
 <script type="text/javascript">
-var _gaq = _gaq || [];
-_gaq.push(['_setAccount', '{{ analytics_code }}']);
-_gaq.push(['_trackPageview']);
-
-(function() {
-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);
-})();
+var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
+document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+</script>
+<script type="text/javascript">
+var pageTracker = _gat._getTracker("{{ analytics_code }}");
+pageTracker._initData();
+pageTracker._trackPageview();
 </script>
\ No newline at end of file
index a1b29af29a6deedd6f2a5317987e7066243c1b7a..f49eb3660a8bd2d4bc119c372dacba1f9da6a794 100644 (file)
@@ -15,6 +15,7 @@ def do_get_analytics(parser, token):
     except ValueError:
         code = None
    
+    template_name = 'google_analytics/%s_template.html' % tag_name
     if not code:
         current_site = Site.objects.get_current()
     else:
@@ -22,12 +23,13 @@ def do_get_analytics(parser, token):
             raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
         code = code[1:-1]
         current_site = None
-    return AnalyticsNode(current_site, code)
+    return AnalyticsNode(current_site, code, template_name)
     
 class AnalyticsNode(template.Node):
-    def __init__(self, site=None, code=None):
+    def __init__(self, site=None, code=None, template_name='google_analytics/analytics_template.html'):
         self.site = site
         self.code = code
+        self.template_name = template_name
         
     def render(self, context):
         content = ''
@@ -43,7 +45,7 @@ class AnalyticsNode(template.Node):
             return ''
         
         if code.strip() != '':
-            t = loader.get_template('google_analytics/analytics_template.html')
+            t = loader.get_template(self.template_name)
             c = Context({
                 'analytics_code': code,
             })
@@ -52,3 +54,4 @@ class AnalyticsNode(template.Node):
             return ''
         
 register.tag('analytics', do_get_analytics)
+register.tag('analytics_async', do_get_analytics)