Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jcassee/django-analytical
Browse files Browse the repository at this point in the history
  • Loading branch information
jcassee committed Oct 30, 2011
2 parents 128014d + 9e9aa95 commit 31ffca4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
4 changes: 3 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The django-analytical package was written by `Joost Cassee`_, with
contributions from `Eric Davis`_, `Paul Oswald`_ and others.
contributions from `Eric Davis`_, `Paul Oswald`_, `Uros Trebec`_ and
others.

Included Javascript code snippets for integration of the analytics
services were written by the respective service providers.
Expand All @@ -12,5 +13,6 @@ The work on Crazy Egg was made possible by `Bateau Knowledge`_.
.. _`Joost Cassee`: mailto:joost@cassee.net
.. _`Eric Davis`: https://github.com/edavis
.. _`Paul Oswald`: https://github.com/poswald
.. _`Uros Trebec`: https://github.com/failedguidedog
.. _Analytical: https://github.com/jkrall/analytical
.. _`Bateau Knowledge`: http://www.bateauknowledge.nl/
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Version 0.11.0
--------------
* Added support for the Spring Metrics service.
* Allow sending events and properties to KISSmetrics (Paul Oswald).
* Add support for the Site Speed report in Google Analytics (Uros
Trebec).

Version 0.10.0
--------------
Expand All @@ -20,8 +22,8 @@ Version 0.9.1
Version 0.9.0
-------------
* Updated Clicky tracking code to support multiple site ids.
* Fixed Chartbeat auto-domain bug when the Sites framework is not used (Eric
Davis).
* Fixed Chartbeat auto-domain bug when the Sites framework is not used
(Eric Davis).
* Improved testing code (Eric Davis).

Version 0.8.1
Expand Down
11 changes: 9 additions & 2 deletions analytical/templatetags/google_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def enumerate(sequence, start=0):
ALLOW_LINKER_CODE = "_gaq.push(['_setAllowLinker', true]);"
CUSTOM_VAR_CODE = "_gaq.push(['_setCustomVar', %(index)s, '%(name)s', " \
"'%(value)s', %(scope)s]);"
SITE_SPEED_CODE = "_gaq.push(['_trackPageLoadTime']);"


register = Library()
Expand Down Expand Up @@ -76,6 +77,7 @@ def __init__(self):
def render(self, context):
commands = self._get_domain_commands(context)
commands.extend(self._get_custom_var_commands(context))
commands.extend(self._get_other_commands(context))
html = SETUP_CODE % {'property_id': self.property_id,
'commands': " ".join(commands)}
if is_internal_ip(context, 'GOOGLE_ANALYTICS'):
Expand All @@ -91,8 +93,8 @@ def _get_domain_commands(self, context):
else:
domain = get_domain(context, 'google_analytics')
if domain is None:
raise AnalyticalException("tracking multiple domains with Google"
" Analytics requires a domain name")
raise AnalyticalException("tracking multiple domains with"
" Google Analytics requires a domain name")
commands.append(DOMAIN_CODE % domain)
commands.append(NO_ALLOW_HASH_CODE)
if tracking_type == TRACK_MULTIPLE_DOMAINS:
Expand All @@ -114,6 +116,11 @@ def _get_custom_var_commands(self, context):
commands.append(CUSTOM_VAR_CODE % locals())
return commands

def _get_other_commands(self, context):
commands = []
if getattr(settings, 'GOOGLE_ANALYTICS_SITE_SPEED', False):
commands.append(SITE_SPEED_CODE)
return commands

def contribute_to_analytical(add_node):
GoogleAnalyticsNode() # ensure properly configured
Expand Down
22 changes: 18 additions & 4 deletions analytical/tests/test_tag_google_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from django.template import Context

from analytical.templatetags.google_analytics import GoogleAnalyticsNode, \
TRACK_SINGLE_DOMAIN, TRACK_MULTIPLE_DOMAINS, TRACK_MULTIPLE_SUBDOMAINS
TRACK_SINGLE_DOMAIN, TRACK_MULTIPLE_DOMAINS, TRACK_MULTIPLE_SUBDOMAINS,\
SCOPE_VISITOR, SCOPE_SESSION, SCOPE_PAGE
from analytical.tests.utils import TestCase, TagTestCase, override_settings, \
without_apps, SETTING_DELETED
from analytical.utils import AnalyticalException
Expand Down Expand Up @@ -54,14 +55,27 @@ def test_track_multiple_domains(self):
self.assertTrue("_gaq.push(['_setAllowLinker', true]);" in r, r)

def test_custom_vars(self):
context = Context({'google_analytics_var1': ('test1', 'foo'),
'google_analytics_var5': ('test2', 'bar', 1)})
context = Context({
'google_analytics_var1': ('test1', 'foo'),
'google_analytics_var2': ('test2', 'bar', SCOPE_VISITOR),
'google_analytics_var4': ('test4', 'baz', SCOPE_SESSION),
'google_analytics_var5': ('test5', 'qux', SCOPE_PAGE),
})
r = GoogleAnalyticsNode().render(context)
self.assertTrue("_gaq.push(['_setCustomVar', 1, 'test1', 'foo', 3]);"
in r, r)
self.assertTrue("_gaq.push(['_setCustomVar', 5, 'test2', 'bar', 1]);"
self.assertTrue("_gaq.push(['_setCustomVar', 2, 'test2', 'bar', 1]);"
in r, r)
self.assertTrue("_gaq.push(['_setCustomVar', 4, 'test4', 'baz', 2]);"
in r, r)
self.assertTrue("_gaq.push(['_setCustomVar', 5, 'test5', 'qux', 3]);"
in r, r)

@override_settings(GOOGLE_ANALYTICS_SITE_SPEED=True)
def test_track_page_load_time(self):
r = GoogleAnalyticsNode().render(Context())
self.assertTrue("_gaq.push(['_trackPageLoadTime']);" in r, r)

@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
def test_render_internal_ip(self):
req = HttpRequest()
Expand Down
13 changes: 13 additions & 0 deletions docs/services/google_analytics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ or set it in the project :file:`settings.py` file using
:const:`GOOGLE_ANALYTICS_DOMAIN` (fallback: :const:`ANALYTICAL_DOMAIN`).


Tracking site speed
-------------------

You can view page load times in the `Site Speed report`_ by setting the
:const:`GOOGLE_ANALYTICS_SITE_SPEED` configuration setting::

GOOGLE_ANALYTICS_SITE_SPEED = True

By default, page load times are not tracked.

.. _`Site Speed report`: http://www.google.com/support/analyticshelp/bin/answer.py?answer=1205784&topic=1282106


.. _google-analytics-internal-ips:

Internal IP addresses
Expand Down

0 comments on commit 31ffca4

Please sign in to comment.