From 06a9c4160507af05f8ae9da8833db451c40b315b Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Wed, 2 Dec 2009 17:07:09 -0500 Subject: [PATCH] Make new asynchronous google analytics optional. 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 | 20 +++++++++++++++++++ .../analytics_async_template.html | 11 ++++++++++ .../google_analytics/analytics_template.html | 17 +++++++--------- google_analytics/templatetags/analytics.py | 9 ++++++--- 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 google_analytics/templates/google_analytics/analytics_async_template.html diff --git a/README.mdown b/README.mdown index 47d5359..675f90a 100644 --- a/README.mdown +++ b/README.mdown @@ -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 tag, while + the old snippet goes at the end of the 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 index 0000000..2c255c3 --- /dev/null +++ b/google_analytics/templates/google_analytics/analytics_async_template.html @@ -0,0 +1,11 @@ + diff --git a/google_analytics/templates/google_analytics/analytics_template.html b/google_analytics/templates/google_analytics/analytics_template.html index 4bf6b69..5fb5df1 100644 --- a/google_analytics/templates/google_analytics/analytics_template.html +++ b/google_analytics/templates/google_analytics/analytics_template.html @@ -1,12 +1,9 @@ + \ No newline at end of file diff --git a/google_analytics/templatetags/analytics.py b/google_analytics/templatetags/analytics.py index a1b29af..f49eb36 100644 --- a/google_analytics/templatetags/analytics.py +++ b/google_analytics/templatetags/analytics.py @@ -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) -- 2.39.5