Skip to content

Commit

Permalink
Make matomo custom variables dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
dejafait committed May 15, 2020
1 parent 8b7fa9c commit 229bf08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
6 changes: 3 additions & 3 deletions itou/templates/layout/base.html
Expand Up @@ -240,9 +240,9 @@
var _paq = window._paq || [];
/* Custom variables: https://matomo.org/docs/custom-variables/ */
/* Fields are, in order: index, name, value, scope. */
_paq.push(['setCustomVariable', 1, 'IsAuthenticated', '{{ matomo_custom_var_is_authenticated }}', 'page']);
_paq.push(['setCustomVariable', 2, 'AccountType', '{{ matomo_custom_var_account_type }}', 'page']);
_paq.push(['setCustomVariable', 3, 'AccountSubType', '{{ matomo_custom_var_account_sub_type }}', 'page']);
{% for key, value in matomo_custom_variables.items %}
_paq.push(['setCustomVariable', {{ forloop.counter }}, '{{ key }}', '{{ value }}', 'page']);
{% endfor %}
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
Expand Down
42 changes: 23 additions & 19 deletions itou/utils/perms/context_processors.py
@@ -1,3 +1,5 @@
from collections import OrderedDict

from django.conf import settings
from django.core.exceptions import PermissionDenied

Expand Down Expand Up @@ -53,31 +55,33 @@ def get_current_organization_and_perms(request):


def get_matomo_context(user, prescriber_organization, user_is_siae_admin):
matomo_custom_var_is_authenticated = "yes" if user.is_authenticated else "no"
is_authenticated = "yes" if user.is_authenticated else "no"

if not user.is_authenticated:
matomo_custom_var_account_type = "anonymous"
matomo_custom_var_account_sub_type = "anonymous"
account_type = "anonymous"
account_sub_type = "anonymous"
elif user.is_job_seeker:
matomo_custom_var_account_type = "job_seeker"
matomo_custom_var_account_sub_type = "peconnect" if user.is_peamu else "not_peconnect"
account_type = "job_seeker"
account_sub_type = "peconnect" if user.is_peamu else "not_peconnect"
elif user.is_prescriber:
matomo_custom_var_account_type = "prescriber"
account_type = "prescriber"
if prescriber_organization:
matomo_custom_var_account_sub_type = (
"authorized_org" if prescriber_organization.is_authorized else "unauthorized_org"
)
account_sub_type = "authorized_org" if prescriber_organization.is_authorized else "unauthorized_org"
else:
matomo_custom_var_account_sub_type = "without_org"
account_sub_type = "without_org"
elif user.is_siae_staff:
matomo_custom_var_account_type = "employer"
matomo_custom_var_account_sub_type = "admin" if user_is_siae_admin else "not_admin"
account_type = "employer"
account_sub_type = "admin" if user_is_siae_admin else "not_admin"
else:
matomo_custom_var_account_type = "unknown"
matomo_custom_var_account_sub_type = "unknown"
account_type = "unknown"
account_sub_type = "unknown"

return {
"matomo_custom_var_is_authenticated": matomo_custom_var_is_authenticated,
"matomo_custom_var_account_type": matomo_custom_var_account_type,
"matomo_custom_var_account_sub_type": matomo_custom_var_account_sub_type,
}
matomo_custom_variables = OrderedDict(
[
("is_authenticated", is_authenticated),
("account_type", account_type),
("account_sub_type", account_sub_type),
]
)

return {"matomo_custom_variables": matomo_custom_variables}
23 changes: 14 additions & 9 deletions itou/utils/tests.py
@@ -1,4 +1,5 @@
import datetime
from collections import OrderedDict
from unittest import mock

from django.conf import settings
Expand Down Expand Up @@ -59,9 +60,9 @@ def test_siae_one_membership(self):
"user_is_prescriber_org_admin": False,
"user_is_siae_admin": True,
"user_siae_set": [siae],
"matomo_custom_var_is_authenticated": "yes",
"matomo_custom_var_account_type": "employer",
"matomo_custom_var_account_sub_type": "admin",
"matomo_custom_variables": OrderedDict(
[("is_authenticated", "yes"), ("account_type", "employer"), ("account_sub_type", "admin")]
),
}
self.assertDictEqual(expected, result)

Expand Down Expand Up @@ -95,9 +96,9 @@ def test_siae_multiple_memberships(self):
"user_is_prescriber_org_admin": False,
"user_is_siae_admin": False,
"user_siae_set": [siae1, siae2, siae3],
"matomo_custom_var_is_authenticated": "yes",
"matomo_custom_var_account_type": "employer",
"matomo_custom_var_account_sub_type": "not_admin",
"matomo_custom_variables": OrderedDict(
[("is_authenticated", "yes"), ("account_type", "employer"), ("account_sub_type", "not_admin")]
),
}
self.assertDictEqual(expected, result)

Expand All @@ -123,9 +124,13 @@ def test_prescriber_organization_one_membership(self):
"user_is_prescriber_org_admin": True,
"user_is_siae_admin": False,
"user_siae_set": [],
"matomo_custom_var_is_authenticated": "yes",
"matomo_custom_var_account_type": "prescriber",
"matomo_custom_var_account_sub_type": "unauthorized_org",
"matomo_custom_variables": OrderedDict(
[
("is_authenticated", "yes"),
("account_type", "prescriber"),
("account_sub_type", "unauthorized_org"),
]
),
}
self.assertDictEqual(expected, result)

Expand Down

0 comments on commit 229bf08

Please sign in to comment.