Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google_analytics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Analytics(models.Model):

def __unicode__(self):
return u"%s" % (self.analytics_code)

class Meta:
verbose_name_plural = "Analytics"
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<script type="text/javascript">
var _gaq = _gaq || [];
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{{ analytics_code }}']);
_gaq.push(['_trackPageview']);

{% if track_page_load_time %}
_gaq.push(['_trackPageLoadTime']);
_gaq.push(['_trackPageLoadTime']);
{% endif %}

(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);
})();
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>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<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"));
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();
{% if track_page_load_time %}
var pageTracker = _gat._getTracker("{{ analytics_code }}");
pageTracker._initData();
pageTracker._trackPageview();
{% if track_page_load_time %}
pageTracker._trackPageLoadTime();
{% endif %}
{% endif %}
</script>
10 changes: 5 additions & 5 deletions google_analytics/templatetags/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def do_get_analytics(parser, token):
code = None
else:
raise template.TemplateSyntaxError, "%r cannot take more than one argument" % tag_name

if not code:
current_site = Site.objects.get_current()
else:
Expand All @@ -30,13 +30,13 @@ def do_get_analytics(parser, token):
current_site = None

return AnalyticsNode(current_site, code, template_name)

class AnalyticsNode(template.Node):
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 = ''
if self.site:
Expand All @@ -49,7 +49,7 @@ def render(self, context):
code = self.code
else:
return ''

if code.strip() != '':
t = loader.get_template(self.template_name)
c = Context({
Expand All @@ -61,6 +61,6 @@ def render(self, context):
return t.render(c)
else:
return ''

register.tag('analytics', do_get_analytics)
register.tag('analytics_async', do_get_analytics)
20 changes: 9 additions & 11 deletions google_analytics/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django import template
from django.contrib.sites.models import Site
from google_analytics.models import Analytics
from google_analytics.templatetags import analytics
from google_analytics.templatetags import analytic

code7 = 'UA-777777-3' # for fixture-based codes
code9 = 'UA-999999-1' # for explicit codes
Expand Down Expand Up @@ -36,11 +36,11 @@ def _test_null_node_template(self):
def test_noarg_node_template(self):
node = analytics.do_get_analytics(self.parser, self.token_noarg)
self.assertEqual(node.template_name, 'google_analytics/test_template.html')

def test_onearg_node_template(self):
node = analytics.do_get_analytics(self.parser, self.token_onearg)
self.assertEqual(node.template_name, 'google_analytics/test_template.html')

def test_twoarg_node_exception(self):
self.assertRaises(template.TemplateSyntaxError, analytics.do_get_analytics, self.parser, self.token_twoarg)

Expand All @@ -59,7 +59,7 @@ class NodeTest(TestCase):
"""Test set-up and rendering of AnalyticsNodes"""

fixtures = ['analytics_test']

def setUp(self):
self.site = Site.objects.get_current()
self.node_noarg = analytics.AnalyticsNode()
Expand All @@ -74,7 +74,7 @@ def test_fixture(self):

def test_default_template_name(self):
self.assertEqual(
self.node_code.template_name,
self.node_code.template_name,
'google_analytics/analytics_template.html'
)

Expand All @@ -90,12 +90,12 @@ def test_noarg_code_name(self):

def test_explicit_template_name(self):
self.assertEqual(
self.node_explicit_template.template_name,
self.node_explicit_template.template_name,
'google_analytics/test_template.html'
)
self.assertEqual(
self.node_explicit_template.render(template.Context()).strip(),
'Tracking code: %s' % code9
'Tracking code: %s' % code9
)

def test_defined_site(self):
Expand All @@ -107,15 +107,13 @@ def test_defined_site(self):
)

def test_site_overrides_explicit_code(self):
"""If both code and site are set, the site code will override the
"""If both code and site are set, the site code will override the
explicitly set code. This is contrary to how the tag works, but
the parser never passes this combination of arguments."""

self.assertEqual(self.node_code_and_site.code, code9)
self.assertEqual(self.node_code_and_site.site, self.site)
self.assertEqual(
self.node_code_and_site.render(template.Context()).strip(),
'Tracking code: %s' % code7
'Tracking code: %s' % code7
)