From d36699ef0c57e1a03ac96675ae06e887a7191a77 Mon Sep 17 00:00:00 2001 From: Julian Dehm Date: Tue, 2 Jul 2024 16:12:40 +0200 Subject: [PATCH] add matomo integration --- apps/core/static/js/matomo.js | 20 +++++++++++++ apps/core/templates/base.html | 2 ++ apps/core/templates/matomo/tracking_code.html | 4 +++ apps/core/templatetags/core_tags.py | 28 +++++++++++++++++++ changelog/8236.md | 3 ++ webpack.common.js | 3 ++ 6 files changed, 60 insertions(+) create mode 100644 apps/core/static/js/matomo.js create mode 100644 apps/core/templates/matomo/tracking_code.html create mode 100644 changelog/8236.md diff --git a/apps/core/static/js/matomo.js b/apps/core/static/js/matomo.js new file mode 100644 index 00000000..9ced7441 --- /dev/null +++ b/apps/core/static/js/matomo.js @@ -0,0 +1,20 @@ +function initMatomo () { + const matomo = document.getElementById('matomo') + const _paq = window._paq = window._paq || [] + /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + if (matomo.dataset.cookieDisabed) { + _paq.push(['disableCookies']) + } + _paq.push(['trackPageView']) + _paq.push(['enableLinkTracking']) + insertMatomo(_paq, matomo.dataset.url, matomo.dataset.id) +} + +function insertMatomo (_paq, url, id) { + _paq.push(['setTrackerUrl', url + 'matomo.php']) + _paq.push(['setSiteId', id]) + const d = document; const g = d.createElement('script'); const s = d.getElementsByTagName('script')[0] + g.type = 'text/javascript'; g.async = true; g.src = url + 'matomo.js'; s.parentNode.insertBefore(g, s) +} + +initMatomo() diff --git a/apps/core/templates/base.html b/apps/core/templates/base.html index a4c77f76..fd34e66a 100644 --- a/apps/core/templates/base.html +++ b/apps/core/templates/base.html @@ -19,6 +19,8 @@ {# External stylesheets #} + {% matomo_enabled as matomo %} + {% if matomo %} {% matomo_tracking_code %} {% endif %} {% block extra_css %} diff --git a/apps/core/templates/matomo/tracking_code.html b/apps/core/templates/matomo/tracking_code.html new file mode 100644 index 00000000..0d534097 --- /dev/null +++ b/apps/core/templates/matomo/tracking_code.html @@ -0,0 +1,4 @@ +{% load static %} + + + diff --git a/apps/core/templatetags/core_tags.py b/apps/core/templatetags/core_tags.py index 943295e9..5e5b4a3e 100644 --- a/apps/core/templatetags/core_tags.py +++ b/apps/core/templatetags/core_tags.py @@ -1,4 +1,6 @@ from django import template +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured from django.http import Http404 from django.urls import resolve @@ -54,3 +56,29 @@ def file_type(media_file): return "audio/wav" else: return "type invalid" + + +@register.simple_tag() +def matomo_enabled(): + if hasattr(settings, "MATOMO_ENABLED"): + return settings.MATOMO_ENABLED + return False + + +@register.inclusion_tag("matomo/tracking_code.html") +def matomo_tracking_code(): + if not hasattr(settings, "MATOMO_SITE_ID"): + raise ImproperlyConfigured("MATOMO_SITE_ID does not exist.") + + if not hasattr(settings, "MATOMO_URL"): + raise ImproperlyConfigured("MATOMO_URL does not exist.") + + cookie_disabled = True + if hasattr(settings, "MATOMO_COOKIE_DISABLED"): + cookie_disabled = settings.MATOMO_COOKIE_DISABLED + + return { + "id": settings.MATOMO_SITE_ID, + "url": settings.MATOMO_URL, + "cookie_disabled": cookie_disabled, + } diff --git a/changelog/8236.md b/changelog/8236.md new file mode 100644 index 00000000..d604537a --- /dev/null +++ b/changelog/8236.md @@ -0,0 +1,3 @@ +### Added + +- added matomo integration diff --git a/webpack.common.js b/webpack.common.js index 36130b1a..a30cece2 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -13,6 +13,9 @@ module.exports = { all: [ './apps/core/static/scss/all.scss', './apps/core/static/js/app.js' + ], + matomo: [ + './apps/core/static/js/matomo.js' ] },