@mparticle/web-amplitude-kit 2.3.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +1 -1
- package/dist/Amplitude.common.js +6 -8
- package/dist/Amplitude.esm.js +790 -0
- package/dist/Amplitude.iife.js +799 -0
- package/package.json +38 -28
|
@@ -0,0 +1,799 @@
|
|
|
1
|
+
var mpAmplitudeKit = (function (exports) {
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-undef*/
|
|
4
|
+
//
|
|
5
|
+
// Copyright 2015 mParticle, Inc.
|
|
6
|
+
//
|
|
7
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
// you may not use this file except in compliance with the License.
|
|
9
|
+
// You may obtain a copy of the License at
|
|
10
|
+
//
|
|
11
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
//
|
|
13
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
// See the License for the specific language governing permissions and
|
|
17
|
+
// limitations under the License.
|
|
18
|
+
|
|
19
|
+
var name = 'Amplitude',
|
|
20
|
+
moduleId = 53,
|
|
21
|
+
MessageType = {
|
|
22
|
+
SessionStart: 1,
|
|
23
|
+
SessionEnd: 2,
|
|
24
|
+
PageView: 3,
|
|
25
|
+
PageEvent: 4,
|
|
26
|
+
CrashReport: 5,
|
|
27
|
+
OptOut: 6,
|
|
28
|
+
Commerce: 16,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
var constants = {
|
|
32
|
+
MPID: 'mpId',
|
|
33
|
+
customerId: 'customerId',
|
|
34
|
+
email: 'email',
|
|
35
|
+
other: 'other',
|
|
36
|
+
other2: 'other2',
|
|
37
|
+
other3: 'other3',
|
|
38
|
+
other4: 'other4',
|
|
39
|
+
other5: 'other5',
|
|
40
|
+
other6: 'other6',
|
|
41
|
+
other7: 'other7',
|
|
42
|
+
other8: 'other8',
|
|
43
|
+
other9: 'other9',
|
|
44
|
+
other10: 'other10',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
var MP_AMP_SPLIT = 'mparticle_amplitude_should_split',
|
|
48
|
+
TOTAL_AMOUNT = 'Total Amount',
|
|
49
|
+
TOTAL = 'Total',
|
|
50
|
+
PRODUCTS = 'products',
|
|
51
|
+
REFUND = 'Refund',
|
|
52
|
+
PURCHASE = 'Purchase',
|
|
53
|
+
TOTAL_PRODUCT_AMOUNT = 'Total Product Amount';
|
|
54
|
+
|
|
55
|
+
var includeIndividualProductEvents,
|
|
56
|
+
shouldSendSeparateAmplitudeRevenueEvent,
|
|
57
|
+
enableTempAmplitudeEcommerce;
|
|
58
|
+
|
|
59
|
+
/* eslint-disable */
|
|
60
|
+
// prettier-ignore
|
|
61
|
+
var renderSnippet = function() {
|
|
62
|
+
(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script");
|
|
63
|
+
r.type="text/javascript";
|
|
64
|
+
r.integrity="sha384-QahB0HKETlcqjneomU3Ohs+UgJTinhUNFIKJitEl2Vo7DjvphO2jei64ZP5J2GA5";
|
|
65
|
+
r.crossOrigin="anonymous";r.async=true;
|
|
66
|
+
r.src="https://cdn.amplitude.com/libs/amplitude-8.21.8-min.gz.js";
|
|
67
|
+
r.onload=function(){if(!e.amplitude.runQueuedFunctions){console.log(
|
|
68
|
+
"[Amplitude] Error: could not load SDK");}};var s=t.getElementsByTagName("script"
|
|
69
|
+
)[0];s.parentNode.insertBefore(r,s);function i(e,t){e.prototype[t]=function(){
|
|
70
|
+
this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this};}
|
|
71
|
+
var o=function(){this._q=[];return this};var a=["add","append","clearAll",
|
|
72
|
+
"prepend","set","setOnce","unset","preInsert","postInsert","remove"];for(
|
|
73
|
+
var c=0;c<a.length;c++){i(o,a[c]);}n.Identify=o;var l=function(){this._q=[];
|
|
74
|
+
return this};var p=["setProductId","setQuantity","setPrice","setRevenueType",
|
|
75
|
+
"setEventProperties"];for(var u=0;u<p.length;u++){i(l,p[u]);}n.Revenue=l;var d=[
|
|
76
|
+
"init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut",
|
|
77
|
+
"setVersionName","setDomain","setDeviceId","enableTracking",
|
|
78
|
+
"setGlobalUserProperties","identify","clearUserProperties","setGroup",
|
|
79
|
+
"logRevenueV2","regenerateDeviceId","groupIdentify","onInit","onNewSessionStart"
|
|
80
|
+
,"logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId",
|
|
81
|
+
"getDeviceId","getUserId","setMinTimeBetweenSessionsMillis",
|
|
82
|
+
"setEventUploadThreshold","setUseDynamicConfig","setServerZone","setServerUrl",
|
|
83
|
+
"sendEvents","setLibrary","setTransport"];function v(t){function e(e){t[e
|
|
84
|
+
]=function(){t._q.push([e].concat(Array.prototype.slice.call(arguments,0)));};}
|
|
85
|
+
for(var n=0;n<d.length;n++){e(d[n]);}}v(n);n.getInstance=function(e){e=(
|
|
86
|
+
!e||e.length===0?"$default_instance":e).toLowerCase();if(
|
|
87
|
+
!Object.prototype.hasOwnProperty.call(n._iq,e)){n._iq[e]={_q:[]};v(n._iq[e]);}
|
|
88
|
+
return n._iq[e]};e.amplitude=n;})(window,document);
|
|
89
|
+
/* eslint-enable */
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
var constructor = function() {
|
|
93
|
+
var self = this,
|
|
94
|
+
isInitialized = false,
|
|
95
|
+
forwarderSettings,
|
|
96
|
+
reportingService,
|
|
97
|
+
isDefaultInstance;
|
|
98
|
+
|
|
99
|
+
self.name = name;
|
|
100
|
+
|
|
101
|
+
function getInstance() {
|
|
102
|
+
if (isDefaultInstance) {
|
|
103
|
+
return window.amplitude.getInstance();
|
|
104
|
+
} else {
|
|
105
|
+
return window.amplitude.getInstance(forwarderSettings.instanceName);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function getIdentityTypeName(identityType) {
|
|
110
|
+
return mParticle.IdentityType.getName(identityType);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function processEvent(event) {
|
|
114
|
+
var reportEvent = false;
|
|
115
|
+
|
|
116
|
+
if (isInitialized) {
|
|
117
|
+
try {
|
|
118
|
+
if (event.EventDataType === MessageType.PageView) {
|
|
119
|
+
reportEvent = true;
|
|
120
|
+
logPageView(event);
|
|
121
|
+
} else if (event.EventDataType === MessageType.Commerce) {
|
|
122
|
+
reportEvent = logCommerce(event);
|
|
123
|
+
} else if (event.EventDataType === MessageType.PageEvent) {
|
|
124
|
+
reportEvent = true;
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
event.EventCategory ===
|
|
128
|
+
window.mParticle.EventType.Transaction
|
|
129
|
+
) {
|
|
130
|
+
logTransaction(event);
|
|
131
|
+
} else {
|
|
132
|
+
logEvent(event);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (reportEvent && reportingService) {
|
|
137
|
+
reportingService(self, event);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return 'Successfully sent to ' + name;
|
|
141
|
+
} catch (e) {
|
|
142
|
+
return 'Failed to send to: ' + name + ' ' + e;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return 'Cannot send to forwarder ' + name + ', not initialized';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function setUserIdentity(id, type) {
|
|
150
|
+
if (window.mParticle.getVersion()[0] !== '1') {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (isInitialized) {
|
|
154
|
+
if (type === window.mParticle.IdentityType.CustomerId) {
|
|
155
|
+
getInstance().setUserId(id);
|
|
156
|
+
} else {
|
|
157
|
+
setUserAttribute(getIdentityTypeName(type), id);
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
return (
|
|
161
|
+
'Cannot call setUserIdentity on forwarder ' +
|
|
162
|
+
name +
|
|
163
|
+
', not initialized'
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function onUserIdentified(user) {
|
|
169
|
+
var userId;
|
|
170
|
+
|
|
171
|
+
if (isInitialized) {
|
|
172
|
+
var userIdentities = user.getUserIdentities().userIdentities;
|
|
173
|
+
|
|
174
|
+
// Additional check for email to match server
|
|
175
|
+
if (forwarderSettings.includeEmailAsUserProperty === 'True') {
|
|
176
|
+
setUserAttribute('email', userIdentities.email);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
switch (forwarderSettings.userIdentification) {
|
|
181
|
+
case constants.MPID:
|
|
182
|
+
userId = user.getMPID();
|
|
183
|
+
break;
|
|
184
|
+
// server returns `customerId` whereas key on userIdentities object is `customerid`
|
|
185
|
+
case constants.customerId:
|
|
186
|
+
userId = userIdentities.customerid;
|
|
187
|
+
break;
|
|
188
|
+
case constants.email:
|
|
189
|
+
userId = userIdentities.email;
|
|
190
|
+
break;
|
|
191
|
+
case constants.other:
|
|
192
|
+
userId = userIdentities.other;
|
|
193
|
+
break;
|
|
194
|
+
case constants.other2:
|
|
195
|
+
userId = userIdentities.other2;
|
|
196
|
+
break;
|
|
197
|
+
case constants.other3:
|
|
198
|
+
userId = userIdentities.other3;
|
|
199
|
+
break;
|
|
200
|
+
case constants.other4:
|
|
201
|
+
userId = userIdentities.other4;
|
|
202
|
+
break;
|
|
203
|
+
case constants.other5:
|
|
204
|
+
userId = userIdentities.other5;
|
|
205
|
+
break;
|
|
206
|
+
case constants.other6:
|
|
207
|
+
userId = userIdentities.other6;
|
|
208
|
+
break;
|
|
209
|
+
case constants.other7:
|
|
210
|
+
userId = userIdentities.other7;
|
|
211
|
+
break;
|
|
212
|
+
case constants.other8:
|
|
213
|
+
userId = userIdentities.other8;
|
|
214
|
+
break;
|
|
215
|
+
case constants.other9:
|
|
216
|
+
userId = userIdentities.other9;
|
|
217
|
+
break;
|
|
218
|
+
case constants.other10:
|
|
219
|
+
userId = userIdentities.other10;
|
|
220
|
+
break;
|
|
221
|
+
default:
|
|
222
|
+
userId = null;
|
|
223
|
+
}
|
|
224
|
+
if (userId) {
|
|
225
|
+
return getInstance().setUserId(userId);
|
|
226
|
+
} else {
|
|
227
|
+
console.warn(
|
|
228
|
+
'A user identification type of ' +
|
|
229
|
+
forwarderSettings.userIdentification +
|
|
230
|
+
' was selected in mParticle dashboard, but was not passed to the identity call. Please check your implementation.'
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {
|
|
234
|
+
console.error(
|
|
235
|
+
'Error calling onUserIdentified on forwarder ' + name
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
return (
|
|
240
|
+
'Cannot call onUserIdentified on forwarder ' +
|
|
241
|
+
name +
|
|
242
|
+
', not initialized'
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function removeUserAttribute(key) {
|
|
248
|
+
if (isInitialized) {
|
|
249
|
+
if (
|
|
250
|
+
forwarderSettings.allowUnsetUserAttributes &&
|
|
251
|
+
forwarderSettings.allowUnsetUserAttributes === 'True'
|
|
252
|
+
) {
|
|
253
|
+
try {
|
|
254
|
+
var identify = new window.amplitude.Identify().unset(key);
|
|
255
|
+
getInstance().identify(identify);
|
|
256
|
+
|
|
257
|
+
return 'Successfully unset Amplitude user property: ' + key;
|
|
258
|
+
} catch (e) {
|
|
259
|
+
return 'Failed to call unset on ' + name + ' ' + e;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
return (
|
|
264
|
+
'Cannot call removeUserAttribute on forwarder ' +
|
|
265
|
+
name +
|
|
266
|
+
', not initialized'
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function setUserAttribute(key, value) {
|
|
272
|
+
if (isInitialized) {
|
|
273
|
+
try {
|
|
274
|
+
var attributeDict = {};
|
|
275
|
+
attributeDict[key] = value;
|
|
276
|
+
getInstance().setUserProperties(attributeDict);
|
|
277
|
+
|
|
278
|
+
return 'Successfully called setUserProperties API on ' + name;
|
|
279
|
+
} catch (e) {
|
|
280
|
+
return (
|
|
281
|
+
'Failed to call SET setUserProperties on ' + name + ' ' + e
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
} else {
|
|
285
|
+
return (
|
|
286
|
+
'Cannot call setUserAttribute on forwarder ' +
|
|
287
|
+
name +
|
|
288
|
+
', not initialized'
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function setOptOut(isOptingOut) {
|
|
294
|
+
if (isInitialized) {
|
|
295
|
+
getInstance().setOptOut(isOptingOut);
|
|
296
|
+
} else {
|
|
297
|
+
return (
|
|
298
|
+
'Cannot call setOptOut on forwarder ' +
|
|
299
|
+
name +
|
|
300
|
+
', not initialized'
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function logPageView(data) {
|
|
306
|
+
if (data.EventAttributes) {
|
|
307
|
+
data.EventAttributes = convertJsonAttrs(data.EventAttributes);
|
|
308
|
+
getInstance().logEvent(
|
|
309
|
+
'Viewed ' + data.EventName,
|
|
310
|
+
data.EventAttributes
|
|
311
|
+
);
|
|
312
|
+
} else {
|
|
313
|
+
getInstance().logEvent('Viewed ' + data.EventName);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function logEvent(data) {
|
|
318
|
+
if (data.EventAttributes) {
|
|
319
|
+
data.EventAttributes = convertJsonAttrs(data.EventAttributes);
|
|
320
|
+
getInstance().logEvent(data.EventName, data.EventAttributes);
|
|
321
|
+
} else {
|
|
322
|
+
getInstance().logEvent(data.EventName);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function logTransaction(data) {
|
|
327
|
+
if (
|
|
328
|
+
!data.EventAttributes ||
|
|
329
|
+
!data.EventAttributes.$MethodName ||
|
|
330
|
+
!data.EventAttributes.$MethodName === 'LogEcommerceTransaction'
|
|
331
|
+
) {
|
|
332
|
+
// User didn't use logTransaction method, so just log normally
|
|
333
|
+
logEvent(data);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
getInstance().logRevenue(
|
|
338
|
+
data.EventAttributes.RevenueAmount,
|
|
339
|
+
data.EventAttributes.ProductQuantity,
|
|
340
|
+
data.EventAttributes.ProductSKU.toString()
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function createEcommerceAttributes(attributes) {
|
|
345
|
+
var updatedAttributes = {};
|
|
346
|
+
for (var key in attributes) {
|
|
347
|
+
if (key !== TOTAL_AMOUNT && key !== TOTAL_PRODUCT_AMOUNT) {
|
|
348
|
+
updatedAttributes[key] = attributes[key];
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return convertJsonAttrs(updatedAttributes);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function logCommerce(event) {
|
|
356
|
+
var expandedEvents = mParticle.eCommerce.expandCommerceEvent(event);
|
|
357
|
+
// if Product Action exists, it's a regular product action commerce event
|
|
358
|
+
if (event.ProductAction) {
|
|
359
|
+
if (enableTempAmplitudeEcommerce) {
|
|
360
|
+
return processTemporaryProductAction(event, expandedEvents);
|
|
361
|
+
} else {
|
|
362
|
+
// TODO: Remove this old code path when Amplitude is ready
|
|
363
|
+
var isRefund, isPurchase, logRevenue;
|
|
364
|
+
isRefund =
|
|
365
|
+
event.ProductAction.ProductActionType ===
|
|
366
|
+
mParticle.ProductActionType.Refund;
|
|
367
|
+
isPurchase =
|
|
368
|
+
event.ProductAction.ProductActionType ===
|
|
369
|
+
mParticle.ProductActionType.Purchase;
|
|
370
|
+
logRevenue = isRefund || isPurchase;
|
|
371
|
+
expandedEvents.forEach(function(expandedEvt) {
|
|
372
|
+
// Exclude Totals from the attributes as we log it in the revenue call
|
|
373
|
+
var updatedAttributes = createEcommerceAttributes(
|
|
374
|
+
expandedEvt.EventAttributes
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
// Purchase and Refund events generate an additional 'Total' event
|
|
378
|
+
if (
|
|
379
|
+
logRevenue &&
|
|
380
|
+
expandedEvt.EventName.indexOf('Total') > -1
|
|
381
|
+
) {
|
|
382
|
+
var revenueAmount =
|
|
383
|
+
(expandedEvt.EventAttributes['Total Amount'] || 0) *
|
|
384
|
+
(isRefund ? -1 : 1);
|
|
385
|
+
var revenue = new window.amplitude.Revenue()
|
|
386
|
+
.setPrice(revenueAmount)
|
|
387
|
+
.setEventProperties(updatedAttributes);
|
|
388
|
+
getInstance().logRevenueV2(revenue);
|
|
389
|
+
} else {
|
|
390
|
+
getInstance().logEvent(
|
|
391
|
+
expandedEvt.EventName,
|
|
392
|
+
updatedAttributes
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// if it is not a product action, it is an impression or promotion commerce event
|
|
402
|
+
if (isNotProductAction(event)) {
|
|
403
|
+
expandedEvents.forEach(function(expandedEvt) {
|
|
404
|
+
// Exclude Totals from the attributes as we log it in the revenue call
|
|
405
|
+
var updatedAttributes = createEcommerceAttributes(
|
|
406
|
+
expandedEvt.EventAttributes
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
getInstance().logEvent(
|
|
410
|
+
expandedEvt.EventName,
|
|
411
|
+
updatedAttributes
|
|
412
|
+
);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
console.warn(
|
|
419
|
+
'Commerce event does not conform to our expectations and was not forwarded to Amplitude. Please double-check your code.'
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/*
|
|
426
|
+
When we process a product action event, Amplitude has a very specific way of
|
|
427
|
+
sending events to them:
|
|
428
|
+
|
|
429
|
+
1. Send a summary event with event attributes from the MP event.
|
|
430
|
+
a. Add a key of `products` with a value of JSON.stringify(productArray).
|
|
431
|
+
b. Add a key of mparticle_amplitude_should_split with a value of `false`.
|
|
432
|
+
|
|
433
|
+
2. Determine if we send product level events or not.
|
|
434
|
+
a. If includeIndividualProductEvents === true, send product level events
|
|
435
|
+
b. If includeIndividualProductEvents === false, do not send product level events
|
|
436
|
+
|
|
437
|
+
3. Determine if we send an Amplitude revenue event or not.
|
|
438
|
+
a. If shouldSendSeparateAmplitudeRevenueEvent === true, send an Amplitude revenue event.
|
|
439
|
+
b. If shouldSendSeparateAmplitudeRevenueEvent === true, the summary event attribute should be `revenue`.
|
|
440
|
+
c. If shouldSendSeparateAmplitudeRevenueEvent === false, the summary event attribute should be `$revenue`
|
|
441
|
+
|
|
442
|
+
See test/AmplitudeCommerceEvent.MD for examples of what the expectations of the payload are.
|
|
443
|
+
|
|
444
|
+
*/
|
|
445
|
+
function processTemporaryProductAction(
|
|
446
|
+
unexpandedCommerceEvent,
|
|
447
|
+
expandedEvents
|
|
448
|
+
) {
|
|
449
|
+
var summaryEvent, isRefund, isPurchase, isMPRevenueEvent;
|
|
450
|
+
|
|
451
|
+
isRefund =
|
|
452
|
+
unexpandedCommerceEvent.ProductAction.ProductActionType ===
|
|
453
|
+
mParticle.ProductActionType.Refund;
|
|
454
|
+
isPurchase =
|
|
455
|
+
unexpandedCommerceEvent.ProductAction.ProductActionType ===
|
|
456
|
+
mParticle.ProductActionType.Purchase;
|
|
457
|
+
isMPRevenueEvent = isRefund || isPurchase;
|
|
458
|
+
|
|
459
|
+
// if the event is a revenue event, then we set it to the expanded `Total` event for backwards compatibility
|
|
460
|
+
if (
|
|
461
|
+
isMPRevenueEvent &&
|
|
462
|
+
expandedEvents[0].EventName.indexOf(TOTAL) > -1
|
|
463
|
+
) {
|
|
464
|
+
summaryEvent = expandedEvents[0];
|
|
465
|
+
sendMPRevenueSummaryEvent(
|
|
466
|
+
summaryEvent,
|
|
467
|
+
unexpandedCommerceEvent.ProductAction.ProductList,
|
|
468
|
+
isRefund,
|
|
469
|
+
shouldSendSeparateAmplitudeRevenueEvent
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (!isMPRevenueEvent) {
|
|
474
|
+
sendSummaryEvent(unexpandedCommerceEvent);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (includeIndividualProductEvents) {
|
|
478
|
+
sendIndividualProductEvents(
|
|
479
|
+
expandedEvents,
|
|
480
|
+
isMPRevenueEvent,
|
|
481
|
+
shouldSendSeparateAmplitudeRevenueEvent,
|
|
482
|
+
isRefund
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// If event.ProductAction does not exist, the commerce event is a promotion or impression event
|
|
490
|
+
function isNotProductAction(event) {
|
|
491
|
+
return (
|
|
492
|
+
event.EventCategory ===
|
|
493
|
+
mParticle.CommerceEventType.ProductImpression ||
|
|
494
|
+
event.EventCategory === mParticle.CommerceEventType.PromotionView ||
|
|
495
|
+
event.EventCategory === mParticle.CommerceEventType.PromotionClick
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// this function does not use Amplitude's logRevenueV2, but rather sends custom event names
|
|
500
|
+
function sendMPRevenueSummaryEvent(
|
|
501
|
+
summaryEvent,
|
|
502
|
+
products,
|
|
503
|
+
isRefund,
|
|
504
|
+
shouldSendSeparateAmplitudeRevenueEvent
|
|
505
|
+
) {
|
|
506
|
+
// send the ecommerce - purchase event
|
|
507
|
+
var updatedAttributes = createMPRevenueEcommerceAttributes(
|
|
508
|
+
summaryEvent.EventAttributes,
|
|
509
|
+
shouldSendSeparateAmplitudeRevenueEvent,
|
|
510
|
+
isRefund
|
|
511
|
+
);
|
|
512
|
+
updatedAttributes[MP_AMP_SPLIT] = false;
|
|
513
|
+
|
|
514
|
+
updatedAttributes[PRODUCTS] = products;
|
|
515
|
+
var revenueEventLabel = isRefund ? REFUND : PURCHASE;
|
|
516
|
+
getInstance().logEvent(
|
|
517
|
+
'eCommerce - ' + revenueEventLabel,
|
|
518
|
+
updatedAttributes
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// revenue summary event will either have $price/price or $revenue/revenue depending on if
|
|
523
|
+
function createMPRevenueEcommerceAttributes(
|
|
524
|
+
attributes,
|
|
525
|
+
shouldSendSeparateAmplitudeRevenueEvent,
|
|
526
|
+
isRefund
|
|
527
|
+
) {
|
|
528
|
+
var updatedAttributes = {};
|
|
529
|
+
for (var key in attributes) {
|
|
530
|
+
if (key === TOTAL_AMOUNT) {
|
|
531
|
+
// A purchase is a positive amount and a refund is negative
|
|
532
|
+
var revenueAmount = attributes[key] * (isRefund ? -1 : 1);
|
|
533
|
+
// If we send a separate Amplitude Revenue Event, Amplitude's
|
|
534
|
+
// SDK prefixes price/revenue with a $ for calculating things
|
|
535
|
+
// like LTV, so we do not want to prepend it as part of the
|
|
536
|
+
// summary event to avoid double counting
|
|
537
|
+
var revenueKey = shouldSendSeparateAmplitudeRevenueEvent
|
|
538
|
+
? 'revenue'
|
|
539
|
+
: '$revenue';
|
|
540
|
+
|
|
541
|
+
updatedAttributes[revenueKey] = revenueAmount;
|
|
542
|
+
} else if (key !== TOTAL_AMOUNT) {
|
|
543
|
+
updatedAttributes[key] = attributes[key];
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return convertJsonAttrs(updatedAttributes);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function createAttrsForAmplitudeRevenueEvent(attributes) {
|
|
551
|
+
var updatedAttributes = {};
|
|
552
|
+
for (var key in attributes) {
|
|
553
|
+
if (key !== TOTAL_AMOUNT) {
|
|
554
|
+
updatedAttributes[key] = attributes[key];
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
return convertJsonAttrs(updatedAttributes);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function sendSummaryEvent(summaryEvent) {
|
|
562
|
+
var updatedAttributes = createEcommerceAttributes(
|
|
563
|
+
summaryEvent.EventAttributes
|
|
564
|
+
);
|
|
565
|
+
updatedAttributes[MP_AMP_SPLIT] = false;
|
|
566
|
+
try {
|
|
567
|
+
updatedAttributes[PRODUCTS] =
|
|
568
|
+
summaryEvent.ProductAction.ProductList;
|
|
569
|
+
} catch (e) {
|
|
570
|
+
console.error('error adding Product List to summary event');
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
getInstance().logEvent(summaryEvent.EventName, updatedAttributes);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function sendIndividualProductEvents(
|
|
577
|
+
expandedEvents,
|
|
578
|
+
isMPRevenueEvent,
|
|
579
|
+
shouldSendSeparateAmplitudeRevenueEvent,
|
|
580
|
+
isRefund
|
|
581
|
+
) {
|
|
582
|
+
expandedEvents.forEach(function(expandedEvt) {
|
|
583
|
+
var updatedAttributes;
|
|
584
|
+
// `Total` exists on an expanded event if it is part of a revenue/purchase event
|
|
585
|
+
// but not on other commerce events. This only needs to be fired if shouldSendSeparateAmplitudeRevenueEvent === True
|
|
586
|
+
if (
|
|
587
|
+
isMPRevenueEvent &&
|
|
588
|
+
// A purchase is a positive amount and a refund is negative
|
|
589
|
+
(expandedEvt.EventName.indexOf(TOTAL) > -1) &
|
|
590
|
+
shouldSendSeparateAmplitudeRevenueEvent
|
|
591
|
+
) {
|
|
592
|
+
var revenueAmount =
|
|
593
|
+
// A purchase is a positive amount and a refund is negative
|
|
594
|
+
(expandedEvt.EventAttributes[TOTAL_AMOUNT] || 0) *
|
|
595
|
+
(isRefund ? -1 : 1);
|
|
596
|
+
updatedAttributes = createAttrsForAmplitudeRevenueEvent(
|
|
597
|
+
expandedEvt.EventAttributes
|
|
598
|
+
);
|
|
599
|
+
|
|
600
|
+
var revenue = new window.amplitude.Revenue()
|
|
601
|
+
.setPrice(revenueAmount)
|
|
602
|
+
.setEventProperties(updatedAttributes);
|
|
603
|
+
getInstance().logRevenueV2(revenue);
|
|
604
|
+
} else if (expandedEvt.EventName.indexOf(TOTAL) === -1) {
|
|
605
|
+
updatedAttributes = createEcommerceAttributes(
|
|
606
|
+
expandedEvt.EventAttributes
|
|
607
|
+
);
|
|
608
|
+
getInstance().logEvent(
|
|
609
|
+
expandedEvt.EventName,
|
|
610
|
+
updatedAttributes
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function convertJsonAttrs(customAttributes) {
|
|
617
|
+
if (forwarderSettings.sendEventAttributesAsObjects === 'True') {
|
|
618
|
+
for (var key in customAttributes) {
|
|
619
|
+
if (typeof customAttributes[key] === 'string') {
|
|
620
|
+
try {
|
|
621
|
+
var parsed = JSON.parse(customAttributes[key]);
|
|
622
|
+
if (typeof parsed === 'object') {
|
|
623
|
+
customAttributes[key] = parsed;
|
|
624
|
+
}
|
|
625
|
+
} catch (e) {
|
|
626
|
+
// if parsing fails, don't update the customAttribute object
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return customAttributes;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function initForwarder(settings, service, testMode) {
|
|
636
|
+
var ampSettings;
|
|
637
|
+
|
|
638
|
+
forwarderSettings = settings;
|
|
639
|
+
reportingService = service;
|
|
640
|
+
|
|
641
|
+
// Changing this setting from a negative action to a positive action for readability
|
|
642
|
+
includeIndividualProductEvents =
|
|
643
|
+
forwarderSettings.excludeIndividualProductEvents === 'False';
|
|
644
|
+
// Only send separate amplitude revenue events when we includeIndividualProductEvents,
|
|
645
|
+
// so create this variable for clarity
|
|
646
|
+
shouldSendSeparateAmplitudeRevenueEvent = includeIndividualProductEvents;
|
|
647
|
+
|
|
648
|
+
enableTempAmplitudeEcommerce =
|
|
649
|
+
forwarderSettings.enableTempAmplitudeEcommerce === 'True';
|
|
650
|
+
try {
|
|
651
|
+
if (!window.amplitude) {
|
|
652
|
+
if (testMode !== true) {
|
|
653
|
+
renderSnippet();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
ampSettings = {};
|
|
658
|
+
|
|
659
|
+
// allow the client to set custom amplitude init properties
|
|
660
|
+
if (
|
|
661
|
+
typeof window.AmplitudeInitSettings === 'object' &&
|
|
662
|
+
window.AmplitudeInitSettings !== null
|
|
663
|
+
) {
|
|
664
|
+
ampSettings = window.AmplitudeInitSettings;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (forwarderSettings.saveEvents) {
|
|
668
|
+
ampSettings.saveEvents =
|
|
669
|
+
forwarderSettings.saveEvents === 'True';
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (forwarderSettings.savedMaxCount) {
|
|
673
|
+
ampSettings.savedMaxCount = parseInt(
|
|
674
|
+
forwarderSettings.savedMaxCount,
|
|
675
|
+
10
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
if (forwarderSettings.uploadBatchSize) {
|
|
680
|
+
ampSettings.uploadBatchSize = parseInt(
|
|
681
|
+
forwarderSettings.uploadBatchSize,
|
|
682
|
+
10
|
|
683
|
+
);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (forwarderSettings.includeUtm) {
|
|
687
|
+
ampSettings.includeUtm =
|
|
688
|
+
forwarderSettings.includeUtm === 'True';
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (forwarderSettings.includeReferrer) {
|
|
692
|
+
ampSettings.includeReferrer =
|
|
693
|
+
forwarderSettings.includeReferrer === 'True';
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (forwarderSettings.forceHttps) {
|
|
697
|
+
ampSettings.forceHttps =
|
|
698
|
+
forwarderSettings.forceHttps === 'True';
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
if (forwarderSettings.baseUrl) {
|
|
702
|
+
ampSettings.apiEndpoint = forwarderSettings.baseUrl;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
isDefaultInstance =
|
|
706
|
+
!forwarderSettings.instanceName ||
|
|
707
|
+
forwarderSettings.instanceName === 'default';
|
|
708
|
+
|
|
709
|
+
getInstance().init(forwarderSettings.apiKey, null, ampSettings);
|
|
710
|
+
isInitialized = true;
|
|
711
|
+
|
|
712
|
+
if (forwarderSettings.userIdentification === constants.MPID) {
|
|
713
|
+
if (window.mParticle && window.mParticle.Identity) {
|
|
714
|
+
var user = window.mParticle.Identity.getCurrentUser();
|
|
715
|
+
if (user) {
|
|
716
|
+
var userId = user.getMPID();
|
|
717
|
+
getInstance().setUserId(userId);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return 'Successfully initialized: ' + name;
|
|
723
|
+
} catch (e) {
|
|
724
|
+
return 'Failed to initialize: ' + name;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
this.init = initForwarder;
|
|
729
|
+
this.process = processEvent;
|
|
730
|
+
this.setUserIdentity = setUserIdentity;
|
|
731
|
+
this.onUserIdentified = onUserIdentified;
|
|
732
|
+
this.setUserAttribute = setUserAttribute;
|
|
733
|
+
this.setOptOut = setOptOut;
|
|
734
|
+
this.removeUserAttribute = removeUserAttribute;
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
function getId() {
|
|
738
|
+
return moduleId;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function isObject(val) {
|
|
742
|
+
return (
|
|
743
|
+
val != null && typeof val === 'object' && Array.isArray(val) === false
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function register(config) {
|
|
748
|
+
if (!config) {
|
|
749
|
+
console.log(
|
|
750
|
+
'You must pass a config object to register the kit ' + name
|
|
751
|
+
);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
if (!isObject(config)) {
|
|
756
|
+
console.log(
|
|
757
|
+
'The "config" must be an object. You passed in a ' + typeof config
|
|
758
|
+
);
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
if (isObject(config.kits)) {
|
|
763
|
+
config.kits[name] = {
|
|
764
|
+
constructor: constructor,
|
|
765
|
+
};
|
|
766
|
+
} else {
|
|
767
|
+
config.kits = {};
|
|
768
|
+
config.kits[name] = {
|
|
769
|
+
constructor: constructor,
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
console.log(
|
|
773
|
+
'Successfully registered ' + name + ' to your mParticle configuration'
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
if (typeof window !== 'undefined') {
|
|
778
|
+
if (window && window.mParticle && window.mParticle.addForwarder) {
|
|
779
|
+
window.mParticle.addForwarder({
|
|
780
|
+
name: name,
|
|
781
|
+
constructor: constructor,
|
|
782
|
+
getId: getId,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
var Amplitude = {
|
|
788
|
+
register: register,
|
|
789
|
+
};
|
|
790
|
+
var Amplitude_1 = Amplitude.register;
|
|
791
|
+
|
|
792
|
+
exports["default"] = Amplitude;
|
|
793
|
+
exports.register = Amplitude_1;
|
|
794
|
+
|
|
795
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
796
|
+
|
|
797
|
+
return exports;
|
|
798
|
+
|
|
799
|
+
})({});
|