Permalink
Browse files

Initial commit of the project.

  • Loading branch information...
0 parents commit 10c2795494e8b791c3b87295a71d95adedd07365 Clint Ecker committed Jul 7, 2008
No changes.
@@ -0,0 +1,9 @@
+from django.db import models
+from django.contrib.sites.models import Site
+
+class Analytics(models.Model):
+ site = models.ForeignKey(Site, edit_inline=models.TABULAR, max_num_in_admin=1, min_num_in_admin=1)
+ analytics_code = models.CharField(blank=True, max_length=100, core=True)
+
+ def __unicode__(self):
+ return u"%s" % (self.analytics_code)
@@ -0,0 +1,9 @@
+<script type="text/javascript">
+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 changes.
@@ -0,0 +1,48 @@
+from django import template
+from django.db import models
+from django.contrib.sites.models import Site
+
+from django.template import Context, loader
+
+
+register = template.Library()
+Analytics = models.get_model('googleanalytics', 'analytics')
+
+def do_get_analytics(parser, token):
+ try:
+ # split_contents() knows not to split quoted strings.
+ tag_name, code = token.split_contents()
+ except ValueError:
+ code = None
+
+ if not code:
+ print "No code, grabbing from sites"
+ current_site = Site.objects.get_current()
+ else:
+ if not (code[0] == code[-1] and code[0] in ('"', "'")):
+ 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)
+
+class AnalyticsNode(template.Node):
+ def __init__(self, site=None, code=None):
+ self.site = site
+ self.code = code
+
+ def render(self, context):
+ content = ''
+ if self.site:
+ code = self.site.analytics_set.all()[0].analytics_code
+ elif self.code:
+ code = self.code
+ else:
+ return ''
+
+ t = loader.get_template('google_analytics/analytics_template.html')
+ c = Context({
+ 'analytics_code': code,
+ })
+ return t.render(c)
+
+register.tag('analytics', do_get_analytics)

0 comments on commit 10c2795

Please sign in to comment.