-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathgdpr-utils.js
300 lines (271 loc) · 14.1 KB
/
gdpr-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* GDPR utils
*
* The General Data Protection Regulation (GDPR) is a regulation in EU law on data protection
* and privacy for all individuals within the European Union. It addresses the export of personal
* data outside the EU. The GDPR aims primarily to give control back to citizens and residents
* over their personal data and to simplify the regulatory environment for international business
* by unifying the regulation within the EU.
*
* This set of utilities is intended to enable opt in/out functionality in the Mixpanel JS SDK.
* These functions are used internally by the SDK and are not intended to be publicly exposed.
*/
import { _, console, window } from './utils';
/**
* A function used to track a Mixpanel event (e.g. MixpanelLib.track)
* @callback trackFunction
* @param {String} event_name The name of the event. This can be anything the user does - 'Button Click', 'Sign Up', 'Item Purchased', etc.
* @param {Object} [properties] A set of properties to include with the event you're sending. These describe the user who did the event or details about the event itself.
* @param {Function} [callback] If provided, the callback function will be called after tracking the event.
*/
/** Public **/
var GDPR_DEFAULT_PERSISTENCE_PREFIX = '__mp_opt_in_out_';
/**
* Opt the user in to data tracking and cookies/localstorage for the given token
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {trackFunction} [options.track] - function used for tracking a Mixpanel event to record the opt-in action
* @param {string} [options.trackEventName] - event name to be used for tracking the opt-in action
* @param {Object} [options.trackProperties] - set of properties to be tracked along with the opt-in action
* @param {string} [options.persistenceType] Persistence mechanism used - cookie or localStorage
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @param {Number} [options.cookieExpiration] - number of days until the opt-in cookie expires
* @param {string} [options.cookieDomain] - custom cookie domain
* @param {boolean} [options.crossSiteCookie] - whether the opt-in cookie is set as cross-site-enabled
* @param {boolean} [options.crossSubdomainCookie] - whether the opt-in cookie is set as cross-subdomain or not
* @param {boolean} [options.secureCookie] - whether the opt-in cookie is set as secure or not
*/
export function optIn(token, options) {
_optInOut(true, token, options);
}
/**
* Opt the user out of data tracking and cookies/localstorage for the given token
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistenceType] Persistence mechanism used - cookie or localStorage
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @param {Number} [options.cookieExpiration] - number of days until the opt-out cookie expires
* @param {string} [options.cookieDomain] - custom cookie domain
* @param {boolean} [options.crossSiteCookie] - whether the opt-in cookie is set as cross-site-enabled
* @param {boolean} [options.crossSubdomainCookie] - whether the opt-out cookie is set as cross-subdomain or not
* @param {boolean} [options.secureCookie] - whether the opt-out cookie is set as secure or not
*/
export function optOut(token, options) {
_optInOut(false, token, options);
}
/**
* Check whether the user has opted in to data tracking and cookies/localstorage for the given token
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistenceType] Persistence mechanism used - cookie or localStorage
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @returns {boolean} whether the user has opted in to the given opt type
*/
export function hasOptedIn(token, options) {
return _getStorageValue(token, options) === '1';
}
/**
* Check whether the user has opted out of data tracking and cookies/localstorage for the given token
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistenceType] Persistence mechanism used - cookie or localStorage
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @param {boolean} [options.ignoreDnt] - flag to ignore browser DNT settings and always return false
* @returns {boolean} whether the user has opted out of the given opt type
*/
export function hasOptedOut(token, options) {
if (_hasDoNotTrackFlagOn(options)) {
console.warn('This browser has "Do Not Track" enabled. This will prevent the Mixpanel SDK from sending any data. To ignore the "Do Not Track" browser setting, initialize the Mixpanel instance with the config "ignore_dnt: true"');
return true;
}
var optedOut = _getStorageValue(token, options) === '0';
if (optedOut) {
console.warn('You are opted out of Mixpanel tracking. This will prevent the Mixpanel SDK from sending any data.');
}
return optedOut;
}
/**
* Wrap a MixpanelLib method with a check for whether the user is opted out of data tracking and cookies/localstorage for the given token
* If the user has opted out, return early instead of executing the method.
* If a callback argument was provided, execute it passing the 0 error code.
* @param {function} method - wrapped method to be executed if the user has not opted out
* @returns {*} the result of executing method OR undefined if the user has opted out
*/
export function addOptOutCheckMixpanelLib(method) {
return _addOptOutCheck(method, function(name) {
return this.get_config(name);
});
}
/**
* Wrap a MixpanelPeople method with a check for whether the user is opted out of data tracking and cookies/localstorage for the given token
* If the user has opted out, return early instead of executing the method.
* If a callback argument was provided, execute it passing the 0 error code.
* @param {function} method - wrapped method to be executed if the user has not opted out
* @returns {*} the result of executing method OR undefined if the user has opted out
*/
export function addOptOutCheckMixpanelPeople(method) {
return _addOptOutCheck(method, function(name) {
return this._get_config(name);
});
}
/**
* Wrap a MixpanelGroup method with a check for whether the user is opted out of data tracking and cookies/localstorage for the given token
* If the user has opted out, return early instead of executing the method.
* If a callback argument was provided, execute it passing the 0 error code.
* @param {function} method - wrapped method to be executed if the user has not opted out
* @returns {*} the result of executing method OR undefined if the user has opted out
*/
export function addOptOutCheckMixpanelGroup(method) {
return _addOptOutCheck(method, function(name) {
return this._get_config(name);
});
}
/**
* Clear the user's opt in/out status of data tracking and cookies/localstorage for the given token
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistenceType] Persistence mechanism used - cookie or localStorage
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @param {Number} [options.cookieExpiration] - number of days until the opt-in cookie expires
* @param {string} [options.cookieDomain] - custom cookie domain
* @param {boolean} [options.crossSiteCookie] - whether the opt-in cookie is set as cross-site-enabled
* @param {boolean} [options.crossSubdomainCookie] - whether the opt-in cookie is set as cross-subdomain or not
* @param {boolean} [options.secureCookie] - whether the opt-in cookie is set as secure or not
*/
export function clearOptInOut(token, options) {
options = options || {};
_getStorage(options).remove(
_getStorageKey(token, options), !!options.crossSubdomainCookie, options.cookieDomain
);
}
/** Private **/
/**
* Get storage util
* @param {Object} [options]
* @param {string} [options.persistenceType]
* @returns {object} either _.cookie or _.localstorage
*/
function _getStorage(options) {
options = options || {};
return options.persistenceType === 'localStorage' ? _.localStorage : _.cookie;
}
/**
* Get the name of the cookie that is used for the given opt type (tracking, cookie, etc.)
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @returns {string} the name of the cookie for the given opt type
*/
function _getStorageKey(token, options) {
options = options || {};
return (options.persistencePrefix || GDPR_DEFAULT_PERSISTENCE_PREFIX) + token;
}
/**
* Get the value of the cookie that is used for the given opt type (tracking, cookie, etc.)
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @returns {string} the value of the cookie for the given opt type
*/
function _getStorageValue(token, options) {
return _getStorage(options).get(_getStorageKey(token, options));
}
/**
* Check whether the user has set the DNT/doNotTrack setting to true in their browser
* @param {Object} [options]
* @param {string} [options.window] - alternate window object to check; used to force various DNT settings in browser tests
* @param {boolean} [options.ignoreDnt] - flag to ignore browser DNT settings and always return false
* @returns {boolean} whether the DNT setting is true
*/
function _hasDoNotTrackFlagOn(options) {
if (options && options.ignoreDnt) {
return false;
}
var win = (options && options.window) || window;
var nav = win['navigator'] || {};
var hasDntOn = false;
_.each([
nav['doNotTrack'], // standard
nav['msDoNotTrack'],
win['doNotTrack']
], function(dntValue) {
if (_.includes([true, 1, '1', 'yes'], dntValue)) {
hasDntOn = true;
}
});
return hasDntOn;
}
/**
* Set cookie/localstorage for the user indicating that they are opted in or out for the given opt type
* @param {boolean} optValue - whether to opt the user in or out for the given opt type
* @param {string} token - Mixpanel project tracking token
* @param {Object} [options]
* @param {trackFunction} [options.track] - function used for tracking a Mixpanel event to record the opt-in action
* @param {string} [options.trackEventName] - event name to be used for tracking the opt-in action
* @param {Object} [options.trackProperties] - set of properties to be tracked along with the opt-in action
* @param {string} [options.persistencePrefix=__mp_opt_in_out] - custom prefix to be used in the cookie/localstorage name
* @param {Number} [options.cookieExpiration] - number of days until the opt-in cookie expires
* @param {string} [options.cookieDomain] - custom cookie domain
* @param {boolean} [options.crossSiteCookie] - whether the opt-in cookie is set as cross-site-enabled
* @param {boolean} [options.crossSubdomainCookie] - whether the opt-in cookie is set as cross-subdomain or not
* @param {boolean} [options.secureCookie] - whether the opt-in cookie is set as secure or not
*/
function _optInOut(optValue, token, options) {
if (!_.isString(token) || !token.length) {
console.error('gdpr.' + (optValue ? 'optIn' : 'optOut') + ' called with an invalid token');
return;
}
options = options || {};
_getStorage(options).set(
_getStorageKey(token, options),
optValue ? 1 : 0,
_.isNumber(options.cookieExpiration) ? options.cookieExpiration : null,
!!options.crossSubdomainCookie,
!!options.secureCookie,
!!options.crossSiteCookie,
options.cookieDomain
);
if (options.track && optValue) { // only track event if opting in (optValue=true)
options.track(options.trackEventName || '$opt_in', options.trackProperties, {
'send_immediately': true
});
}
}
/**
* Wrap a method with a check for whether the user is opted out of data tracking and cookies/localstorage for the given token
* If the user has opted out, return early instead of executing the method.
* If a callback argument was provided, execute it passing the 0 error code.
* @param {function} method - wrapped method to be executed if the user has not opted out
* @param {function} getConfigValue - getter function for the Mixpanel API token and other options to be used with opt-out check
* @returns {*} the result of executing method OR undefined if the user has opted out
*/
function _addOptOutCheck(method, getConfigValue) {
return function() {
var optedOut = false;
try {
var token = getConfigValue.call(this, 'token');
var ignoreDnt = getConfigValue.call(this, 'ignore_dnt');
var persistenceType = getConfigValue.call(this, 'opt_out_tracking_persistence_type');
var persistencePrefix = getConfigValue.call(this, 'opt_out_tracking_cookie_prefix');
var win = getConfigValue.call(this, 'window'); // used to override window during browser tests
if (token) { // if there was an issue getting the token, continue method execution as normal
optedOut = hasOptedOut(token, {
ignoreDnt: ignoreDnt,
persistenceType: persistenceType,
persistencePrefix: persistencePrefix,
window: win
});
}
} catch(err) {
console.error('Unexpected error when checking tracking opt-out status: ' + err);
}
if (!optedOut) {
return method.apply(this, arguments);
}
var callback = arguments[arguments.length - 1];
if (typeof(callback) === 'function') {
callback(0);
}
return;
};
}