-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathmixpanel-people.js
483 lines (450 loc) · 17.2 KB
/
mixpanel-people.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* eslint camelcase: "off" */
import { addOptOutCheckMixpanelPeople } from './gdpr-utils';
import {
SET_ACTION,
SET_ONCE_ACTION,
UNSET_ACTION,
ADD_ACTION,
APPEND_ACTION,
REMOVE_ACTION,
UNION_ACTION,
apiActions
} from './api-actions';
import { _, console } from './utils';
/**
* Mixpanel People Object
* @constructor
*/
var MixpanelPeople = function() {};
_.extend(MixpanelPeople.prototype, apiActions);
MixpanelPeople.prototype._init = function(mixpanel_instance) {
this._mixpanel = mixpanel_instance;
};
/*
* Set properties on a user record.
*
* ### Usage:
*
* mixpanel.people.set('gender', 'm');
*
* // or set multiple properties at once
* mixpanel.people.set({
* 'Company': 'Acme',
* 'Plan': 'Premium',
* 'Upgrade date': new Date()
* });
* // properties can be strings, integers, dates, or lists
*
* @param {Object|String} prop If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [to] A value to set on the given property name
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.set = addOptOutCheckMixpanelPeople(function(prop, to, callback) {
var data = this.set_action(prop, to);
if (_.isObject(prop)) {
callback = to;
}
// make sure that the referrer info has been updated and saved
if (this._get_config('save_referrer')) {
this._mixpanel['persistence'].update_referrer_info(document.referrer);
}
// update $set object with default people properties
data[SET_ACTION] = _.extend(
{},
_.info.people_properties(),
data[SET_ACTION]
);
return this._send_request(data, callback);
});
/*
* Set properties on a user record, only if they do not yet exist.
* This will not overwrite previous people property values, unlike
* people.set().
*
* ### Usage:
*
* mixpanel.people.set_once('First Login Date', new Date());
*
* // or set multiple properties at once
* mixpanel.people.set_once({
* 'First Login Date': new Date(),
* 'Starting Plan': 'Premium'
* });
*
* // properties can be strings, integers or dates
*
* @param {Object|String} prop If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [to] A value to set on the given property name
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.set_once = addOptOutCheckMixpanelPeople(function(prop, to, callback) {
var data = this.set_once_action(prop, to);
if (_.isObject(prop)) {
callback = to;
}
return this._send_request(data, callback);
});
/*
* Unset properties on a user record (permanently removes the properties and their values from a profile).
*
* ### Usage:
*
* mixpanel.people.unset('gender');
*
* // or unset multiple properties at once
* mixpanel.people.unset(['gender', 'Company']);
*
* @param {Array|String} prop If a string, this is the name of the property. If an array, this is a list of property names.
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.unset = addOptOutCheckMixpanelPeople(function(prop, callback) {
var data = this.unset_action(prop);
return this._send_request(data, callback);
});
/*
* Increment/decrement numeric people analytics properties.
*
* ### Usage:
*
* mixpanel.people.increment('page_views', 1);
*
* // or, for convenience, if you're just incrementing a counter by
* // 1, you can simply do
* mixpanel.people.increment('page_views');
*
* // to decrement a counter, pass a negative number
* mixpanel.people.increment('credits_left', -1);
*
* // like mixpanel.people.set(), you can increment multiple
* // properties at once:
* mixpanel.people.increment({
* counter1: 1,
* counter2: 6
* });
*
* @param {Object|String} prop If a string, this is the name of the property. If an object, this is an associative array of names and numeric values.
* @param {Number} [by] An amount to increment the given property
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.increment = addOptOutCheckMixpanelPeople(function(prop, by, callback) {
var data = {};
var $add = {};
if (_.isObject(prop)) {
_.each(prop, function(v, k) {
if (!this._is_reserved_property(k)) {
if (isNaN(parseFloat(v))) {
console.error('Invalid increment value passed to mixpanel.people.increment - must be a number');
return;
} else {
$add[k] = v;
}
}
}, this);
callback = by;
} else {
// convenience: mixpanel.people.increment('property'); will
// increment 'property' by 1
if (_.isUndefined(by)) {
by = 1;
}
$add[prop] = by;
}
data[ADD_ACTION] = $add;
return this._send_request(data, callback);
});
/*
* Append a value to a list-valued people analytics property.
*
* ### Usage:
*
* // append a value to a list, creating it if needed
* mixpanel.people.append('pages_visited', 'homepage');
*
* // like mixpanel.people.set(), you can append multiple
* // properties at once:
* mixpanel.people.append({
* list1: 'bob',
* list2: 123
* });
*
* @param {Object|String} list_name If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [value] value An item to append to the list
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.append = addOptOutCheckMixpanelPeople(function(list_name, value, callback) {
if (_.isObject(list_name)) {
callback = value;
}
var data = this.append_action(list_name, value);
return this._send_request(data, callback);
});
/*
* Remove a value from a list-valued people analytics property.
*
* ### Usage:
*
* mixpanel.people.remove('School', 'UCB');
*
* @param {Object|String} list_name If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [value] value Item to remove from the list
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.remove = addOptOutCheckMixpanelPeople(function(list_name, value, callback) {
if (_.isObject(list_name)) {
callback = value;
}
var data = this.remove_action(list_name, value);
return this._send_request(data, callback);
});
/*
* Merge a given list with a list-valued people analytics property,
* excluding duplicate values.
*
* ### Usage:
*
* // merge a value to a list, creating it if needed
* mixpanel.people.union('pages_visited', 'homepage');
*
* // like mixpanel.people.set(), you can append multiple
* // properties at once:
* mixpanel.people.union({
* list1: 'bob',
* list2: 123
* });
*
* // like mixpanel.people.append(), you can append multiple
* // values to the same list:
* mixpanel.people.union({
* list1: ['bob', 'billy']
* });
*
* @param {Object|String} list_name If a string, this is the name of the property. If an object, this is an associative array of names and values.
* @param {*} [value] Value / values to merge with the given property
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
*/
MixpanelPeople.prototype.union = addOptOutCheckMixpanelPeople(function(list_name, values, callback) {
if (_.isObject(list_name)) {
callback = values;
}
var data = this.union_action(list_name, values);
return this._send_request(data, callback);
});
/*
* Record that you have charged the current user a certain amount
* of money. Charges recorded with track_charge() will appear in the
* Mixpanel revenue report.
*
* ### Usage:
*
* // charge a user $50
* mixpanel.people.track_charge(50);
*
* // charge a user $30.50 on the 2nd of january
* mixpanel.people.track_charge(30.50, {
* '$time': new Date('jan 1 2012')
* });
*
* @param {Number} amount The amount of money charged to the current user
* @param {Object} [properties] An associative array of properties associated with the charge
* @param {Function} [callback] If provided, the callback will be called when the server responds
* @deprecated
*/
MixpanelPeople.prototype.track_charge = addOptOutCheckMixpanelPeople(function(amount, properties, callback) {
if (!_.isNumber(amount)) {
amount = parseFloat(amount);
if (isNaN(amount)) {
console.error('Invalid value passed to mixpanel.people.track_charge - must be a number');
return;
}
}
return this.append('$transactions', _.extend({
'$amount': amount
}, properties), callback);
});
/*
* Permanently clear all revenue report transactions from the
* current user's people analytics profile.
*
* ### Usage:
*
* mixpanel.people.clear_charges();
*
* @param {Function} [callback] If provided, the callback will be called after tracking the event.
* @deprecated
*/
MixpanelPeople.prototype.clear_charges = function(callback) {
return this.set('$transactions', [], callback);
};
/*
* Permanently deletes the current people analytics profile from
* Mixpanel (using the current distinct_id).
*
* ### Usage:
*
* // remove the all data you have stored about the current user
* mixpanel.people.delete_user();
*
*/
MixpanelPeople.prototype.delete_user = function() {
if (!this._identify_called()) {
console.error('mixpanel.people.delete_user() requires you to call identify() first');
return;
}
var data = {'$delete': this._mixpanel.get_distinct_id()};
return this._send_request(data);
};
MixpanelPeople.prototype.toString = function() {
return this._mixpanel.toString() + '.people';
};
MixpanelPeople.prototype._send_request = function(data, callback) {
data['$token'] = this._get_config('token');
data['$distinct_id'] = this._mixpanel.get_distinct_id();
var device_id = this._mixpanel.get_property('$device_id');
var user_id = this._mixpanel.get_property('$user_id');
var had_persisted_distinct_id = this._mixpanel.get_property('$had_persisted_distinct_id');
if (device_id) {
data['$device_id'] = device_id;
}
if (user_id) {
data['$user_id'] = user_id;
}
if (had_persisted_distinct_id) {
data['$had_persisted_distinct_id'] = had_persisted_distinct_id;
}
var date_encoded_data = _.encodeDates(data);
if (!this._identify_called()) {
this._enqueue(data);
if (!_.isUndefined(callback)) {
if (this._get_config('verbose')) {
callback({status: -1, error: null});
} else {
callback(-1);
}
}
return _.truncate(date_encoded_data, 255);
}
return this._mixpanel._track_or_batch({
type: 'people',
data: date_encoded_data,
endpoint: this._get_config('api_host') + '/' + this._get_config('api_routes')['engage'],
batcher: this._mixpanel.request_batchers.people
}, callback);
};
MixpanelPeople.prototype._get_config = function(conf_var) {
return this._mixpanel.get_config(conf_var);
};
MixpanelPeople.prototype._identify_called = function() {
return this._mixpanel._flags.identify_called === true;
};
// Queue up engage operations if identify hasn't been called yet.
MixpanelPeople.prototype._enqueue = function(data) {
if (SET_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(SET_ACTION, data);
} else if (SET_ONCE_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(SET_ONCE_ACTION, data);
} else if (UNSET_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(UNSET_ACTION, data);
} else if (ADD_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(ADD_ACTION, data);
} else if (APPEND_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(APPEND_ACTION, data);
} else if (REMOVE_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(REMOVE_ACTION, data);
} else if (UNION_ACTION in data) {
this._mixpanel['persistence']._add_to_people_queue(UNION_ACTION, data);
} else {
console.error('Invalid call to _enqueue():', data);
}
};
MixpanelPeople.prototype._flush_one_queue = function(action, action_method, callback, queue_to_params_fn) {
var _this = this;
var queued_data = _.extend({}, this._mixpanel['persistence'].load_queue(action));
var action_params = queued_data;
if (!_.isUndefined(queued_data) && _.isObject(queued_data) && !_.isEmptyObject(queued_data)) {
_this._mixpanel['persistence']._pop_from_people_queue(action, queued_data);
_this._mixpanel['persistence'].save();
if (queue_to_params_fn) {
action_params = queue_to_params_fn(queued_data);
}
action_method.call(_this, action_params, function(response, data) {
// on bad response, we want to add it back to the queue
if (response === 0) {
_this._mixpanel['persistence']._add_to_people_queue(action, queued_data);
}
if (!_.isUndefined(callback)) {
callback(response, data);
}
});
}
};
// Flush queued engage operations - order does not matter,
// and there are network level race conditions anyway
MixpanelPeople.prototype._flush = function(
_set_callback, _add_callback, _append_callback, _set_once_callback, _union_callback, _unset_callback, _remove_callback
) {
var _this = this;
this._flush_one_queue(SET_ACTION, this.set, _set_callback);
this._flush_one_queue(SET_ONCE_ACTION, this.set_once, _set_once_callback);
this._flush_one_queue(UNSET_ACTION, this.unset, _unset_callback, function(queue) { return _.keys(queue); });
this._flush_one_queue(ADD_ACTION, this.increment, _add_callback);
this._flush_one_queue(UNION_ACTION, this.union, _union_callback);
// we have to fire off each $append individually since there is
// no concat method server side
var $append_queue = this._mixpanel['persistence'].load_queue(APPEND_ACTION);
if (!_.isUndefined($append_queue) && _.isArray($append_queue) && $append_queue.length) {
var $append_item;
var append_callback = function(response, data) {
if (response === 0) {
_this._mixpanel['persistence']._add_to_people_queue(APPEND_ACTION, $append_item);
}
if (!_.isUndefined(_append_callback)) {
_append_callback(response, data);
}
};
for (var i = $append_queue.length - 1; i >= 0; i--) {
$append_queue = this._mixpanel['persistence'].load_queue(APPEND_ACTION);
$append_item = $append_queue.pop();
_this._mixpanel['persistence'].save();
if (!_.isEmptyObject($append_item)) {
_this.append($append_item, append_callback);
}
}
}
// same for $remove
var $remove_queue = this._mixpanel['persistence'].load_queue(REMOVE_ACTION);
if (!_.isUndefined($remove_queue) && _.isArray($remove_queue) && $remove_queue.length) {
var $remove_item;
var remove_callback = function(response, data) {
if (response === 0) {
_this._mixpanel['persistence']._add_to_people_queue(REMOVE_ACTION, $remove_item);
}
if (!_.isUndefined(_remove_callback)) {
_remove_callback(response, data);
}
};
for (var j = $remove_queue.length - 1; j >= 0; j--) {
$remove_queue = this._mixpanel['persistence'].load_queue(REMOVE_ACTION);
$remove_item = $remove_queue.pop();
_this._mixpanel['persistence'].save();
if (!_.isEmptyObject($remove_item)) {
_this.remove($remove_item, remove_callback);
}
}
}
};
MixpanelPeople.prototype._is_reserved_property = function(prop) {
return prop === '$distinct_id' || prop === '$token' || prop === '$device_id' || prop === '$user_id' || prop === '$had_persisted_distinct_id';
};
// MixpanelPeople Exports
MixpanelPeople.prototype['set'] = MixpanelPeople.prototype.set;
MixpanelPeople.prototype['set_once'] = MixpanelPeople.prototype.set_once;
MixpanelPeople.prototype['unset'] = MixpanelPeople.prototype.unset;
MixpanelPeople.prototype['increment'] = MixpanelPeople.prototype.increment;
MixpanelPeople.prototype['append'] = MixpanelPeople.prototype.append;
MixpanelPeople.prototype['remove'] = MixpanelPeople.prototype.remove;
MixpanelPeople.prototype['union'] = MixpanelPeople.prototype.union;
MixpanelPeople.prototype['track_charge'] = MixpanelPeople.prototype.track_charge;
MixpanelPeople.prototype['clear_charges'] = MixpanelPeople.prototype.clear_charges;
MixpanelPeople.prototype['delete_user'] = MixpanelPeople.prototype.delete_user;
MixpanelPeople.prototype['toString'] = MixpanelPeople.prototype.toString;
export { MixpanelPeople };