# HG changeset patch # User ymh # Date 1331822914 -3600 # Node ID cb3e869ab252e7f80dbfa03f76f14cbd30573272 # Parent 822676c32474b11b393e130f9eccecdeb85bde2a Add google analytics diff -r 822676c32474 -r cb3e869ab252 web/hdalab/config.py.tmpl --- a/web/hdalab/config.py.tmpl Tue Mar 13 16:16:59 2012 +0100 +++ b/web/hdalab/config.py.tmpl Thu Mar 15 15:48:34 2012 +0100 @@ -55,3 +55,5 @@ # Examples: "http://foo.com/static/admin/", "/static/admin/". ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/' +GOOGLE_ANALYTICS_CODE = None + diff -r 822676c32474 -r cb3e869ab252 web/hdalab/templates/base.html --- a/web/hdalab/templates/base.html Tue Mar 13 16:16:59 2012 +0100 +++ b/web/hdalab/templates/base.html Thu Mar 15 15:48:34 2012 +0100 @@ -1,3 +1,4 @@ +{% load analytics %} @@ -55,6 +56,6 @@ {% endblock %} - + {% analytics %} diff -r 822676c32474 -r cb3e869ab252 web/hdalab/templates/google_analytics/analytics_template.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hdalab/templates/google_analytics/analytics_template.html Thu Mar 15 15:48:34 2012 +0100 @@ -0,0 +1,10 @@ + \ No newline at end of file diff -r 822676c32474 -r cb3e869ab252 web/hdalab/templates/google_analytics/async_analytics_template.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hdalab/templates/google_analytics/async_analytics_template.html Thu Mar 15 15:48:34 2012 +0100 @@ -0,0 +1,6 @@ + \ No newline at end of file diff -r 822676c32474 -r cb3e869ab252 web/hdalab/templatetags/__init__.py diff -r 822676c32474 -r cb3e869ab252 web/hdalab/templatetags/analytics.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/hdalab/templatetags/analytics.py Thu Mar 15 15:48:34 2012 +0100 @@ -0,0 +1,70 @@ +### +# Taken and adapted form django-google-analytics : http:// +# +# +## +from django import template + +from django.template import Context, loader +from django.conf import settings + + +register = template.Library() + +def __clean_token(str, tag_name): + if not (str[0] == str[-1] and str[0] in ('"', "'")): + raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name + return str[1:-1] + +def do_get_analytics(parser, token): + code = None + style = None + tag_name = None + try: + # split_contents() knows not to split quoted strings. + tokens = token.split_contents() + if len(tokens) > 0 : + tag_name = tokens[0] + if len(tokens) > 1 : + style = tokens[1] + if len(tokens) > 2 : + code = tokens[2] + except ValueError: + code = None + + if not code: + code = getattr(settings,'GOOGLE_ANALYTICS_CODE',None) + else: + code = __clean_token(code, tag_name) + + if style: + style = __clean_token(style, tag_name) + else: + style = 'async' + + return AnalyticsNode(code, style) + +class AnalyticsNode(template.Node): + def __init__(self, code=None, style='async'): + self.code = code + self.style = style + + def render(self, context): + if not self.code: + return '' + + if self.style == 'async': + template_path = 'google_analytics/async_analytics_template.html' + else: + template_path = 'google_analytics/analytics_template.html' + + if self.code.strip() != '': + t = loader.get_template(template_path) + c = Context({ + 'analytics_code': self.code, + }) + return t.render(c) + else: + return '' + +register.tag('analytics', do_get_analytics)