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.
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 ###
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
--- /dev/null
+<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>
<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
except ValueError:
code = None
+ template_name = 'google_analytics/%s_template.html' % tag_name
if not code:
current_site = Site.objects.get_current()
else:
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 = ''
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,
})
return ''
register.tag('analytics', do_get_analytics)
+register.tag('analytics_async', do_get_analytics)