-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathmixpanel-persistence.js
450 lines (393 loc) · 13.3 KB
/
mixpanel-persistence.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* eslint camelcase: "off" */
import {
SET_ACTION,
SET_ONCE_ACTION,
UNSET_ACTION,
ADD_ACTION,
APPEND_ACTION,
REMOVE_ACTION,
UNION_ACTION
} from './api-actions';
import { _, console, JSONStringify } from './utils';
/*
* Constants
*/
/** @const */ var SET_QUEUE_KEY = '__mps';
/** @const */ var SET_ONCE_QUEUE_KEY = '__mpso';
/** @const */ var UNSET_QUEUE_KEY = '__mpus';
/** @const */ var ADD_QUEUE_KEY = '__mpa';
/** @const */ var APPEND_QUEUE_KEY = '__mpap';
/** @const */ var REMOVE_QUEUE_KEY = '__mpr';
/** @const */ var UNION_QUEUE_KEY = '__mpu';
// This key is deprecated, but we want to check for it to see whether aliasing is allowed.
/** @const */ var PEOPLE_DISTINCT_ID_KEY = '$people_distinct_id';
/** @const */ var ALIAS_ID_KEY = '__alias';
/** @const */ var EVENT_TIMERS_KEY = '__timers';
/** @const */ var RESERVED_PROPERTIES = [
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
UNSET_QUEUE_KEY,
ADD_QUEUE_KEY,
APPEND_QUEUE_KEY,
REMOVE_QUEUE_KEY,
UNION_QUEUE_KEY,
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
EVENT_TIMERS_KEY
];
/**
* Mixpanel Persistence Object
* @constructor
*/
var MixpanelPersistence = function(config) {
this['props'] = {};
this.campaign_params_saved = false;
if (config['persistence_name']) {
this.name = 'mp_' + config['persistence_name'];
} else {
this.name = 'mp_' + config['token'] + '_mixpanel';
}
var storage_type = config['persistence'];
if (storage_type !== 'cookie' && storage_type !== 'localStorage') {
console.critical('Unknown persistence type ' + storage_type + '; falling back to cookie');
storage_type = config['persistence'] = 'cookie';
}
if (storage_type === 'localStorage' && _.localStorage.is_supported()) {
this.storage = _.localStorage;
} else {
this.storage = _.cookie;
}
this.load();
this.update_config(config);
this.upgrade();
this.save();
};
MixpanelPersistence.prototype.properties = function() {
var p = {};
this.load();
// Filter out reserved properties
_.each(this['props'], function(v, k) {
if (!_.include(RESERVED_PROPERTIES, k)) {
p[k] = v;
}
});
return p;
};
MixpanelPersistence.prototype.load = function() {
if (this.disabled) { return; }
var entry = this.storage.parse(this.name);
if (entry) {
this['props'] = _.extend({}, entry);
}
};
MixpanelPersistence.prototype.upgrade = function() {
var old_cookie,
old_localstorage;
// if transferring from cookie to localStorage or vice-versa, copy existing
// super properties over to new storage mode
if (this.storage === _.localStorage) {
old_cookie = _.cookie.parse(this.name);
_.cookie.remove(this.name);
_.cookie.remove(this.name, true);
if (old_cookie) {
this.register_once(old_cookie);
}
} else if (this.storage === _.cookie) {
old_localstorage = _.localStorage.parse(this.name);
_.localStorage.remove(this.name);
if (old_localstorage) {
this.register_once(old_localstorage);
}
}
};
MixpanelPersistence.prototype.save = function() {
if (this.disabled) { return; }
this.storage.set(
this.name,
JSONStringify(this['props']),
this.expire_days,
this.cross_subdomain,
this.secure,
this.cross_site,
this.cookie_domain
);
};
MixpanelPersistence.prototype.load_prop = function(key) {
this.load();
return this['props'][key];
};
MixpanelPersistence.prototype.remove = function() {
// remove both domain and subdomain cookies
this.storage.remove(this.name, false, this.cookie_domain);
this.storage.remove(this.name, true, this.cookie_domain);
};
// removes the storage entry and deletes all loaded data
// forced name for tests
MixpanelPersistence.prototype.clear = function() {
this.remove();
this['props'] = {};
};
/**
* @param {Object} props
* @param {*=} default_value
* @param {number=} days
*/
MixpanelPersistence.prototype.register_once = function(props, default_value, days) {
if (_.isObject(props)) {
if (typeof(default_value) === 'undefined') { default_value = 'None'; }
this.expire_days = (typeof(days) === 'undefined') ? this.default_expiry : days;
this.load();
_.each(props, function(val, prop) {
if (!this['props'].hasOwnProperty(prop) || this['props'][prop] === default_value) {
this['props'][prop] = val;
}
}, this);
this.save();
return true;
}
return false;
};
/**
* @param {Object} props
* @param {number=} days
*/
MixpanelPersistence.prototype.register = function(props, days) {
if (_.isObject(props)) {
this.expire_days = (typeof(days) === 'undefined') ? this.default_expiry : days;
this.load();
_.extend(this['props'], props);
this.save();
return true;
}
return false;
};
MixpanelPersistence.prototype.unregister = function(prop) {
this.load();
if (prop in this['props']) {
delete this['props'][prop];
this.save();
}
};
MixpanelPersistence.prototype.update_search_keyword = function(referrer) {
this.register(_.info.searchInfo(referrer));
};
// EXPORTED METHOD, we test this directly.
MixpanelPersistence.prototype.update_referrer_info = function(referrer) {
// If referrer doesn't exist, we want to note the fact that it was type-in traffic.
this.register_once({
'$initial_referrer': referrer || '$direct',
'$initial_referring_domain': _.info.referringDomain(referrer) || '$direct'
}, '');
};
MixpanelPersistence.prototype.get_referrer_info = function() {
return _.strip_empty_properties({
'$initial_referrer': this['props']['$initial_referrer'],
'$initial_referring_domain': this['props']['$initial_referring_domain']
});
};
MixpanelPersistence.prototype.update_config = function(config) {
this.default_expiry = this.expire_days = config['cookie_expiration'];
this.set_disabled(config['disable_persistence']);
this.set_cookie_domain(config['cookie_domain']);
this.set_cross_site(config['cross_site_cookie']);
this.set_cross_subdomain(config['cross_subdomain_cookie']);
this.set_secure(config['secure_cookie']);
};
MixpanelPersistence.prototype.set_disabled = function(disabled) {
this.disabled = disabled;
if (this.disabled) {
this.remove();
} else {
this.save();
}
};
MixpanelPersistence.prototype.set_cookie_domain = function(cookie_domain) {
if (cookie_domain !== this.cookie_domain) {
this.remove();
this.cookie_domain = cookie_domain;
this.save();
}
};
MixpanelPersistence.prototype.set_cross_site = function(cross_site) {
if (cross_site !== this.cross_site) {
this.cross_site = cross_site;
this.remove();
this.save();
}
};
MixpanelPersistence.prototype.set_cross_subdomain = function(cross_subdomain) {
if (cross_subdomain !== this.cross_subdomain) {
this.cross_subdomain = cross_subdomain;
this.remove();
this.save();
}
};
MixpanelPersistence.prototype.get_cross_subdomain = function() {
return this.cross_subdomain;
};
MixpanelPersistence.prototype.set_secure = function(secure) {
if (secure !== this.secure) {
this.secure = secure ? true : false;
this.remove();
this.save();
}
};
MixpanelPersistence.prototype._add_to_people_queue = function(queue, data) {
var q_key = this._get_queue_key(queue),
q_data = data[queue],
set_q = this._get_or_create_queue(SET_ACTION),
set_once_q = this._get_or_create_queue(SET_ONCE_ACTION),
unset_q = this._get_or_create_queue(UNSET_ACTION),
add_q = this._get_or_create_queue(ADD_ACTION),
union_q = this._get_or_create_queue(UNION_ACTION),
remove_q = this._get_or_create_queue(REMOVE_ACTION, []),
append_q = this._get_or_create_queue(APPEND_ACTION, []);
if (q_key === SET_QUEUE_KEY) {
// Update the set queue - we can override any existing values
_.extend(set_q, q_data);
// if there was a pending increment, override it
// with the set.
this._pop_from_people_queue(ADD_ACTION, q_data);
// if there was a pending union, override it
// with the set.
this._pop_from_people_queue(UNION_ACTION, q_data);
this._pop_from_people_queue(UNSET_ACTION, q_data);
} else if (q_key === SET_ONCE_QUEUE_KEY) {
// only queue the data if there is not already a set_once call for it.
_.each(q_data, function(v, k) {
if (!(k in set_once_q)) {
set_once_q[k] = v;
}
});
this._pop_from_people_queue(UNSET_ACTION, q_data);
} else if (q_key === UNSET_QUEUE_KEY) {
_.each(q_data, function(prop) {
// undo previously-queued actions on this key
_.each([set_q, set_once_q, add_q, union_q], function(enqueued_obj) {
if (prop in enqueued_obj) {
delete enqueued_obj[prop];
}
});
_.each(append_q, function(append_obj) {
if (prop in append_obj) {
delete append_obj[prop];
}
});
unset_q[prop] = true;
});
} else if (q_key === ADD_QUEUE_KEY) {
_.each(q_data, function(v, k) {
// If it exists in the set queue, increment
// the value
if (k in set_q) {
set_q[k] += v;
} else {
// If it doesn't exist, update the add
// queue
if (!(k in add_q)) {
add_q[k] = 0;
}
add_q[k] += v;
}
}, this);
this._pop_from_people_queue(UNSET_ACTION, q_data);
} else if (q_key === UNION_QUEUE_KEY) {
_.each(q_data, function(v, k) {
if (_.isArray(v)) {
if (!(k in union_q)) {
union_q[k] = [];
}
// Prevent duplicate values
_.each(v, function(item) {
if (!_.include(union_q[k], item)) {
union_q[k].push(item);
}
});
}
});
this._pop_from_people_queue(UNSET_ACTION, q_data);
} else if (q_key === REMOVE_QUEUE_KEY) {
remove_q.push(q_data);
this._pop_from_people_queue(APPEND_ACTION, q_data);
} else if (q_key === APPEND_QUEUE_KEY) {
append_q.push(q_data);
this._pop_from_people_queue(UNSET_ACTION, q_data);
}
console.log('MIXPANEL PEOPLE REQUEST (QUEUED, PENDING IDENTIFY):');
console.log(data);
this.save();
};
MixpanelPersistence.prototype._pop_from_people_queue = function(queue, data) {
var q = this['props'][this._get_queue_key(queue)];
if (!_.isUndefined(q)) {
_.each(data, function(v, k) {
if (queue === APPEND_ACTION || queue === REMOVE_ACTION) {
// list actions: only remove if both k+v match
// e.g. remove should not override append in a case like
// append({foo: 'bar'}); remove({foo: 'qux'})
_.each(q, function(queued_action) {
if (queued_action[k] === v) {
delete queued_action[k];
}
});
} else {
delete q[k];
}
}, this);
}
};
MixpanelPersistence.prototype.load_queue = function(queue) {
return this.load_prop(this._get_queue_key(queue));
};
MixpanelPersistence.prototype._get_queue_key = function(queue) {
if (queue === SET_ACTION) {
return SET_QUEUE_KEY;
} else if (queue === SET_ONCE_ACTION) {
return SET_ONCE_QUEUE_KEY;
} else if (queue === UNSET_ACTION) {
return UNSET_QUEUE_KEY;
} else if (queue === ADD_ACTION) {
return ADD_QUEUE_KEY;
} else if (queue === APPEND_ACTION) {
return APPEND_QUEUE_KEY;
} else if (queue === REMOVE_ACTION) {
return REMOVE_QUEUE_KEY;
} else if (queue === UNION_ACTION) {
return UNION_QUEUE_KEY;
} else {
console.error('Invalid queue:', queue);
}
};
MixpanelPersistence.prototype._get_or_create_queue = function(queue, default_val) {
var key = this._get_queue_key(queue);
default_val = _.isUndefined(default_val) ? {} : default_val;
return this['props'][key] || (this['props'][key] = default_val);
};
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
var timers = this.load_prop(EVENT_TIMERS_KEY) || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;
this.save();
};
MixpanelPersistence.prototype.remove_event_timer = function(event_name) {
var timers = this.load_prop(EVENT_TIMERS_KEY) || {};
var timestamp = timers[event_name];
if (!_.isUndefined(timestamp)) {
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;
};
export {
MixpanelPersistence,
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
UNSET_QUEUE_KEY,
ADD_QUEUE_KEY,
APPEND_QUEUE_KEY,
REMOVE_QUEUE_KEY,
UNION_QUEUE_KEY,
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
EVENT_TIMERS_KEY
};