From 73b3ed3b154af1df096c4d15cf1746c318d79569 Mon Sep 17 00:00:00 2001 From: Fabien Le Frapper Date: Thu, 11 Sep 2025 12:44:04 +0200 Subject: [PATCH 1/7] Factorise analytics / add back matomo on carte / formulaire --- core/context_processors.py | 1 + core/settings.py | 1 + core/templatetags/analytics_tags.py | 13 +++++++++++++ qfdmd/templatetags/qfdmd_tags.py | 9 --------- templates/{tracking => analytics}/matomo.html | 0 templates/analytics/posthog_data_attributes.html | 8 ++++++++ templates/layout/base.html | 14 ++++---------- templates/qfdmd/base.html | 14 +++----------- 8 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 core/templatetags/analytics_tags.py rename templates/{tracking => analytics}/matomo.html (100%) create mode 100644 templates/analytics/posthog_data_attributes.html diff --git a/core/context_processors.py b/core/context_processors.py index dba9553f2..136ff6a70 100644 --- a/core/context_processors.py +++ b/core/context_processors.py @@ -38,6 +38,7 @@ def global_context(request) -> dict: "DECLARATION_ACCESSIBILITE_PAGE_ID": settings.LVAO[ "DECLARATION_ACCESSIBILITE_PAGE_ID" ], + "MATOMO_ID": settings.LVAO["MATOMO_ID"], }, } diff --git a/core/settings.py b/core/settings.py index d9ebee7d0..251920042 100644 --- a/core/settings.py +++ b/core/settings.py @@ -39,6 +39,7 @@ # Carte settings # -------------- LVAO = { + "MATOMO_ID": decouple.config("LVAO_MATOMO_ID", default=50, cast=int), "GOOGLE_SEARCH_CONSOLE": "google9dfbbc61adbe3888.html", "POSTHOG_KEY": decouple.config( "LVAO_POSTHOG_KEY", diff --git a/core/templatetags/analytics_tags.py b/core/templatetags/analytics_tags.py new file mode 100644 index 000000000..9d518054e --- /dev/null +++ b/core/templatetags/analytics_tags.py @@ -0,0 +1,13 @@ +from django import template + +register = template.Library() + + +@register.inclusion_tag("analytics/matomo.html") +def matomo(id): + return {"matomo_url": "stats.beta.gouv.fr", "matomo_id": id} + + +@register.inclusion_tag("analytics/posthog_data_attributes.html") +def posthog_data_attributes(key): + return {"key": key} diff --git a/qfdmd/templatetags/qfdmd_tags.py b/qfdmd/templatetags/qfdmd_tags.py index 54a39f95d..9da1bb6e7 100644 --- a/qfdmd/templatetags/qfdmd_tags.py +++ b/qfdmd/templatetags/qfdmd_tags.py @@ -2,7 +2,6 @@ from typing import cast from django import template -from django.conf import settings from django.core.cache import cache from django.forms import FileField from django.utils.safestring import mark_safe @@ -100,11 +99,3 @@ def carte(context, carte_config: CarteConfig) -> dict: @register.inclusion_tag("head/favicon.html") def favicon() -> dict: return {} - - -@register.inclusion_tag("tracking/matomo.html") -def matomo(): - return { - "matomo_url": "stats.beta.gouv.fr", - "matomo_id": settings.ASSISTANT["MATOMO_ID"], - } diff --git a/templates/tracking/matomo.html b/templates/analytics/matomo.html similarity index 100% rename from templates/tracking/matomo.html rename to templates/analytics/matomo.html diff --git a/templates/analytics/posthog_data_attributes.html b/templates/analytics/posthog_data_attributes.html new file mode 100644 index 000000000..24dc3d745 --- /dev/null +++ b/templates/analytics/posthog_data_attributes.html @@ -0,0 +1,8 @@ +data-analytics-posthog-key-value="{{ key }}" +data-analytics-posthog-debug-value="{{ POSTHOG_DEBUG|lower }}" +{% if request.user.is_authenticated %} + data-analytics-user-username-value="{{ request.user.username }}" + data-analytics-user-email-value="{{ request.user.email }}" + data-analytics-user-admin-value="{{ request.user.is_superuser|lower }}" +{% endif %} +data-analytics-initial-action-value="{% block analytics_action %}{% endblock %}" diff --git a/templates/layout/base.html b/templates/layout/base.html index 10bcec77b..5bcc3a7a1 100644 --- a/templates/layout/base.html +++ b/templates/layout/base.html @@ -1,4 +1,4 @@ -{% load qfdmo_tags static %} +{% load analytics_tags qfdmd_tags qfdmo_tags static %} {% block javascript_extras %}{% endblock %} @@ -48,15 +50,7 @@ data-state-search-solution-form-outlet="[data-controller='search-solution-form']" data-action="address-autocomplete:change->state#setLocation" {# Analytics #} - {# TODO: factorize with django-templating-language template as soon as jinja2 will be removed #} - data-analytics-posthog-key-value="{{ lvao.POSTHOG_KEY }}" - data-analytics-posthog-debug-value="{{ POSTHOG_DEBUG|lower }}" - {% if request.user.is_authenticated %} - data-analytics-user-username-value="{{ request.user.username }}" - data-analytics-user-email-value="{{ request.user.email }}" - data-analytics-user-admin-value="{{ request.user.is_superuser|lower }}" - {% endif %} - data-analytics-initial-action-value="{% block analytics_action %}{% endblock %}" + {% posthog_data_attributes assistant.POSTHOG_KEY %} >