From d5443c214511e3c6d7b911bf9b7f370a242e54d9 Mon Sep 17 00:00:00 2001 From: Ian Stride Date: Thu, 12 Oct 2023 17:40:46 +0100 Subject: [PATCH 1/6] Create session-based user tracking ID When JavaScript is disabled in the browser, we use the Matomo Image Tracker to gather analytics data. In this case, first party cookies are also disabled and Matomo is not able to differentiate between different visitors very well. The tracking ID is intended to provide the same function as the visitor ID that is usually provided by Matomo when JS is enabled. --- matomo/templates/matomo_tracking_tags.html | 8 ++--- matomo/templatetags/matomo_tags.py | 38 +++++++++++++++++----- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/matomo/templates/matomo_tracking_tags.html b/matomo/templates/matomo_tracking_tags.html index bd8519eae..ec904538e 100644 --- a/matomo/templates/matomo_tracking_tags.html +++ b/matomo/templates/matomo_tracking_tags.html @@ -17,10 +17,10 @@ })(); {% endif %} diff --git a/matomo/templatetags/matomo_tags.py b/matomo/templatetags/matomo_tags.py index 60157d6d3..3e4e0817c 100644 --- a/matomo/templatetags/matomo_tags.py +++ b/matomo/templatetags/matomo_tags.py @@ -1,26 +1,46 @@ +from uuid import uuid4 + from django import template from django.conf import settings - register = template.Library() @register.inclusion_tag("matomo_tracking_tags.html", takes_context=True) def matomo_tracking_tags(context): + server_url = settings.MATOMO_SERVER_URL + site_id = settings.MATOMO_SITE_ID + uid = user_tracking_id(context["request"].session) + context.update( { - "tracking_enabled": ( - settings.MATOMO_TRACKING - and settings.MATOMO_SERVER_URL - and settings.MATOMO_SITE_ID - ), - "matomo_site_id": settings.MATOMO_SITE_ID, - "matomo_server_url": settings.MATOMO_SERVER_URL, + "tracking_enabled": (settings.MATOMO_TRACKING and server_url and site_id), + "matomo_site_id": site_id, + "matomo_server_url": server_url, + "matomo_image_tracker_url": image_tracker(server_url, site_id, uid), } ) additional_site_id = settings.MATOMO_ADDITIONAL_SITE_ID if type(additional_site_id) is int and additional_site_id > 0: - context.update({"matomo_additional_site_id": additional_site_id}) + context.update( + { + "matomo_additional_site_id": additional_site_id, + "matomo_additional_image_tracker_url": image_tracker( + server_url, additional_site_id, uid + ), + } + ) return context + + +def user_tracking_id(session): + if not session.get("tid"): + session["tid"] = str(uuid4()) + + return session["tid"] + + +def image_tracker(server_url, site_id, uid): + return f"{server_url}matomo.php?idsite={site_id}&rec=1&uid={uid}" From 90a1b39649d901859f1dcdf4aaf4f94386fe4065 Mon Sep 17 00:00:00 2001 From: Ian Stride Date: Mon, 16 Oct 2023 14:00:55 +0100 Subject: [PATCH 2/6] Fix tests for Matomo tags --- matomo/tests/test_matomo_tags.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/matomo/tests/test_matomo_tags.py b/matomo/tests/test_matomo_tags.py index fbf9bd7ec..94cd0810e 100644 --- a/matomo/tests/test_matomo_tags.py +++ b/matomo/tests/test_matomo_tags.py @@ -10,26 +10,35 @@ ) class MatomoTagsTests(TestCase): def test_only_enabled_when_required_settings_are_set(self): - self.assertTrue(matomo_tracking_tags(dict()).get("tracking_enabled")) + self.assertTrue(matomo_tracking_tags(create_context()).get("tracking_enabled")) def test_additional_site_id_must_be_positive_integer(self): with override_settings(MATOMO_ADDITIONAL_SITE_ID=567): self.assertEqual( - matomo_tracking_tags(dict()).get("matomo_additional_site_id"), + matomo_tracking_tags(create_context()).get("matomo_additional_site_id"), 567, ) with override_settings(MATOMO_ADDITIONAL_SITE_ID=0): self.assertFalse( - "matomo_additional_site_id" in matomo_tracking_tags(dict()) + "matomo_additional_site_id" in matomo_tracking_tags(create_context()) ) with override_settings(MATOMO_ADDITIONAL_SITE_ID=None): self.assertFalse( - "matomo_additional_site_id" in matomo_tracking_tags(dict()) + "matomo_additional_site_id" in matomo_tracking_tags(create_context()) ) - with override_settings(MATOMO_ADDITIONAL_SITE_ID='123'): + with override_settings(MATOMO_ADDITIONAL_SITE_ID="123"): self.assertFalse( - "matomo_additional_site_id" in matomo_tracking_tags(dict()) + "matomo_additional_site_id" in matomo_tracking_tags(create_context()) ) + + +class MockRequest: + def __init__(self): + self.session = dict() + + +def create_context(): + return {"request": MockRequest()} From 9d51db5b2301d5707a21a46e86c77f61f71894b8 Mon Sep 17 00:00:00 2001 From: Ian Stride Date: Mon, 16 Oct 2023 16:31:27 +0100 Subject: [PATCH 3/6] Set user id on Matomo JavaScript tracker --- matomo/templates/matomo_tracking_tags.html | 2 +- matomo/templatetags/matomo_tags.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/matomo/templates/matomo_tracking_tags.html b/matomo/templates/matomo_tracking_tags.html index ec904538e..d5f0af1b4 100644 --- a/matomo/templates/matomo_tracking_tags.html +++ b/matomo/templates/matomo_tracking_tags.html @@ -2,7 +2,7 @@ {% if tracking_enabled %}