Added 'ask for consent' logic and documentation to new matomo files

This commit is contained in:
Julian Haluska 2019-06-09 21:46:39 +02:00
parent 4b7bf0d228
commit 845089d4e6
3 changed files with 53 additions and 1 deletions

View file

@ -41,7 +41,31 @@ 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\']);'
DISABLE_COOKIES_CODE = "_paq.push(['disableCookies']);"
GIVE_CONSENT_CLASS = "piwik_give_consent"
REMOVE_CONSENT_CLASS = "piwik_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'
@ -96,6 +120,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(variables_code, (

View file

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

View file

@ -149,6 +149,26 @@ 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:
`piwik_give_consent` - class name for element to click when visitors want to **give** consent
`piwik_remove_consent` - class name for element to click when visitors want to **remove** consent
Examples:
# button to allow tracking
<button class="piwik_give_consent">Track me!</button>
# button to remove tracking consent
<button class="piwik_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
---------------------