From 3469a9ab15f9ae0035d9ccc9910678e52b635827 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 21 Nov 2023 15:20:35 +0100 Subject: [PATCH] feat: add matomo tracking site Reviewed-by: andriac --- atlas/atlasRoutes.py | 6 ++++++ atlas/configuration/config.py.example | 5 +++++ atlas/configuration/config_schema.py | 18 ++++++++++++++++++ atlas/templates/core/layout.html | 13 +++++++++++++ 4 files changed, 42 insertions(+) diff --git a/atlas/atlasRoutes.py b/atlas/atlasRoutes.py index 21b95163..0958a481 100644 --- a/atlas/atlasRoutes.py +++ b/atlas/atlasRoutes.py @@ -204,6 +204,11 @@ def index(): else: lastDiscoveries = [] + if current_app.config["MATOMO_URL"] != "": + matomo_script = current_app.config["MATOMO_SCRIPT"] + else: + matomo_script = "" + connection.close() session.close() @@ -213,6 +218,7 @@ def index(): mostViewTaxon=mostViewTaxon, customStatMedias=customStatMedias, lastDiscoveries=lastDiscoveries, + matomo_script=matomo_script, ) diff --git a/atlas/configuration/config.py.example b/atlas/configuration/config.py.example index 88c2e0c2..aa99bd54 100644 --- a/atlas/configuration/config.py.example +++ b/atlas/configuration/config.py.example @@ -87,6 +87,11 @@ LANGUAGES = { # Code de suivi des statistiques Google Analytics (si AFFICHAGE_FOOTER = True) ID_GOOGLE_ANALYTICS = "UA-xxxxxxx-xx" +# Suivi de statistiques avec MATOMO si MATOMO_URL != "" +MATOMO_URL = "" # URL lié à votre MATOMO +MATOMO_SITE_ID = 1 # id du site suivi qui correspond ici à celui du GeoNature Atlas (votre site doit être renseigné dans MATOMO) +#### LIEN AIDE MATOMO : https://developer.matomo.org/guides/tracking-javascript-guide + # Utiliser et afficher le glossaire (static/custom/glossaire.json.sample) GLOSSAIRE = False diff --git a/atlas/configuration/config_schema.py b/atlas/configuration/config_schema.py index 068944a7..ea52cadd 100644 --- a/atlas/configuration/config_schema.py +++ b/atlas/configuration/config_schema.py @@ -77,6 +77,21 @@ }, } +# LATEST VERSION OF MATOMO +MATOMO_SCRIPT_TO_INCLUDE = """ + var _paq = _paq || []; + var _paq = window._paq = window._paq || []; + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + (function() { + var u="//url_matomo/"; + _paq.push(['setTrackerUrl', u+'matomo.php']); + _paq.push(['setSiteId', site_id]); + var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; + g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); + })(); +""" + class SecretSchemaConf(Schema): class Meta: @@ -208,6 +223,9 @@ class Meta: # (no need to restart the atlas service when updating templates) # Defaults to False to have the best performance in production TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False) + MATOMO_SCRIPT = fields.String(load_default=MATOMO_SCRIPT_TO_INCLUDE) + MATOMO_URL = fields.String(load_default="") + MATOMO_SITE_ID = fields.Integer(load_default=0) @validates_schema def validate_url_taxhub(self, data, **kwargs): diff --git a/atlas/templates/core/layout.html b/atlas/templates/core/layout.html index 7166804b..84dd4222 100644 --- a/atlas/templates/core/layout.html +++ b/atlas/templates/core/layout.html @@ -15,6 +15,19 @@ {% endblock %} {% block metaTags %}{% endblock %} + + {% if configuration.MATOMO_URL and configuration.MATOMO_SITE_ID is defined %} + {% set site_id = configuration.MATOMO_SITE_ID | int(default=-1) %} + {% set url_matomo = configuration.MATOMO_URL %} + + + + {% endif %} +