diff --git a/analytical/templatetags/matomo.py b/analytical/templatetags/matomo.py index 2509150..4ff6c2e 100644 --- a/analytical/templatetags/matomo.py +++ b/analytical/templatetags/matomo.py @@ -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( diff --git a/docs/services/matomo.rst b/docs/services/matomo.rst index 2aad059..a59394f 100644 --- a/docs/services/matomo.rst +++ b/docs/services/matomo.rst @@ -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 to remove tracking consent + + +.. _`asking for consent`: https://developer.matomo.org/guides/tracking-javascript-guide#asking-for-consent + Internal IP addresses --------------------- diff --git a/tests/unit/test_tag_matomo.py b/tests/unit/test_tag_matomo.py index 0765c12..c6ebd93 100644 --- a/tests/unit/test_tag_matomo.py +++ b/tests/unit/test_tag_matomo.py @@ -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)