1 | const {merge_modifiers, ProfileHelpers} = require('./profile_helpers');
|
2 |
|
3 | class MixpanelPeople extends ProfileHelpers() {
|
4 | constructor(mp_instance) {
|
5 | super();
|
6 | this.mixpanel = mp_instance;
|
7 | this.endpoint = '/engage';
|
8 | }
|
9 |
|
10 | /** people.set_once(distinct_id, prop, to, modifiers, callback)
|
11 | ---
|
12 | The same as people.set but in the words of mixpanel:
|
13 | mixpanel.people.set_once
|
14 |
|
15 | " This method allows you to set a user attribute, only if
|
16 | it is not currently set. It can be called multiple times
|
17 | safely, so is perfect for storing things like the first date
|
18 | you saw a user, or the referrer that brought them to your
|
19 | website for the first time. "
|
20 |
|
21 | */
|
22 | set_once(distinct_id, prop, to, modifiers, callback) {
|
23 | const identifiers = {$distinct_id: distinct_id};
|
24 | this._set(prop, to, modifiers, callback, {identifiers, set_once: true});
|
25 | }
|
26 |
|
27 | /**
|
28 | people.set(distinct_id, prop, to, modifiers, callback)
|
29 | ---
|
30 | set properties on an user record in engage
|
31 |
|
32 | usage:
|
33 |
|
34 | mixpanel.people.set('bob', 'gender', 'm');
|
35 |
|
36 | mixpanel.people.set('joe', {
|
37 | 'company': 'acme',
|
38 | 'plan': 'premium'
|
39 | });
|
40 | */
|
41 | set(distinct_id, prop, to, modifiers, callback) {
|
42 | const identifiers = {$distinct_id: distinct_id};
|
43 | this._set(prop, to, modifiers, callback, {identifiers});
|
44 | }
|
45 |
|
46 | /**
|
47 | people.increment(distinct_id, prop, by, modifiers, callback)
|
48 | ---
|
49 | increment/decrement properties on an user record in engage
|
50 |
|
51 | usage:
|
52 |
|
53 | mixpanel.people.increment('bob', 'page_views', 1);
|
54 |
|
55 | // or, for convenience, if you're just incrementing a counter by 1, you can
|
56 | // simply do
|
57 | mixpanel.people.increment('bob', 'page_views');
|
58 |
|
59 | // to decrement a counter, pass a negative number
|
60 | mixpanel.people.increment('bob', 'credits_left', -1);
|
61 |
|
62 | // like mixpanel.people.set(), you can increment multiple properties at once:
|
63 | mixpanel.people.increment('bob', {
|
64 | counter1: 1,
|
65 | counter2: 3,
|
66 | counter3: -2
|
67 | });
|
68 | */
|
69 | increment(distinct_id, prop, by, modifiers, callback) {
|
70 | // TODO extract to ProfileHelpers
|
71 |
|
72 | var $add = {};
|
73 |
|
74 | if (typeof(prop) === 'object') {
|
75 | if (typeof(by) === 'object') {
|
76 | callback = modifiers;
|
77 | modifiers = by;
|
78 | } else {
|
79 | callback = by;
|
80 | }
|
81 | for (const [key, val] of Object.entries(prop)) {
|
82 | if (isNaN(parseFloat(val))) {
|
83 | if (this.mixpanel.config.debug) {
|
84 | console.error("Invalid increment value passed to mixpanel.people.increment - must be a number");
|
85 | console.error("Passed " + key + ":" + val);
|
86 | }
|
87 | } else {
|
88 | $add[key] = val;
|
89 | }
|
90 | };
|
91 | } else {
|
92 | if (typeof(by) === 'number' || !by) {
|
93 | by = by || 1;
|
94 | $add[prop] = by;
|
95 | if (typeof(modifiers) === 'function') {
|
96 | callback = modifiers;
|
97 | }
|
98 | } else if (typeof(by) === 'function') {
|
99 | callback = by;
|
100 | $add[prop] = 1;
|
101 | } else {
|
102 | callback = modifiers;
|
103 | modifiers = (typeof(by) === 'object') ? by : {};
|
104 | $add[prop] = 1;
|
105 | }
|
106 | }
|
107 |
|
108 | var data = {
|
109 | '$add': $add,
|
110 | '$token': this.mixpanel.token,
|
111 | '$distinct_id': distinct_id
|
112 | };
|
113 |
|
114 | data = merge_modifiers(data, modifiers);
|
115 |
|
116 | if (this.mixpanel.config.debug) {
|
117 | console.log("Sending the following data to Mixpanel (Engage):");
|
118 | console.log(data);
|
119 | }
|
120 |
|
121 | this.mixpanel.send_request({ method: "GET", endpoint: "/engage", data: data }, callback);
|
122 | }
|
123 |
|
124 | /**
|
125 | people.append(distinct_id, prop, value, modifiers, callback)
|
126 | ---
|
127 | Append a value to a list-valued people analytics property.
|
128 |
|
129 | usage:
|
130 |
|
131 | // append a value to a list, creating it if needed
|
132 | mixpanel.people.append('bob', 'pages_visited', 'homepage');
|
133 |
|
134 | // like mixpanel.people.set(), you can append multiple properties at once:
|
135 | mixpanel.people.append('bob', {
|
136 | list1: 'bob',
|
137 | list2: 123
|
138 | });
|
139 | */
|
140 | append(distinct_id, prop, value, modifiers, callback) {
|
141 | // TODO extract to ProfileHelpers
|
142 |
|
143 | var $append = {};
|
144 |
|
145 | if (typeof(prop) === 'object') {
|
146 | if (typeof(value) === 'object') {
|
147 | callback = modifiers;
|
148 | modifiers = value;
|
149 | } else {
|
150 | callback = value;
|
151 | }
|
152 | Object.keys(prop).forEach(function(key) {
|
153 | $append[key] = prop[key];
|
154 | });
|
155 | } else {
|
156 | $append[prop] = value;
|
157 | if (typeof(modifiers) === 'function') {
|
158 | callback = modifiers;
|
159 | }
|
160 | }
|
161 |
|
162 | var data = {
|
163 | '$append': $append,
|
164 | '$token': this.mixpanel.token,
|
165 | '$distinct_id': distinct_id
|
166 | };
|
167 |
|
168 | data = merge_modifiers(data, modifiers);
|
169 |
|
170 | if (this.mixpanel.config.debug) {
|
171 | console.log("Sending the following data to Mixpanel (Engage):");
|
172 | console.log(data);
|
173 | }
|
174 |
|
175 | this.mixpanel.send_request({ method: "GET", endpoint: "/engage", data: data }, callback);
|
176 | }
|
177 |
|
178 | /**
|
179 | people.track_charge(distinct_id, amount, properties, modifiers, callback)
|
180 | ---
|
181 | Record that you have charged the current user a certain
|
182 | amount of money.
|
183 |
|
184 | usage:
|
185 |
|
186 | // charge a user $29.99
|
187 | mixpanel.people.track_charge('bob', 29.99);
|
188 |
|
189 | // charge a user $19 on the 1st of february
|
190 | mixpanel.people.track_charge('bob', 19, { '$time': new Date('feb 1 2012') });
|
191 | */
|
192 | track_charge(distinct_id, amount, properties, modifiers, callback) {
|
193 | if (typeof(properties) === 'function' || !properties) {
|
194 | callback = properties || function() {};
|
195 | properties = {};
|
196 | } else {
|
197 | if (typeof(modifiers) === 'function' || !modifiers) {
|
198 | callback = modifiers || function() {};
|
199 | if (properties.$ignore_time || properties.hasOwnProperty("$ip")) {
|
200 | modifiers = {};
|
201 | Object.keys(properties).forEach(function(key) {
|
202 | modifiers[key] = properties[key];
|
203 | delete properties[key];
|
204 | });
|
205 | }
|
206 | }
|
207 | }
|
208 |
|
209 | if (typeof(amount) !== 'number') {
|
210 | amount = parseFloat(amount);
|
211 | if (isNaN(amount)) {
|
212 | console.error("Invalid value passed to mixpanel.people.track_charge - must be a number");
|
213 | return;
|
214 | }
|
215 | }
|
216 |
|
217 | properties.$amount = amount;
|
218 |
|
219 | if (properties.hasOwnProperty('$time')) {
|
220 | var time = properties.$time;
|
221 | if (Object.prototype.toString.call(time) === '[object Date]') {
|
222 | properties.$time = time.toISOString();
|
223 | }
|
224 | }
|
225 |
|
226 | var data = {
|
227 | '$append': { '$transactions': properties },
|
228 | '$token': this.mixpanel.token,
|
229 | '$distinct_id': distinct_id
|
230 | };
|
231 |
|
232 | data = merge_modifiers(data, modifiers);
|
233 |
|
234 | if (this.mixpanel.config.debug) {
|
235 | console.log("Sending the following data to Mixpanel (Engage):");
|
236 | console.log(data);
|
237 | }
|
238 |
|
239 | this.mixpanel.send_request({ method: "GET", endpoint: "/engage", data: data }, callback);
|
240 | }
|
241 |
|
242 | /**
|
243 | people.clear_charges(distinct_id, modifiers, callback)
|
244 | ---
|
245 | Clear all the current user's transactions.
|
246 |
|
247 | usage:
|
248 |
|
249 | mixpanel.people.clear_charges('bob');
|
250 | */
|
251 | clear_charges(distinct_id, modifiers, callback) {
|
252 | var data = {
|
253 | '$set': { '$transactions': [] },
|
254 | '$token': this.mixpanel.token,
|
255 | '$distinct_id': distinct_id
|
256 | };
|
257 |
|
258 | if (typeof(modifiers) === 'function') { callback = modifiers; }
|
259 |
|
260 | data = merge_modifiers(data, modifiers);
|
261 |
|
262 | if (this.mixpanel.config.debug) {
|
263 | console.log("Clearing this user's charges:", distinct_id);
|
264 | }
|
265 |
|
266 | this.mixpanel.send_request({ method: "GET", endpoint: "/engage", data: data }, callback);
|
267 | }
|
268 |
|
269 | /**
|
270 | people.delete_user(distinct_id, modifiers, callback)
|
271 | ---
|
272 | delete an user record in engage
|
273 |
|
274 | usage:
|
275 |
|
276 | mixpanel.people.delete_user('bob');
|
277 | */
|
278 | delete_user(distinct_id, modifiers, callback) {
|
279 | const identifiers = {$distinct_id: distinct_id};
|
280 | this._delete_profile({identifiers, modifiers, callback});
|
281 | }
|
282 |
|
283 | /**
|
284 | people.remove(distinct_id, data, modifiers, callback)
|
285 | ---
|
286 | remove a value from a list-valued user profile property.
|
287 |
|
288 | usage:
|
289 |
|
290 | mixpanel.people.remove('bob', {'browsers': 'firefox'});
|
291 |
|
292 | mixpanel.people.remove('bob', {'browsers': 'chrome', 'os': 'linux'});
|
293 | */
|
294 | remove(distinct_id, data, modifiers, callback) {
|
295 | const identifiers = {'$distinct_id': distinct_id};
|
296 | this._remove({identifiers, data, modifiers, callback})
|
297 | }
|
298 |
|
299 | /**
|
300 | people.union(distinct_id, data, modifiers, callback)
|
301 | ---
|
302 | merge value(s) into a list-valued people analytics property.
|
303 |
|
304 | usage:
|
305 |
|
306 | mixpanel.people.union('bob', {'browsers': 'firefox'});
|
307 |
|
308 | mixpanel.people.union('bob', {'browsers': ['chrome'], os: ['linux']});
|
309 | */
|
310 | union(distinct_id, data, modifiers, callback) {
|
311 | const identifiers = {$distinct_id: distinct_id};
|
312 | this._union({identifiers, data, modifiers, callback});
|
313 | }
|
314 |
|
315 | /**
|
316 | people.unset(distinct_id, prop, modifiers, callback)
|
317 | ---
|
318 | delete a property on an user record in engage
|
319 |
|
320 | usage:
|
321 |
|
322 | mixpanel.people.unset('bob', 'page_views');
|
323 |
|
324 | mixpanel.people.unset('bob', ['page_views', 'last_login']);
|
325 | */
|
326 | unset(distinct_id, prop, modifiers, callback) {
|
327 | const identifiers = {$distinct_id: distinct_id};
|
328 | this._unset({identifiers, prop, modifiers, callback});
|
329 | }
|
330 | };
|
331 |
|
332 | exports.MixpanelPeople = MixpanelPeople;
|