Skip to content

Commit

Permalink
Allow setting properties in context; Document properties and events.
Browse files Browse the repository at this point in the history
  • Loading branch information
poswald committed Sep 17, 2011
1 parent f24998e commit f85b627
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
9 changes: 7 additions & 2 deletions AUTHORS.rst
@@ -1,5 +1,9 @@
The django-analytical package is was written by `Joost Cassee`_, with
contributions by `Eric Davis`_.
The django-analytical package was written by `Joost Cassee`_, with
contributions from:

`Eric Davis`_
`Paul Oswald`_
and others.

Included Javascript code snippets for integration of the analytics
services were written by the respective service providers.
Expand All @@ -13,3 +17,4 @@ The work on Crazy Egg was made possible by `Bateau Knowledge`_.
.. _`Eric Davis`: https://github.com/edavis
.. _Analytical: https://github.com/jkrall/analytical
.. _`Bateau Knowledge`: http://www.bateauknowledge.nl/
.. _`Paul Oswald`: https://github.com/poswald
9 changes: 9 additions & 0 deletions analytical/templatetags/kiss_metrics.py
Expand Up @@ -34,7 +34,10 @@
"""
IDENTIFY_CODE = "_kmq.push(['identify', '%s']);"
EVENT_CODE = "_kmq.push(['record', '%(name)s', %(properties)s]);"
PROPERTY_CODE = "_kmq.push(['set', %(properties)s]);"

EVENT_CONTEXT_KEY = 'kiss_metrics_event'
PROPERTY_CONTEXT_KEY = 'kiss_metrics_property'

register = Library()

Expand Down Expand Up @@ -70,6 +73,12 @@ def render(self, context):
'properties': simplejson.dumps(properties)})
except KeyError:
pass
try:
properties = context[PROPERTY_CONTEXT_KEY]
commands.append(PROPERTY_CODE % {
'properties': simplejson.dumps(properties)})
except KeyError:
pass
html = TRACKING_CODE % {'api_key': self.api_key,
'commands': " ".join(commands)}
if is_internal_ip(context, 'KISS_METRICS'):
Expand Down
39 changes: 38 additions & 1 deletion docs/services/kiss_metrics.rst
Expand Up @@ -9,7 +9,6 @@ many drop out at each stage.

.. _KISSmetrics: http://www.kissmetrics.com/


.. kiss-metrics-installation:
Installation
Expand Down Expand Up @@ -108,3 +107,41 @@ a context processor that you add to the
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.

.. _kiss-metrics-event:

Recording events
----------------

You may tell KISSmetrics about an event by setting a variable in the context.

For example::

context = RequestContext({
'kiss_metrics_event': ['Signed Up', {'Plan' : 'Pro', 'Amount' : 9.99}],
})
return some_template.render(context)

The output script tag will then include the corresponding Javascript event:

_kmq.push(['record', 'Signed Up', {'Plan':'Pro', 'Amount':9.99}]);


.. _kiss-metrics-properties:

Recording properties
--------------------

You may also set KISSmetrics properties without a corresponding event.

For example::

context = RequestContext({
'kiss_metrics_property': {'gender': 'Male'},
})
return some_template.render(context)

The output script tag will then include the corresponding Javascript event:

_kmq.push(['set', {'gender':'Male'}]);

0 comments on commit f85b627

Please sign in to comment.