From 8301c20c7be028b8a9721a13fa41c8dc7d18f996 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Mon, 28 Dec 2020 10:42:46 +0100 Subject: [PATCH] Respect doNotTrack setting --- js/h5p-x-api-event.js | 52 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/js/h5p-x-api-event.js b/js/h5p-x-api-event.js index a26b58b..1064e4b 100644 --- a/js/h5p-x-api-event.js +++ b/js/h5p-x-api-event.js @@ -191,6 +191,29 @@ H5P.XAPIEvent.prototype.setContext = function (instance) { * Set the actor. Email and name will be added automatically. */ H5P.XAPIEvent.prototype.setActor = function () { + + /* + * Check for doNotTrack setting by user. Could be placed in h5p.js. + * @param {boolean} [strict=true] If True, will assume "do no track" if unset. + * @return {boolean} True, if user should not be tracked. + */ + const isDoNotTrackSet = function (strict) { + strict = (typeof strict === 'boolean') ? strict : true; + + // User explicitly does not want to be tracked + if (window.doNotTrack === '1' || navigator.doNotTrack === 'yes' || navigator.doNotTrack === '1') { + return true; + } + + // Strict and user/browser did not specify doNotTrack setting + if (strict && !(window.doNotTrack || navigator.doNotTrack)) { + return true; + } + + // Not strict or user explicitly allowed tracking + return false; + } + if (H5PIntegration.user !== undefined) { this.data.statement.actor = { 'name': H5PIntegration.user.name, @@ -200,19 +223,26 @@ H5P.XAPIEvent.prototype.setActor = function () { } else { var uuid; - try { - if (localStorage.H5PUserUUID) { - uuid = localStorage.H5PUserUUID; - } - else { - uuid = H5P.createUUID(); - localStorage.H5PUserUUID = uuid; - } - } - catch (err) { - // LocalStorage and Cookies are probably disabled. Do not track the user. + + if (isDoNotTrackSet()) { uuid = 'not-trackable-' + H5P.createUUID(); } + else { + try { + if (localStorage.H5PUserUUID) { + uuid = localStorage.H5PUserUUID; + } + else { + uuid = H5P.createUUID(); + localStorage.H5PUserUUID = uuid; + } + } + catch (err) { + // LocalStorage and Cookies are probably disabled. Do not track the user. + uuid = 'not-trackable-' + H5P.createUUID(); + } + } + this.data.statement.actor = { 'account': { 'name': uuid,