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