--- 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
+
--- 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 %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{{LANGUAGE_CODE}}" lang="{{LANGUAGE_CODE}}">
@@ -55,6 +56,6 @@
</ul>
</div>
{% endblock %}
-
+ {% analytics %}
</body>
</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 @@
+<script type="text/javascript">
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', '{{ analytics_code }}']);
+_gaq.push(['_trackPageview']);
+(function() {
+var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+})();
+</script>
\ No newline at end of file
--- /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 @@
+<script>
+var _gaq=[['_setAccount','{{ analytics_code }}'],['_trackPageview']];
+(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
+g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
+s.parentNode.insertBefore(g,s)}(document,'script'));
+</script>
\ No newline at end of file
--- /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)