Add GoSquared support

This commit is contained in:
Joost Cassee 2011-04-01 22:41:36 +02:00
parent 41c7484495
commit 3a02a73d94
7 changed files with 248 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Version 0.8.0
-------------
* Added support for the GoSquared service.
* Updated Clicky tracking code to use relative URLs.
Version 0.7.0
-------------
* Added support for the Woopra service.

View file

@ -23,6 +23,7 @@ Currently supported services:
* `Clicky`_ traffic analysis
* `Crazy Egg`_ visual click tracking
* `Google Analytics`_ traffic analysis
* `GoSquared`_ traffic monitoring
* `HubSpot`_ inbound marketing
* `KISSinsights`_ feedback surveys
* `KISSmetrics`_ funnel analysis
@ -49,6 +50,7 @@ an issue to discuss your plans.
.. _Clicky: http://getclicky.com/
.. _`Crazy Egg`: http://www.crazyegg.com/
.. _`Google Analytics`: http://www.google.com/analytics/
.. _GoSquared: http://www.gosquared.com/
.. _HubSpot: http://www.hubspot.com/
.. _KISSinsights: http://www.kissinsights.com/
.. _KISSmetrics: http://www.kissmetrics.com/

View file

@ -19,6 +19,7 @@ TAG_MODULES = [
'analytical.clicky',
'analytical.crazy_egg',
'analytical.google_analytics',
'analytical.gosquared',
'analytical.hubspot',
'analytical.kiss_insights',
'analytical.kiss_metrics',

View file

@ -0,0 +1,79 @@
"""
GoSquared template tags and filters.
"""
from __future__ import absolute_import
import re
from django.conf import settings
from django.template import Library, Node, TemplateSyntaxError
from django.utils import simplejson
from analytical.utils import get_identity, get_user_from_context, \
is_internal_ip, disable_html, get_required_setting
TOKEN_RE = re.compile(r'^\S+-\S+-\S+$')
TRACKING_CODE = """
<script type="text/javascript">
var GoSquared={};
%(config)s
(function(w){
function gs(){
w._gstc_lt=+(new Date); var d=document;
var g = d.createElement("script"); g.type = "text/javascript"; g.async = true; g.src = "//d1l6p2sc9645hc.cloudfront.net/tracker.js";
var s = d.getElementsByTagName("script")[0]; s.parentNode.insertBefore(g, s);
}
w.addEventListener?w.addEventListener("load",gs,false):w.attachEvent("onload",gs);
})(window);
</script>
"""
TOKEN_CODE = 'GoSquared.acct = "%s";'
IDENTIFY_CODE = 'GoSquared.UserName = "%s";'
register = Library()
@register.tag
def gosquared(parser, token):
"""
GoSquared tracking template tag.
Renders Javascript code to track page visits. You must supply
your GoSquared site token in the ``GOSQUARED_SITE_TOKEN`` setting.
"""
bits = token.split_contents()
if len(bits) > 1:
raise TemplateSyntaxError("'%s' takes no arguments" % bits[0])
return GoSquaredNode()
class GoSquaredNode(Node):
def __init__(self):
self.site_token = get_required_setting('GOSQUARED_SITE_TOKEN', TOKEN_RE,
"must be a string looking like XXX-XXXXXX-X")
def render(self, context):
configs = [TOKEN_CODE % self.site_token]
identity = get_identity(context, 'gosquared', self._identify)
if identity:
configs.append(IDENTIFY_CODE % identity)
html = TRACKING_CODE % {
'token': self.site_token,
'config': ' '.join(configs),
}
if is_internal_ip(context, 'GOSQUARED'):
html = disable_html(html, 'GoSquared')
return html
def _identify(self, user):
name = user.get_full_name()
if not name:
name = user.username
return name
def contribute_to_analytical(add_node):
GoSquaredNode() # ensure properly configured
add_node('body_bottom', GoSquaredNode)

View file

@ -7,6 +7,7 @@ from analytical.tests.test_tag_chartbeat import *
from analytical.tests.test_tag_clicky import *
from analytical.tests.test_tag_crazy_egg import *
from analytical.tests.test_tag_google_analytics import *
from analytical.tests.test_tag_gosquared import *
from analytical.tests.test_tag_hubspot import *
from analytical.tests.test_tag_kiss_insights import *
from analytical.tests.test_tag_kiss_metrics import *

View file

@ -0,0 +1,61 @@
"""
Tests for the GoSquared template tags and filters.
"""
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.template import Context
from analytical.templatetags.gosquared import GoSquaredNode
from analytical.tests.utils import TagTestCase
from analytical.utils import AnalyticalException
class GoSquaredTagTestCase(TagTestCase):
"""
Tests for the ``gosquared`` template tag.
"""
def setUp(self):
super(GoSquaredTagTestCase, self).setUp()
self.settings_manager.set(GOSQUARED_SITE_TOKEN='ABC-123456-D')
def test_tag(self):
r = self.render_tag('gosquared', 'gosquared')
self.assertTrue('GoSquared.acct = "ABC-123456-D";' in r, r)
def test_node(self):
r = GoSquaredNode().render(Context({}))
self.assertTrue('GoSquared.acct = "ABC-123456-D";' in r, r)
def test_no_token(self):
self.settings_manager.delete('GOSQUARED_SITE_TOKEN')
self.assertRaises(AnalyticalException, GoSquaredNode)
def test_wrong_token(self):
self.settings_manager.set(GOSQUARED_SITE_TOKEN='this is not a token')
self.assertRaises(AnalyticalException, GoSquaredNode)
def test_auto_identify(self):
self.settings_manager.set(ANALYTICAL_AUTO_IDENTIFY=True)
r = GoSquaredNode().render(Context({'user': User(username='test',
first_name='Test', last_name='User')}))
self.assertTrue('GoSquared.UserName = "Test User";' in r, r)
def test_manual_identify(self):
self.settings_manager.set(ANALYTICAL_AUTO_IDENTIFY=True)
r = GoSquaredNode().render(Context({
'user': User(username='test', first_name='Test', last_name='User'),
'gosquared_identity': 'test_identity',
}))
self.assertTrue('GoSquared.UserName = "test_identity";' in r, r)
def test_render_internal_ip(self):
self.settings_manager.set(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
req = HttpRequest()
req.META['REMOTE_ADDR'] = '1.1.1.1'
context = Context({'request': req})
r = GoSquaredNode().render(context)
self.assertTrue(r.startswith(
'<!-- GoSquared disabled on internal IP address'), r)
self.assertTrue(r.endswith('-->'), r)

View file

@ -0,0 +1,99 @@
===============================
GoSquared -- traffic monitoring
===============================
GoSquared_ provides both real-time traffic monitoring and and trends.
It tells you what is currently happening at your website, what is
popular, locate and identify visitors and track twitter.
.. _GoSquared: http://www.gosquared.com/
Installation
============
To start using the GoSquared integration, you must have installed the
django-analytical package and have added the ``analytical`` application
to :const:`INSTALLED_APPS` in your project :file:`settings.py` file.
See :doc:`../install` for details.
Next you need to add the GoSquared template tag to your templates. This
step is only needed if you are not using the generic
:ttag:`analytical.*` tags. If you are, skip to
:ref:`gosquared-configuration`.
The GoSquared tracking code is inserted into templates using a template
tag. Load the :mod:`gosquared` template tag library and insert the
:ttag:`gosquared` tag. Because every page that you want to track must
have the tag, it is useful to add it to your base template. Insert
the tag at the bottom of the HTML body::
{% load gosquared %}
...
{% gosquared %}
</body>
</html>
.. _gosquared-configuration:
Configuration
=============
When you set up a website to be tracked by GoSquared, it assigns the
site a token. You can find the token on the *Tracking Code* tab of your
website settings page. Set :const:`GOSQUARED_SITE_TOKEN` in the project
:file:`settings.py` file::
GOSQUARED_SITE_TOKEN = 'XXX-XXXXXX-X'
If you do not set a site token, the tracking code will not be rendered.
Internal IP addresses
---------------------
Usually you do not want to track clicks from your development or
internal IP addresses. By default, if the tags detect that the client
comes from any address in the :const:`GOSQUARED_INTERNAL_IPS` setting,
the tracking code is commented out. It takes the value of
:const:`ANALYTICAL_INTERNAL_IPS` by default (which in turn is
:const:`INTERNAL_IPS` by default). See :ref:`identifying-visitors` for
important information about detecting the visitor IP address.
Identifying authenticated users
-------------------------------
If your websites identifies visitors, you can pass this information on
to GoSquared to display on the LiveStats dashboard. By default, the
name of an authenticated user is passed to GoSquared automatically. See
:ref:`identifying-visitors`.
You can also send the visitor identity yourself by adding either the
``gosquared_identity`` or the ``analytical_identity`` variable to
the template context. If both variables are set, the former takes
precedence. For example::
context = RequestContext({'gosquared_identity': identity})
return some_template.render(context)
If you can derive the identity from the HTTP request, you can also use
a context processor that you add to the
:data:`TEMPLATE_CONTEXT_PROCESSORS` list in :file:`settings.py`::
def identify(request):
try:
return {'gosquared_identity': request.user.username}
except AttributeError:
return {}
Just remember that if you set the same context variable in the
:class:`~django.template.context.RequestContext` constructor and in a
context processor, the latter clobbers the former.
----
Thanks go to GoSquared for their support with the development of this
application.