diff --git a/analytical/templatetags/piwik.py b/analytical/templatetags/piwik.py index 9eebc4a..10819a3 100644 --- a/analytical/templatetags/piwik.py +++ b/analytical/templatetags/piwik.py @@ -42,6 +42,9 @@ TRACKING_CODE = """ VARIABLE_CODE = '_paq.push(["setCustomVariable", %(index)s, "%(name)s", "%(value)s", "%(scope)s"]);' # noqa IDENTITY_CODE = '_paq.push(["setUserId", "%(userid)s"]);' DISABLE_COOKIES_CODE = '_paq.push([\'disableCookies\']);' +ASK_FOR_CONSENT_CODE = '_paq.push([\'requireConsent\']);' +FORGET_CONSENT_CODE = 'document.getElementById("piwik_deny_consent").addEventListener("click", () => { _paq.push(["forgetConsentGiven"]); });' +REMEMBER_CONSENT_CODE = 'document.getElementById("piwik_give_consent").addEventListener("click", () => { _paq.push(["setConsentGiven"]); _paq.push(["rememberConsentGiven"]); });' DEFAULT_SCOPE = 'page' @@ -96,6 +99,11 @@ class PiwikNode(Node): if getattr(settings, 'PIWIK_DISABLE_COOKIES', False): commands.append(DISABLE_COOKIES_CODE) + if getattr(settings, 'PIWIK_ASK_FOR_CONSENT', False): + commands.append(ASK_FOR_CONSENT_CODE) + commands.append(FORGET_CONSENT_CODE) + commands.append(REMEMBER_CONSENT_CODE) + userid = get_identity(context, 'piwik') if userid is not None: variables_code = chain(variables_code, ( diff --git a/analytical/tests/test_tag_piwik.py b/analytical/tests/test_tag_piwik.py index 32661ee..4becce1 100644 --- a/analytical/tests/test_tag_piwik.py +++ b/analytical/tests/test_tag_piwik.py @@ -150,3 +150,8 @@ class PiwikTagTestCase(TagTestCase): def test_disable_cookies(self): r = PiwikNode().render(Context({})) self.assertTrue("_paq.push(['disableCookies']);" in r, r) + + @override_settings(PIWIK_ASK_FOR_CONSENT=True) + def test_disable_cookies(self): + r = PiwikNode().render(Context({})) + self.assertTrue("_paq.push([\'requireConsent\']);" in r, r) diff --git a/docs/services/piwik.rst b/docs/services/piwik.rst index e4c9aee..d012aca 100644 --- a/docs/services/piwik.rst +++ b/docs/services/piwik.rst @@ -145,11 +145,24 @@ set the context variable ``analytical_identity`` (for global configuration) or Disabling cookies ----------------- -If you want to `disable cookies`_, set :data:`PIWIKI_DISABLE_COOKIES` to +If you want to `disable cookies`_, set :data:`PIWIK_DISABLE_COOKIES` to :const:`True`. This is disabled by default. .. _`disable cookies`: https://matomo.org/faq/general/faq_157/ +Ask for consent +----------------- + +If you want to ask for consent set :data:`PIWIK_ASK_FOR_CONSENT` to +:const:`True`. This is disabled by default. + +To ask the visitor for consent just create DOM elements with the following id's: + +`piwik_deny_consent` - id for DOM element to click, if the user denies consent +`piwik_give_consent` - id for DOM element to click, if the user gives consent + +.. _`asking for consent`: https://developer.matomo.org/guides/tracking-javascript-guide#asking-for-consent + Internal IP addresses ---------------------