-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMixpanelEventForwarder.js
More file actions
476 lines (414 loc) · 16.3 KB
/
MixpanelEventForwarder.js
File metadata and controls
476 lines (414 loc) · 16.3 KB
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
/* eslint-disable no-undef*/
// Copyright 2015 mParticle, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ** Glossary of terms
// Mixpanel Terms:
// user_id: A unique identifier used by the Mixpanel.identify method to identify a unique user. This will map to what an mParticle customer selects in the UI, which translates to the forwarderSettings.userIdentificationType.
// device_id: A unique identifier used by Mixpanel to identify an anonymous user, usually a guid. mParticle and Mixpnael generate their own device ids.
// distinct_id: A unique identifier that Mixpanel uses to bridge a user and a device. Usually the
// distinct_id has a prefix of $device:guid<device_id> to denote an anonymouse user
// and this will be replaced by the user_id once the user has been identified by
// a request to Mixpanel.identify
// eslint-disable-next-line no-redeclare
var name = 'MixpanelEventForwarder',
moduleId = 10,
MessageType = {
SessionStart: 1,
SessionEnd: 2,
PageView: 3,
PageEvent: 4,
CrashReport: 5,
OptOut: 6,
Commerce: 16,
};
/* eslint-disable */
// prettier-ignore
var renderSnippet = function() {
(function(e,b){if (!b.__SV){var a,f,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";f=e.getElementsByTagName("script")[0];f.parentNode.insertBefore(a,f)}})(document,window.mixpanel||[]);
}
/* eslint-enable */
// eslint-disable-next-line no-redeclare
var constructor = function () {
var self = this,
isInitialized = false,
forwarderSettings = null,
reportingService = null,
useMixpanelPeople = false;
self.name = name;
function initForwarder(settings, service, testMode) {
forwarderSettings = settings;
useMixpanelPeople = forwarderSettings.useMixpanelPeople === 'True';
reportingService = service;
try {
if (!testMode) {
renderSnippet();
}
// Build init options object
var initOptions = {
api_host: forwarderSettings.baseUrl,
};
// Session Replay boolean settings
var boolSettings = [
{ key: 'recordHeatmapData', mappedKey: 'record_heatmap_data' },
{ key: 'autocapture', mappedKey: 'autocapture' },
{ key: 'recordCanvas', mappedKey: 'record_canvas' },
];
// Session Replay numeric settings
var numericSettings = [
{
key: 'recordSessionsPercent',
mappedKey: 'record_sessions_percent',
},
{
key: 'recordIdleTimeoutMs',
mappedKey: 'record_idle_timeout_ms',
},
{ key: 'recordMaxMs', mappedKey: 'record_max_ms' },
{ key: 'recordMinMs', mappedKey: 'record_min_ms' },
];
// Session Replay string settings
var stringSettings = [
{
key: 'recordMaskTextSelector',
mappedKey: 'record_mask_text_selector',
},
{
key: 'recordBlockSelector',
mappedKey: 'record_block_selector',
},
{ key: 'recordBlockClass', mappedKey: 'record_block_class' },
{
key: 'recordMaskTextClass',
mappedKey: 'record_mask_text_class',
},
];
// Process boolean settings
boolSettings.forEach(function (setting) {
if (forwarderSettings[setting.key] != null) {
initOptions[setting.mappedKey] =
forwarderSettings[setting.key] === 'True';
}
});
// Process numeric settings
numericSettings.forEach(function (setting) {
var numericValue = parseIntSafe(forwarderSettings[setting.key]);
if (numericValue !== undefined) {
initOptions[setting.mappedKey] = numericValue;
}
});
// Process string settings
stringSettings.forEach(function (setting) {
if (forwarderSettings[setting.key]) {
initOptions[setting.mappedKey] =
forwarderSettings[setting.key];
}
});
mixpanel.init(settings.token, initOptions, 'mparticle');
isInitialized = true;
return 'Successfully initialized: ' + name;
} catch (e) {
return 'Cannot initialize forwarder: ' + name + ': ' + e;
}
}
function processEvent(event) {
var reportEvent = false;
if (!isInitialized) {
return 'Cannot send to forwarder: ' + name + ', not initialized';
}
try {
if (event.EventDataType == MessageType.PageEvent) {
reportEvent = true;
logEvent(event);
} else if (event.EventDataType == MessageType.PageView) {
reportEvent = true;
logPageView(event);
} else if (
event.EventDataType == MessageType.Commerce &&
event.ProductAction &&
event.ProductAction.ProductActionType ==
window.mParticle.ProductActionType.Purchase
) {
reportEvent = true;
logCommerceEvent(event);
}
if (reportEvent && reportingService) {
reportingService(self, event);
}
return 'Successfully sent to forwarder: ' + name;
} catch (e) {
return 'Cannot send to forwarder: ' + name + ' ' + e;
}
}
function setUserIdentity(id, type) {
if (window.mParticle.getVersion()[0] !== '1') {
return;
}
if (!id) {
return (
'Cannot call setUserIdentity on forwarder: ' +
name +
' without ID'
);
}
if (!isInitialized) {
return (
'Cannot call setUserIdentity on forwarder: ' +
name +
', not initialized'
);
}
try {
if (window.mParticle.IdentityType.Alias == type) {
mixpanel.mparticle.alias(id.toString());
} else {
mixpanel.mparticle.identify(id.toString());
}
return 'Successfully called identify on forwarder: ' + name;
} catch (e) {
return 'Cannot call identify on forwarder: ' + name + ': ' + e;
}
}
function getUserIdentities(user) {
return user.getUserIdentities()
? user.getUserIdentities().userIdentities
: {};
}
function onLoginComplete(user) {
var userIdentities = getUserIdentities(user);
// When mParticle identifies a user, the user might
// actually be anonymous. We only want to send an
// identify request to Mixpanel if the user is
// actually known. Only known (logged in) users
// will have userIdentities.
if (!isEmpty(userIdentities)) {
sendIdentifyRequest(user, userIdentities);
} else {
return 'Logged in user does not have user identities and will not be sent to Mixpanel to Identify';
}
}
function onLogoutComplete() {
// For all Identity Requests, we run mixpanel.identify(<user_id>)
// as per Mixpanel's documentation https://docs.mixpanel.com/docs/tracking-methods/identifying-users
// except when a user logs out, where we run mixpanel.reset() to
// detach the Mixpanel distinct_id from the Mixpanel device_id
if (!isInitialized) {
return (
'Cannot call logout on forwarder: ' + name + ', not initialized'
);
}
try {
mixpanel.mparticle.reset();
return 'Successfully called reset on forwarder: ' + name;
} catch (e) {
return 'Cannot call reset on forwarder: ' + name + ': ' + e;
}
}
function onIdentifyComplete(user) {
// Mixpanel considers any user with an identity to be a known user.
// In mParticle, a user will always have an MPID even if they are anonymous.
// When mParticle identifies a user, because the user might
// actually be anonymous, we only want to send an
// identify request to Mixpanel if the user is
// actually known. If a user has any user identities, they are
// considered to be "known" users.
var userIdentities = getUserIdentities(user);
if (!isEmpty(userIdentities)) {
sendIdentifyRequest(user, userIdentities);
} else {
return 'Identified user does not have user identities and will not be sent to Mixpanel to Identify';
}
}
function onModifyComplete(user) {
// Mixpanel does not have the concept of modifying a
// user's identity. For the time being, we will rely on
// doing a simple Mixpanel.identify request for backwards compatibility. However
// this method may be deprecated in a future release
var userIdentities = getUserIdentities(user);
if (!isEmpty(userIdentities)) {
sendIdentifyRequest(user, userIdentities);
} else {
return 'Modified user does not have user identities and will not be sent to Mixpanel to Identify';
}
}
function sendIdentifyRequest(user, userIdentities) {
// We should only make Mixpanel.identify requests
// when a user has userIdentities and is therefore
// a known mParticle user and not anonymous
if (isEmpty(userIdentities)) {
return;
}
var idForMixpanel;
switch (forwarderSettings.userIdentificationType) {
case 'CustomerId':
idForMixpanel = userIdentities.customerid;
break;
case 'MPID':
idForMixpanel = user.getMPID();
break;
case 'Other':
idForMixpanel = userIdentities.other;
break;
case 'Other2':
idForMixpanel = userIdentities.other2;
break;
case 'Other3':
idForMixpanel = userIdentities.other3;
break;
case 'Other4':
idForMixpanel = userIdentities.other4;
break;
default:
idForMixpanel = userIdentities.customerid;
break;
}
if (!isInitialized) {
return (
'Cannot call identify on forwarder: ' +
name +
', not initialized'
);
}
try {
mixpanel.mparticle.identify(idForMixpanel);
return 'Successfully called identify on forwarder: ' + name;
} catch (e) {
return 'Cannot call identify on forwarder: ' + name + ': ' + e;
}
}
function setUserAttribute(key, value) {
var attr = {};
attr[key] = value;
try {
if (useMixpanelPeople) {
mixpanel.mparticle.people.set(attr);
} else {
mixpanel.mparticle.register(attr);
}
} catch (e) {
return 'Cannot call register on forwarder: ' + name + ': ' + e;
}
}
function removeUserAttribute(attribute) {
try {
if (useMixpanelPeople) {
mixpanel.mparticle.people.unset(attribute);
} else {
mixpanel.mparticle.unregister(attribute);
}
} catch (e) {
return 'Cannot call unregister on forwarder: ' + name + ': ' + e;
}
}
function logPageView(event) {
var eventName = 'Viewed ' + event.EventName;
event.EventAttributes = event.EventAttributes || {};
try {
mixpanel.mparticle.track(eventName, event.EventAttributes);
} catch (e) {
return 'Cannot log event on forwarder: ' + name + ': ' + e;
}
}
function logEvent(event) {
event.EventAttributes = event.EventAttributes || {};
try {
mixpanel.mparticle.track(event.EventName, event.EventAttributes);
} catch (e) {
return 'Cannot log event on forwarder: ' + name + ': ' + e;
}
}
function logCommerceEvent(event) {
if (!useMixpanelPeople) {
return (
'Cannot log commerce event on forwarder: ' +
name +
', useMixpanelPeople flag is not set'
);
}
try {
mixpanel.mparticle.people.track_charge(
event.ProductAction.TotalAmount,
{
$time: new Date().toISOString(),
}
);
} catch (e) {
return 'Cannot log commerce event on forwarder: ' + name + ': ' + e;
}
}
this.init = initForwarder;
this.process = processEvent;
this.setUserAttribute = setUserAttribute;
this.setUserIdentity = setUserIdentity;
this.removeUserAttribute = removeUserAttribute;
this.onIdentifyComplete = onIdentifyComplete;
this.onLoginComplete = onLoginComplete;
this.onLogoutComplete = onLogoutComplete;
this.onModifyComplete = onModifyComplete;
};
function getId() {
return moduleId;
}
function register(config) {
if (!config) {
console.log(
'You must pass a config object to register the kit ' + name
);
return;
}
if (!isObject(config)) {
console.log(
'"config" must be an object. You passed in a ' + typeof config
);
return;
}
if (isObject(config.kits)) {
config.kits[name] = {
constructor: constructor,
};
} else {
config.kits = {};
config.kits[name] = {
constructor: constructor,
};
}
console.log(
'Successfully registered ' + name + ' to your mParticle configuration'
);
}
function isEmpty(value) {
return value == null || !(Object.keys(value) || value).length;
}
function isObject(val) {
return (
val != null && typeof val === 'object' && Array.isArray(val) === false
);
}
function parseIntSafe(value) {
var n = parseInt(value, 10);
return isNaN(n) ? undefined : n;
}
if (typeof window !== 'undefined') {
if (window && window.mParticle && window.mParticle.addForwarder) {
window.mParticle.addForwarder({
name: name,
constructor: constructor,
getId: getId,
});
}
}
// eslint-disable-next-line no-undef
module.exports = {
register: register,
};