mirror of
https://github.com/jazzband/django-analytical.git
synced 2026-05-28 00:24:08 +00:00
Add support for Matomo user commands
This commit is contained in:
parent
37009f2e08
commit
d54ad3e944
3 changed files with 37 additions and 0 deletions
|
|
@ -43,6 +43,7 @@ TRACKING_CODE = """
|
|||
VARIABLE_CODE = '_paq.push(["setCustomVariable", %(index)s, "%(name)s", "%(value)s", "%(scope)s"]);' # noqa
|
||||
IDENTITY_CODE = '_paq.push(["setUserId", "%(userid)s"]);'
|
||||
DISABLE_COOKIES_CODE = '_paq.push([\'disableCookies\']);'
|
||||
CUSTOM_COMMAND_CODE = '_paq.push([%(args)s]);'
|
||||
|
||||
DEFAULT_SCOPE = 'page'
|
||||
|
||||
|
|
@ -86,6 +87,7 @@ class MatomoNode(Node):
|
|||
|
||||
def render(self, context):
|
||||
custom_variables = context.get('matomo_vars', ())
|
||||
custom_commands = context.get('matomo_commands', [])
|
||||
|
||||
complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,)
|
||||
for var in custom_variables)
|
||||
|
|
@ -94,6 +96,12 @@ class MatomoNode(Node):
|
|||
for var in complete_variables)
|
||||
|
||||
commands = []
|
||||
|
||||
for custom_command in custom_commands:
|
||||
args = ', '.join('"%s"' % arg if isinstance(arg, str) else str(arg)
|
||||
for arg in custom_command)
|
||||
commands.append(CUSTOM_COMMAND_CODE % {'args': args})
|
||||
|
||||
if getattr(settings, 'MATOMO_DISABLE_COOKIES', False):
|
||||
commands.append(DISABLE_COOKIES_CODE)
|
||||
|
||||
|
|
|
|||
|
|
@ -109,6 +109,25 @@ in :file:`settings.py`.
|
|||
.. _`custom variables`: http://developer.matomo.org/guides/tracking-javascript-guide#custom-variables
|
||||
|
||||
|
||||
User commands
|
||||
-------------
|
||||
|
||||
Matomo commands that uses ``_paq.push()`` can be passed using context variable
|
||||
``matomo_commands`` similar user variables using a list. ::
|
||||
|
||||
context = Context({
|
||||
'matomo_commands': [['setIgnoreClasses', 'no-tracking'],
|
||||
['setDocumentTitle', 'foobar'],
|
||||
['tackGoal', 1, 1000.01]]
|
||||
})
|
||||
return some_template.render(context)
|
||||
|
||||
This generates following JavaScript code. ::
|
||||
|
||||
_paq.push(["setIgnoreClasses", "no-tracking"]);
|
||||
_paq.push(["setDocumentTitle", "foobar"]);
|
||||
_paq.push(["tackGoal", 1, 1000.01]);
|
||||
|
||||
.. _matomo-user-tracking:
|
||||
|
||||
User tracking
|
||||
|
|
|
|||
|
|
@ -113,6 +113,16 @@ class MatomoTagTestCase(TagTestCase):
|
|||
'_paq.push(["setCustomVariable", 3, "spam", "spam_val", "visit"]);']:
|
||||
assert var_code in r
|
||||
|
||||
def test_user_commands(self):
|
||||
context = Context({'matomo_commands': [['setIgnoreClasses', 'no-tracking'],
|
||||
['setDocumentTitle', 'foobar'],
|
||||
['tackGoal', 1, 1000.01]]})
|
||||
r = MatomoNode().render(context)
|
||||
for var_code in ['_paq.push(["setIgnoreClasses", "no-tracking"]);',
|
||||
'_paq.push(["setDocumentTitle", "foobar"]);',
|
||||
'_paq.push(["tackGoal", 1, 1000.01]);']:
|
||||
assert var_code in r
|
||||
|
||||
@override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
|
||||
def test_default_usertrack(self):
|
||||
context = Context({
|
||||
|
|
|
|||
Loading…
Reference in a new issue