Ask for consent - Matomo (Continuation Jayhaluska's work) (#245)
Some checks failed
Check / build (audit) (push) Has been cancelled
Check / build (docs) (push) Has been cancelled
Check / build (format) (push) Has been cancelled
Check / build (lint) (push) Has been cancelled
Check / build (package) (push) Has been cancelled
Test / python-django (4.2, 3.10) (push) Has been cancelled
Test / python-django (4.2, 3.11) (push) Has been cancelled
Test / python-django (4.2, 3.12) (push) Has been cancelled
Test / python-django (4.2, 3.9) (push) Has been cancelled
Test / python-django (5.1, 3.10) (push) Has been cancelled
Test / python-django (5.1, 3.11) (push) Has been cancelled
Test / python-django (5.1, 3.12) (push) Has been cancelled
Test / python-django (5.1, 3.13) (push) Has been cancelled
Test / python-django (5.2, 3.10) (push) Has been cancelled
Test / python-django (5.2, 3.11) (push) Has been cancelled
Test / python-django (5.2, 3.12) (push) Has been cancelled
Test / python-django (5.2, 3.13) (push) Has been cancelled

Co-authored-by: Julian Haluska <j.haluska@gmx.de>
This commit is contained in:
Ronard 2026-03-08 12:10:32 -04:00 committed by GitHub
parent 694fc9097a
commit e6f12719cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 57 additions and 0 deletions

View file

@ -3,6 +3,7 @@
* Fix GA gtag user_id setup and add support for custom dimensions (Erick Massip)
* Change spelling of "JavaScript" across all files in docstrings and docs
(Peter Bittner)
* Ask site visitors for consent when using Matomo (Julian Haluska & Ronard Luna)
Version 3.2.0
-------------

View file

@ -46,6 +46,30 @@ VARIABLE_CODE = (
IDENTITY_CODE = '_paq.push(["setUserId", "%(userid)s"]);'
DISABLE_COOKIES_CODE = "_paq.push(['disableCookies']);"
GIVE_CONSENT_CLASS = 'matomo_give_consent'
REMOVE_CONSENT_CLASS = 'matomo_remove_consent'
ASK_FOR_CONSENT_CODE = """
_paq.push(['requireConsent']);
var elements = document.getElementsByClassName("{}");
for (var i = 0; i < elements.length; i++) {{
elements[i].addEventListener("click",
function () {{
_paq.push(["forgetConsentGiven"]);
}}
);
}}
var elements = document.getElementsByClassName("{}");
for (var i = 0; i < elements.length; i++) {{
elements[i].addEventListener("click",
function () {{
_paq.push(["rememberConsentGiven"]);
}}
);
}}
""".format(REMOVE_CONSENT_CLASS, GIVE_CONSENT_CLASS)
DEFAULT_SCOPE = 'page'
MatomoVar = namedtuple('MatomoVar', ('index', 'name', 'value', 'scope'))
@ -103,6 +127,9 @@ class MatomoNode(Node):
if getattr(settings, 'MATOMO_DISABLE_COOKIES', False):
commands.append(DISABLE_COOKIES_CODE)
if getattr(settings, 'MATOMO_ASK_FOR_CONSENT', False):
commands.append(ASK_FOR_CONSENT_CODE)
userid = get_identity(context, 'matomo')
if userid is not None:
variables_code = chain(

View file

@ -149,6 +149,28 @@ If you want to `disable cookies`_, set :data:`MATOMO_DISABLE_COOKIES` to
.. _`disable cookies`: https://matomo.org/faq/general/faq_157/
Ask for consent
---------------
If you do not want to track visitors without permission, you can `ask for consent`_ first.
To enable this, set :data:`MATOMO_ASK_FOR_CONSENT` to :const:`True`.
By default, no consent for tracking is needed (i.e. :const:`False`).
To give and remove consent in your page, create DOM elements with the following classes:
`matomo_give_consent` - class name for element to click when visitors want to **give** consent
`matomo_remove_consent` - class name for element to click when visitors want to **remove** consent
Examples::
# button to allow tracking
<button class="matomo_give_consent">Track me!</button>
# button to remove tracking consent
<button class="matomo_remove_consent">Don't track me anymore!</button>
.. _`asking for consent`: https://developer.matomo.org/guides/tracking-javascript-guide#asking-for-consent
Internal IP addresses
---------------------

View file

@ -59,6 +59,8 @@ authors = [
{name = "Tinnet Coronam", email = "tinnet@coronam.net"},
{name = "Uros Trebec", email = "uros@trebec.org"},
{name = "Walter Renner", email = "walter.renner@me.com"},
{name = "Julian Haluska", email = "mail@julianhaluska.de"},
{name = "Ronard Luna", email = "rlunag@proton.me"},
]
maintainers = [
{name = "Jazzband community", email = "jazzband-bot@users.noreply.github.com"},

View file

@ -159,3 +159,8 @@ class MatomoTagTestCase(TagTestCase):
def test_disable_cookies(self):
r = MatomoNode().render(Context({}))
assert "_paq.push(['disableCookies']);" in r
@override_settings(MATOMO_ASK_FOR_CONSENT=True)
def test_ask_for_consent(self):
r = MatomoNode().render(Context({}))
self.assertTrue("_paq.push(['requireConsent']);" in r, r)