@mparticle/web-google-analytics-4-client-kit 1.0.2 → 1.0.3

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.
@@ -1,1318 +0,0 @@
1
- var GoogleAnalytics4Kit = (function (exports) {
2
- 'use strict';
3
-
4
- function Common() {}
5
-
6
- Common.prototype.forwarderSettings = null;
7
-
8
- Common.prototype.mergeObjects = function () {
9
- var resObj = {};
10
- for (var i = 0; i < arguments.length; i += 1) {
11
- var obj = arguments[i],
12
- keys = Object.keys(obj);
13
- for (var j = 0; j < keys.length; j += 1) {
14
- resObj[keys[j]] = obj[keys[j]];
15
- }
16
- }
17
- return resObj;
18
- };
19
-
20
- Common.prototype.getUserId = function (
21
- user,
22
- externalUserIdentityType,
23
- hashUserId
24
- ) {
25
- if (!user) {
26
- return;
27
- }
28
-
29
- var externalUserIdentityTypes = {
30
- none: 'None',
31
- customerId: 'CustomerId',
32
- other: 'Other',
33
- other2: 'Other2',
34
- other3: 'Other3',
35
- other4: 'Other4',
36
- other5: 'Other5',
37
- other6: 'Other6',
38
- other7: 'Other7',
39
- other8: 'Other8',
40
- other9: 'Other9',
41
- other10: 'Other10',
42
- mpid: 'mpid',
43
- };
44
-
45
- var userId,
46
- userIdentities = user.getUserIdentities().userIdentities;
47
- if (externalUserIdentityType !== externalUserIdentityTypes.none) {
48
- switch (externalUserIdentityType) {
49
- case 'CustomerId':
50
- userId = userIdentities.customerid;
51
- break;
52
- case externalUserIdentityTypes.other:
53
- userId = userIdentities.other;
54
- break;
55
- case externalUserIdentityTypes.other2:
56
- userId = userIdentities.other2;
57
- break;
58
- case externalUserIdentityTypes.other3:
59
- userId = userIdentities.other3;
60
- break;
61
- case externalUserIdentityTypes.other4:
62
- userId = userIdentities.other4;
63
- break;
64
- case externalUserIdentityTypes.other5:
65
- userId = userIdentities.other5;
66
- break;
67
- case externalUserIdentityTypes.other6:
68
- userId = userIdentities.other6;
69
- break;
70
- case externalUserIdentityTypes.other7:
71
- userId = userIdentities.other7;
72
- break;
73
- case externalUserIdentityTypes.other8:
74
- userId = userIdentities.other8;
75
- break;
76
- case externalUserIdentityTypes.other9:
77
- userId = userIdentities.other9;
78
- break;
79
- case externalUserIdentityTypes.other10:
80
- userId = userIdentities.other10;
81
- break;
82
- case externalUserIdentityTypes.mpid:
83
- userId = user.getMPID();
84
- break;
85
- default:
86
- console.warn(
87
- 'External identity type not found for setting identity on ' +
88
- 'GA4' +
89
- '. User not set. Please double check your implementation.'
90
- );
91
- }
92
- if (userId) {
93
- if (hashUserId == 'True') {
94
- userId = window.mParticle.generateHash(userId);
95
- }
96
- } else {
97
- console.warn(
98
- 'External identity type of ' +
99
- externalUserIdentityType +
100
- ' not set on the user'
101
- );
102
- }
103
- return userId;
104
- }
105
- };
106
-
107
- var common = Common;
108
-
109
- function CommerceHandler(common) {
110
- this.common = common || {};
111
- }
112
-
113
- var ProductActionTypes = {
114
- Unknown: 0,
115
- AddToCart: 10,
116
- Click: 14,
117
- Checkout: 12,
118
- CheckoutOption: 13,
119
- Impression: 22,
120
- Purchase: 16,
121
- Refund: 17,
122
- RemoveFromCart: 11,
123
- ViewDetail: 15,
124
- AddToWishlist: 20,
125
- };
126
-
127
- var PromotionActionTypes = {
128
- PromotionClick: 19,
129
- PromotionView: 18,
130
- };
131
-
132
- // Custom Flags
133
- var GA4_COMMERCE_EVENT_TYPE = 'GA4.CommerceEventType',
134
- GA4_SHIPPING_TIER = 'GA4.ShippingTier',
135
- GA4_PAYMENT_TYPE = 'GA4.PaymentType';
136
-
137
- var ADD_SHIPPING_INFO = 'add_shipping_info',
138
- ADD_PAYMENT_INFO = 'add_payment_info',
139
- VIEW_CART = 'view_cart';
140
-
141
- CommerceHandler.prototype.logCommerceEvent = function (event) {
142
- var needsCurrency = true,
143
- needsValue = true,
144
- ga4CommerceEventParameters,
145
- isViewCartEvent = false;
146
-
147
- // GA4 has a view_cart event which MP does not support via a ProductActionType
148
- // In order to log a view_cart event, pass ProductActionType.Unknown along with
149
- // the proper custom flag
150
- // Unknown ProductActionTypes without a custom flag will reach the switch statement
151
- // below and throw an error to the customer
152
- if (event.EventCategory === ProductActionTypes.Unknown) {
153
- if (
154
- event.CustomFlags &&
155
- event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
156
- ) {
157
- isViewCartEvent = true;
158
- return logViewCart(event);
159
- }
160
- }
161
- // Handle Impressions
162
- if (event.EventCategory === ProductActionTypes.Impression) {
163
- return logImpressionEvent(event);
164
- // Handle Promotions
165
- } else if (
166
- event.EventCategory === PromotionActionTypes.PromotionClick ||
167
- event.EventCategory === PromotionActionTypes.PromotionView
168
- ) {
169
- return logPromotionEvent(event);
170
- }
171
-
172
- ga4CommerceEventParameters = buildParameters(event);
173
- if (event.EventAttributes) {
174
- ga4CommerceEventParameters = this.common.mergeObjects(
175
- ga4CommerceEventParameters,
176
- event.EventAttributes
177
- );
178
- }
179
-
180
- switch (event.EventCategory) {
181
- case ProductActionTypes.AddToCart:
182
- case ProductActionTypes.RemoveFromCart:
183
- ga4CommerceEventParameters = buildAddOrRemoveCartItem(event);
184
- break;
185
- case ProductActionTypes.Checkout:
186
- ga4CommerceEventParameters = buildCheckout(event);
187
- break;
188
- case ProductActionTypes.Click:
189
- ga4CommerceEventParameters = buildSelectItem(event);
190
-
191
- needsCurrency = false;
192
- needsValue = false;
193
- break;
194
- case ProductActionTypes.Purchase:
195
- ga4CommerceEventParameters = buildPurchase(event);
196
- break;
197
- case ProductActionTypes.Refund:
198
- ga4CommerceEventParameters = buildRefund(event);
199
- break;
200
- case ProductActionTypes.ViewDetail:
201
- ga4CommerceEventParameters = buildViewItem(event);
202
- break;
203
- case ProductActionTypes.AddToWishlist:
204
- ga4CommerceEventParameters = buildAddToWishlist(event);
205
- break;
206
-
207
- case ProductActionTypes.CheckoutOption:
208
- return logCheckoutOptionEvent(event);
209
-
210
- default:
211
- // a view cart event is handled at the beginning of this function
212
- if (!isViewCartEvent) {
213
- console.error(
214
- 'Unsupported Commerce Event. Event not sent.',
215
- event
216
- );
217
- return false;
218
- }
219
- }
220
-
221
- // CheckoutOption, Promotions, and Impressions will not make it to this code
222
- if (needsCurrency) {
223
- ga4CommerceEventParameters.currency = event.CurrencyCode;
224
- }
225
-
226
- if (needsValue) {
227
- ga4CommerceEventParameters.value =
228
- (event.CustomFlags && event.CustomFlags['GA4.Value']) ||
229
- event.ProductAction.TotalAmount ||
230
- null;
231
- }
232
-
233
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
234
- return true;
235
- };
236
-
237
- function buildParameters(event) {
238
- return {
239
- items: buildProductsList(event.ProductAction.ProductList),
240
- coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
241
- };
242
- }
243
-
244
- function buildAddOrRemoveCartItem(event) {
245
- return {
246
- items: buildProductsList(event.ProductAction.ProductList),
247
- };
248
- }
249
-
250
- function buildCheckout(event) {
251
- return {
252
- items: buildProductsList(event.ProductAction.ProductList),
253
- coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
254
- };
255
- }
256
-
257
- function buildCheckoutOptions(event) {
258
- var parameters = event.EventAttributes;
259
- parameters.items = buildProductsList(event.ProductAction.ProductList);
260
- parameters.coupon = event.ProductAction
261
- ? event.ProductAction.CouponCode
262
- : null;
263
-
264
- return parameters;
265
- }
266
-
267
- function parseImpression(impression) {
268
- return {
269
- item_list_id: impression.ProductImpressionList,
270
- item_list_name: impression.ProductImpressionList,
271
- items: buildProductsList(impression.ProductList),
272
- };
273
- }
274
-
275
- function buildSelectItem(event) {
276
- return {
277
- items: buildProductsList(event.ProductAction.ProductList),
278
- };
279
- }
280
-
281
- function buildViewItem(event) {
282
- return {
283
- items: buildProductsList(event.ProductAction.ProductList),
284
- };
285
- }
286
-
287
- function buildPromotion(promotion) {
288
- return parsePromotion(promotion);
289
- }
290
-
291
- function buildPurchase(event) {
292
- return {
293
- transaction_id: event.ProductAction.TransactionId,
294
- value: event.ProductAction.TotalAmount,
295
- affiliation: event.ProductAction.Affiliation,
296
- coupon: event.ProductAction.CouponCode,
297
- shipping: event.ProductAction.ShippingAmount,
298
- tax: event.ProductAction.TaxAmount,
299
- items: buildProductsList(event.ProductAction.ProductList),
300
- };
301
- }
302
- function buildRefund(event) {
303
- return {
304
- transaction_id: event.ProductAction.TransactionId,
305
- value: event.ProductAction.TotalAmount,
306
- affiliation: event.ProductAction.Affiliation,
307
- coupon: event.ProductAction.CouponCode,
308
- shipping: event.ProductAction.ShippingAmount,
309
- tax: event.ProductAction.TaxAmount,
310
- items: buildProductsList(event.ProductAction.ProductList),
311
- };
312
- }
313
- function buildAddToWishlist(event) {
314
- return {
315
- value: event.ProductAction.TotalAmount,
316
- items: buildProductsList(event.ProductAction.ProductList),
317
- };
318
- }
319
-
320
- function buildAddShippingInfo(event) {
321
- return {
322
- coupon:
323
- event.ProductAction && event.ProductAction.CouponCode
324
- ? event.ProductAction.CouponCode
325
- : null,
326
- shipping_tier:
327
- event.CustomFlags && event.CustomFlags[GA4_SHIPPING_TIER]
328
- ? event.CustomFlags[GA4_SHIPPING_TIER]
329
- : null,
330
- items: buildProductsList(event.ProductAction.ProductList),
331
- };
332
- }
333
-
334
- function buildAddPaymentInfo(event) {
335
- return {
336
- coupon:
337
- event.ProductAction && event.ProductAction.CouponCode
338
- ? event.ProductAction.CouponCode
339
- : null,
340
- payment_type:
341
- event.CustomFlags && event.CustomFlags[GA4_PAYMENT_TYPE]
342
- ? event.CustomFlags[GA4_PAYMENT_TYPE]
343
- : null,
344
- items: buildProductsList(event.ProductAction.ProductList),
345
- };
346
- }
347
-
348
- // Utility function
349
- function toUnderscore(string) {
350
- return string
351
- .split(/(?=[A-Z])/)
352
- .join('_')
353
- .toLowerCase();
354
- }
355
-
356
- function parseProduct(_product) {
357
- var product = {};
358
-
359
- for (var key in _product) {
360
- switch (key) {
361
- case 'Sku':
362
- product.item_id = _product.Sku;
363
- break;
364
- case 'Name':
365
- product.item_name = _product.Name;
366
- break;
367
- case 'Brand':
368
- product.item_brand = _product.Brand;
369
- break;
370
- case 'Category':
371
- product.item_category = _product.Category;
372
- break;
373
- case 'Variant':
374
- product.item_variant = _product.Variant;
375
- break;
376
- default:
377
- product[toUnderscore(key)] = _product[key];
378
- }
379
- }
380
-
381
- return product;
382
- }
383
-
384
- function parsePromotion(_promotion) {
385
- var promotion = {};
386
-
387
- for (var key in _promotion) {
388
- switch (key) {
389
- case 'Id':
390
- promotion.promotion_id = _promotion.Id;
391
- break;
392
- case 'Creative':
393
- promotion.creative_name = _promotion.Creative;
394
- break;
395
- case 'Name':
396
- promotion.promotion_name = _promotion.Name;
397
- break;
398
- case 'Position':
399
- promotion.creative_slot = _promotion.Position;
400
- break;
401
- default:
402
- promotion[toUnderscore(key)] = _promotion[key];
403
- }
404
- }
405
-
406
- return promotion;
407
- }
408
-
409
- function buildProductsList(products) {
410
- var productsList = [];
411
-
412
- products.forEach(function (product) {
413
- productsList.push(parseProduct(product));
414
- });
415
-
416
- return productsList;
417
- }
418
-
419
- function mapGA4EcommerceEventName(event) {
420
- switch (event.EventCategory) {
421
- case ProductActionTypes.AddToCart:
422
- return 'add_to_cart';
423
- case ProductActionTypes.AddToWishlist:
424
- return 'add_to_wishlist';
425
- case ProductActionTypes.RemoveFromCart:
426
- return 'remove_from_cart';
427
- case ProductActionTypes.Purchase:
428
- return 'purchase';
429
- case ProductActionTypes.Checkout:
430
- return 'begin_checkout';
431
- case ProductActionTypes.CheckoutOption:
432
- return getCheckoutOptionEventName(event.CustomFlags);
433
- case ProductActionTypes.Click:
434
- return 'select_item';
435
- case ProductActionTypes.Impression:
436
- return 'view_item_list';
437
- case ProductActionTypes.Refund:
438
- return 'refund';
439
- case ProductActionTypes.ViewDetail:
440
- return 'view_item';
441
- case PromotionActionTypes.PromotionClick:
442
- return 'select_promotion';
443
- case PromotionActionTypes.PromotionView:
444
- return 'view_promotion';
445
- case ProductActionTypes.Unknown:
446
- if (
447
- event.CustomFlags &&
448
- event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
449
- ) {
450
- return 'view_cart';
451
- }
452
- break;
453
- default:
454
- console.error('Product Action Type not supported');
455
- return null;
456
- }
457
- }
458
-
459
- function getCheckoutOptionEventName(customFlags) {
460
- switch (customFlags[GA4_COMMERCE_EVENT_TYPE]) {
461
- case ADD_SHIPPING_INFO:
462
- return ADD_SHIPPING_INFO;
463
- case ADD_PAYMENT_INFO:
464
- return ADD_PAYMENT_INFO;
465
- default:
466
- return 'set_checkout_option';
467
- }
468
- }
469
-
470
- // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
471
- // Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
472
- // migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
473
- function logCheckoutOptionEvent(event) {
474
- try {
475
- var customFlags = event.CustomFlags,
476
- GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
477
- ga4CommerceEventParameters;
478
-
479
- // if no custom flags exist or there is no GA4CommerceEventType, the user has not updated their code from using legacy GA which still supports checkout_option
480
- if (!customFlags || !GA4CommerceEventType) {
481
- console.error(
482
- 'Your checkout option event for the Google Analytics 4 integration is missing a custom flag for "GA4.CommerceEventType". The event was sent using the deprecated set_checkout_option event. Review mParticle docs for GA4 for the custom flags to add.'
483
- );
484
- }
485
-
486
- switch (GA4CommerceEventType) {
487
- case ADD_SHIPPING_INFO:
488
- ga4CommerceEventParameters = buildAddShippingInfo(event);
489
- break;
490
- case ADD_PAYMENT_INFO:
491
- ga4CommerceEventParameters = buildAddPaymentInfo(event);
492
- break;
493
- default:
494
- ga4CommerceEventParameters = buildCheckoutOptions(event);
495
- break;
496
- }
497
- } catch (error) {
498
- console.error(
499
- 'There is an issue with the custom flags in your CheckoutOption event. The event was not sent. Plrease review the docs and fix.',
500
- error
501
- );
502
- return false;
503
- }
504
-
505
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
506
-
507
- return true;
508
- }
509
-
510
- function logPromotionEvent(event) {
511
- try {
512
- var ga4CommerceEventParameters;
513
- event.PromotionAction.PromotionList.forEach(function (promotion) {
514
- ga4CommerceEventParameters = buildPromotion(promotion);
515
- gtag(
516
- 'event',
517
- mapGA4EcommerceEventName(event),
518
- ga4CommerceEventParameters
519
- );
520
- });
521
- return true;
522
- } catch (error) {
523
- console.error(
524
- 'Error logging Promotions to GA4. Promotions not logged.',
525
- error
526
- );
527
- }
528
- return false;
529
- }
530
-
531
- function logImpressionEvent(event) {
532
- try {
533
- var ga4CommerceEventParameters;
534
- event.ProductImpressions.forEach(function (impression) {
535
- ga4CommerceEventParameters = parseImpression(impression);
536
-
537
- gtag(
538
- 'event',
539
- mapGA4EcommerceEventName(event),
540
- ga4CommerceEventParameters
541
- );
542
- });
543
- } catch (error) {
544
- console.log(
545
- 'Error logging Impressions to GA4. Impressions not logged',
546
- error
547
- );
548
- return false;
549
- }
550
- return true;
551
- }
552
-
553
- function logViewCart(event) {
554
- var ga4CommerceEventParameters = buildViewCart(event);
555
- ga4CommerceEventParameters.currency = event.CurrencyCode;
556
-
557
- ga4CommerceEventParameters.value =
558
- (event.CustomFlags && event.CustomFlags['GA4.Value']) ||
559
- event.ProductAction.TotalAmount ||
560
- null;
561
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
562
- return true;
563
- }
564
-
565
- function buildViewCart(event) {
566
- return {
567
- items: buildProductsList(event.ProductAction.ProductList),
568
- };
569
- }
570
-
571
- var commerceHandler = CommerceHandler;
572
-
573
- function EventHandler(common) {
574
- this.common = common || {};
575
- }
576
-
577
- EventHandler.prototype.logEvent = function (event) {
578
- gtag('event', event.EventName, event.EventAttributes || {});
579
- };
580
-
581
- EventHandler.prototype.logError = function () {
582
- console.warn('Google Analytics 4 does not support error events.');
583
- };
584
-
585
- EventHandler.prototype.logPageView = function (event) {
586
- var TITLE = 'Google.Title';
587
- var LOCATION = 'Google.Location';
588
- var pageTitle, pageLocation;
589
- if (event.CustomFlags.hasOwnProperty(TITLE)) {
590
- pageTitle = event.CustomFlags[TITLE];
591
- } else {
592
- pageTitle = document.title;
593
- }
594
-
595
- if (event.CustomFlags.hasOwnProperty(LOCATION)) {
596
- pageLocation = event.CustomFlags[LOCATION];
597
- } else {
598
- pageLocation = location.href;
599
- }
600
- gtag('event', 'page_view', {
601
- page_title: pageTitle,
602
- page_location: pageLocation,
603
- });
604
-
605
- return true;
606
- };
607
-
608
- var eventHandler = EventHandler;
609
-
610
- /*
611
- The 'mParticleUser' is an object with methods get user Identities and set/get user attributes
612
- Partners can determine what userIds are available to use in their SDK
613
- Call mParticleUser.getUserIdentities() to return an object of userIdentities --> { userIdentities: {customerid: '1234', email: 'email@gmail.com'} }
614
- For more identity types, see http://docs.mparticle.com/developers/sdk/javascript/identity#allowed-identity-types
615
- Call mParticleUser.getMPID() to get mParticle ID
616
- For any additional methods, see http://docs.mparticle.com/developers/sdk/javascript/apidocs/classes/mParticle.Identity.getCurrentUser().html
617
- */
618
-
619
- /*
620
- identityApiRequest has the schema:
621
- {
622
- userIdentities: {
623
- customerid: '123',
624
- email: 'abc'
625
- }
626
- }
627
- For more userIdentity types, see http://docs.mparticle.com/developers/sdk/javascript/identity#allowed-identity-types
628
- */
629
-
630
- function IdentityHandler(common) {
631
- this.common = common || {};
632
- }
633
- IdentityHandler.prototype.onUserIdentified = function (mParticleUser) {
634
- var userId = this.common.getUserId(
635
- mParticleUser,
636
- this.common.forwarderSettings.externalUserIdentityType,
637
- this.common.forwarderSettings.hashUserId
638
- );
639
- gtag('config', this.common.forwarderSettings.measurementId, {
640
- // do not send a page view when updating the userid in an identity call
641
- send_page_view: false,
642
- user_id: userId,
643
- });
644
- };
645
- IdentityHandler.prototype.onIdentifyComplete = function (
646
- mParticleUser,
647
- identityApiRequest
648
- ) {};
649
- IdentityHandler.prototype.onLoginComplete = function (
650
- mParticleUser,
651
- identityApiRequest
652
- ) {};
653
- IdentityHandler.prototype.onLogoutComplete = function (
654
- mParticleUser,
655
- identityApiRequest
656
- ) {};
657
- IdentityHandler.prototype.onModifyComplete = function (
658
- mParticleUser,
659
- identityApiRequest
660
- ) {};
661
-
662
- /* In previous versions of the mParticle web SDK, setting user identities on
663
- kits is only reachable via the onSetUserIdentity method below. We recommend
664
- filling out `onSetUserIdentity` for maximum compatibility
665
- */
666
- IdentityHandler.prototype.onSetUserIdentity = function (
667
- forwarderSettings,
668
- id,
669
- type
670
- ) {};
671
-
672
- var identityHandler = IdentityHandler;
673
-
674
- var initialization = {
675
- name: 'GoogleAnalytics4',
676
- /* ****** Fill out initForwarder to load your SDK ******
677
- Note that not all arguments may apply to your SDK initialization.
678
- These are passed from mParticle, but leave them even if they are not being used.
679
- forwarderSettings contain settings that your SDK requires in order to initialize
680
- userAttributes example: {gender: 'male', age: 25}
681
- userIdentities example: { 1: 'customerId', 2: 'facebookId', 7: 'emailid@email.com' }
682
- additional identityTypes can be found at https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101
683
- */
684
- initForwarder: function (
685
- forwarderSettings,
686
- testMode,
687
- userAttributes,
688
- userIdentities,
689
- processEvent,
690
- eventQueue,
691
- isInitialized,
692
- common
693
- ) {
694
- common.forwarderSettings = forwarderSettings;
695
- var measurementId = forwarderSettings.measurementId;
696
- var userIdType = forwarderSettings.externalUserIdentityType;
697
- var hashUserId = forwarderSettings.hashUserId;
698
- var configSettings = {
699
- send_page_view: forwarderSettings.enablePageView === 'True',
700
- };
701
- window.dataLayer = window.dataLayer || [];
702
-
703
- window.gtag = function () {
704
- window.dataLayer.push(arguments);
705
- };
706
-
707
- if (!testMode) {
708
- var clientScript = document.createElement('script');
709
- clientScript.type = 'text/javascript';
710
- clientScript.async = true;
711
- clientScript.src =
712
- 'https://www.googletagmanager.com/gtag/js?id=' + measurementId;
713
- (
714
- document.getElementsByTagName('head')[0] ||
715
- document.getElementsByTagName('body')[0]
716
- ).appendChild(clientScript);
717
-
718
- clientScript.onload = function () {
719
- gtag('js', new Date());
720
-
721
- if (window.mParticle.getVersion().split('.')[0] === '2') {
722
- var userId = common.getUserId(
723
- mParticle.Identity.getCurrentUser(),
724
- userIdType,
725
- hashUserId
726
- );
727
- if (userId) {
728
- configSettings.user_id = userId;
729
- }
730
- }
731
- gtag('config', measurementId, configSettings);
732
- isInitialized = true;
733
-
734
- if (window.dataLayer && gtag && eventQueue.length > 0) {
735
- for (var i = 0; i < eventQueue.length; i++) {
736
- processEvent(eventQueue[i]);
737
- }
738
- eventQueue = [];
739
- }
740
- };
741
- } else {
742
- isInitialized = true;
743
- }
744
- return isInitialized;
745
- },
746
- };
747
-
748
- var initialization_1 = initialization;
749
-
750
- var sessionHandler = {
751
- onSessionStart: function(event) {
752
-
753
- },
754
- onSessionEnd: function(event) {
755
-
756
- }
757
- };
758
-
759
- var sessionHandler_1 = sessionHandler;
760
-
761
- function UserAttributeHandler(common) {
762
- this.common = common || {};
763
- }
764
-
765
- // `mParticleUser` was removed from the function signatures onRemoveUserAttribute, onSetUserAttribute, and onConsentStateUpload because they were not being used
766
- // In the future if mParticleUser is ever required for an implementation of any of the below APIs, see https://github.com/mparticle-integrations/mparticle-javascript-integration-example/blob/master/src/user-attribute-handler.js
767
- // for previous function signatures
768
-
769
- UserAttributeHandler.prototype.onRemoveUserAttribute = function(key) {
770
- var userAttributes = {};
771
- userAttributes[key] = null;
772
- gtag('set', 'user_properties', userAttributes);
773
- };
774
-
775
- UserAttributeHandler.prototype.onSetUserAttribute = function(key, value) {
776
- var userAttributes = {};
777
- userAttributes[key] = value;
778
- gtag('set', 'user_properties', userAttributes);
779
- };
780
-
781
- // TODO: Commenting this out for now because Integrations PM still determining if this is in scope or not
782
- // UserAttributeHandler.prototype.onConsentStateUpdated = function() // oldState,
783
- // newState,
784
- // mParticleUser
785
- // {};
786
-
787
- var userAttributeHandler = UserAttributeHandler;
788
-
789
- // =============== REACH OUT TO MPARTICLE IF YOU HAVE ANY QUESTIONS ===============
790
- //
791
- // Copyright 2018 mParticle, Inc.
792
- //
793
- // Licensed under the Apache License, Version 2.0 (the "License");
794
- // you may not use this file except in compliance with the License.
795
- // You may obtain a copy of the License at
796
- //
797
- // http://www.apache.org/licenses/LICENSE-2.0
798
- //
799
- // Unless required by applicable law or agreed to in writing, software
800
- // distributed under the License is distributed on an "AS IS" BASIS,
801
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
802
- // See the License for the specific language governing permissions and
803
- // limitations under the License.
804
-
805
-
806
-
807
-
808
-
809
-
810
-
811
-
812
-
813
- var name = initialization_1.name,
814
- moduleId = initialization_1.moduleId,
815
- MessageType = {
816
- SessionStart: 1,
817
- SessionEnd: 2,
818
- PageView: 3,
819
- PageEvent: 4,
820
- CrashReport: 5,
821
- OptOut: 6,
822
- Commerce: 16,
823
- Media: 20,
824
- };
825
-
826
- var constructor = function() {
827
- var self = this,
828
- isInitialized = false,
829
- forwarderSettings,
830
- reportingService,
831
- eventQueue = [];
832
-
833
- self.name = initialization_1.name;
834
- self.moduleId = initialization_1.moduleId;
835
- self.common = new common();
836
-
837
- function initForwarder(
838
- settings,
839
- service,
840
- testMode,
841
- trackerId,
842
- userAttributes,
843
- userIdentities,
844
- appVersion,
845
- appName,
846
- customFlags,
847
- clientId
848
- ) {
849
- forwarderSettings = settings;
850
-
851
- if (
852
- typeof window !== 'undefined' &&
853
- window.mParticle.isTestEnvironment
854
- ) {
855
- reportingService = function() {};
856
- } else {
857
- reportingService = service;
858
- }
859
-
860
- try {
861
- initialization_1.initForwarder(
862
- settings,
863
- testMode,
864
- userAttributes,
865
- userIdentities,
866
- processEvent,
867
- eventQueue,
868
- isInitialized,
869
- self.common,
870
- appVersion,
871
- appName,
872
- customFlags,
873
- clientId
874
- );
875
- self.eventHandler = new eventHandler(self.common);
876
- self.identityHandler = new identityHandler(self.common);
877
- self.userAttributeHandler = new userAttributeHandler(self.common);
878
- self.commerceHandler = new commerceHandler(self.common);
879
-
880
- isInitialized = true;
881
- } catch (e) {
882
- console.log('Failed to initialize ' + name + ' - ' + e);
883
- }
884
- }
885
-
886
- function processEvent(event) {
887
- var reportEvent = false;
888
- if (isInitialized) {
889
- try {
890
- if (event.EventDataType === MessageType.SessionStart) {
891
- reportEvent = logSessionStart(event);
892
- } else if (event.EventDataType === MessageType.SessionEnd) {
893
- reportEvent = logSessionEnd(event);
894
- } else if (event.EventDataType === MessageType.CrashReport) {
895
- reportEvent = logError(event);
896
- } else if (event.EventDataType === MessageType.PageView) {
897
- reportEvent = logPageView(event);
898
- } else if (event.EventDataType === MessageType.Commerce) {
899
- reportEvent = logEcommerceEvent(event);
900
- } else if (event.EventDataType === MessageType.PageEvent) {
901
- reportEvent = logEvent(event);
902
- } else if (event.EventDataType === MessageType.Media) {
903
- // Kits should just treat Media Events as generic Events
904
- reportEvent = logEvent(event);
905
- }
906
- if (reportEvent === true && reportingService) {
907
- reportingService(self, event);
908
- return 'Successfully sent to ' + name;
909
- } else {
910
- return (
911
- 'Error logging event or event type not supported on forwarder ' +
912
- name
913
- );
914
- }
915
- } catch (e) {
916
- return 'Failed to send to ' + name + ' ' + e;
917
- }
918
- } else {
919
- eventQueue.push(event);
920
- return (
921
- "Can't send to forwarder " +
922
- name +
923
- ', not initialized. Event added to queue.'
924
- );
925
- }
926
- }
927
-
928
- function logSessionStart(event) {
929
- try {
930
- sessionHandler_1.onSessionStart(event);
931
- return true;
932
- } catch (e) {
933
- return {
934
- error: 'Error starting session on forwarder ' + name + '; ' + e,
935
- };
936
- }
937
- }
938
-
939
- function logSessionEnd(event) {
940
- try {
941
- sessionHandler_1.onSessionEnd(event);
942
- return true;
943
- } catch (e) {
944
- return {
945
- error: 'Error ending session on forwarder ' + name + '; ' + e,
946
- };
947
- }
948
- }
949
-
950
- function logError(event) {
951
- try {
952
- self.eventHandler.logError(event);
953
- return true;
954
- } catch (e) {
955
- return {
956
- error: 'Error logging error on forwarder ' + name + '; ' + e,
957
- };
958
- }
959
- }
960
-
961
- function logPageView(event) {
962
- try {
963
- self.eventHandler.logPageView(event);
964
- return true;
965
- } catch (e) {
966
- return {
967
- error:
968
- 'Error logging page view on forwarder ' + name + '; ' + e,
969
- };
970
- }
971
- }
972
-
973
- function logEvent(event) {
974
- try {
975
- self.eventHandler.logEvent(event);
976
- return true;
977
- } catch (e) {
978
- return {
979
- error: 'Error logging event on forwarder ' + name + '; ' + e,
980
- };
981
- }
982
- }
983
-
984
- function logEcommerceEvent(event) {
985
- try {
986
- self.commerceHandler.logCommerceEvent(event);
987
- return true;
988
- } catch (e) {
989
- return {
990
- error:
991
- 'Error logging purchase event on forwarder ' +
992
- name +
993
- '; ' +
994
- e,
995
- };
996
- }
997
- }
998
-
999
- function setUserAttribute(key, value) {
1000
- if (isInitialized) {
1001
- try {
1002
- self.userAttributeHandler.onSetUserAttribute(
1003
- key,
1004
- value,
1005
- forwarderSettings
1006
- );
1007
- return 'Successfully set user attribute on forwarder ' + name;
1008
- } catch (e) {
1009
- return (
1010
- 'Error setting user attribute on forwarder ' +
1011
- name +
1012
- '; ' +
1013
- e
1014
- );
1015
- }
1016
- } else {
1017
- return (
1018
- "Can't set user attribute on forwarder " +
1019
- name +
1020
- ', not initialized'
1021
- );
1022
- }
1023
- }
1024
-
1025
- function removeUserAttribute(key) {
1026
- if (isInitialized) {
1027
- try {
1028
- self.userAttributeHandler.onRemoveUserAttribute(
1029
- key,
1030
- forwarderSettings
1031
- );
1032
- return (
1033
- 'Successfully removed user attribute on forwarder ' + name
1034
- );
1035
- } catch (e) {
1036
- return (
1037
- 'Error removing user attribute on forwarder ' +
1038
- name +
1039
- '; ' +
1040
- e
1041
- );
1042
- }
1043
- } else {
1044
- return (
1045
- "Can't remove user attribute on forwarder " +
1046
- name +
1047
- ', not initialized'
1048
- );
1049
- }
1050
- }
1051
-
1052
- function setUserIdentity(id, type) {
1053
- if (isInitialized) {
1054
- try {
1055
- self.identityHandler.onSetUserIdentity(
1056
- forwarderSettings,
1057
- id,
1058
- type
1059
- );
1060
- return 'Successfully set user Identity on forwarder ' + name;
1061
- } catch (e) {
1062
- return (
1063
- 'Error removing user attribute on forwarder ' +
1064
- name +
1065
- '; ' +
1066
- e
1067
- );
1068
- }
1069
- } else {
1070
- return (
1071
- "Can't call setUserIdentity on forwarder " +
1072
- name +
1073
- ', not initialized'
1074
- );
1075
- }
1076
- }
1077
-
1078
- function onUserIdentified(user) {
1079
- if (isInitialized) {
1080
- try {
1081
- self.identityHandler.onUserIdentified(user);
1082
-
1083
- return (
1084
- 'Successfully called onUserIdentified on forwarder ' + name
1085
- );
1086
- } catch (e) {
1087
- return {
1088
- error:
1089
- 'Error calling onUserIdentified on forwarder ' +
1090
- name +
1091
- '; ' +
1092
- e,
1093
- };
1094
- }
1095
- } else {
1096
- return (
1097
- "Can't set new user identities on forwader " +
1098
- name +
1099
- ', not initialized'
1100
- );
1101
- }
1102
- }
1103
-
1104
- function onIdentifyComplete(user, filteredIdentityRequest) {
1105
- if (isInitialized) {
1106
- try {
1107
- self.identityHandler.onIdentifyComplete(
1108
- user,
1109
- filteredIdentityRequest
1110
- );
1111
-
1112
- return (
1113
- 'Successfully called onIdentifyComplete on forwarder ' +
1114
- name
1115
- );
1116
- } catch (e) {
1117
- return {
1118
- error:
1119
- 'Error calling onIdentifyComplete on forwarder ' +
1120
- name +
1121
- '; ' +
1122
- e,
1123
- };
1124
- }
1125
- } else {
1126
- return (
1127
- "Can't call onIdentifyCompleted on forwader " +
1128
- name +
1129
- ', not initialized'
1130
- );
1131
- }
1132
- }
1133
-
1134
- function onLoginComplete(user, filteredIdentityRequest) {
1135
- if (isInitialized) {
1136
- try {
1137
- self.identityHandler.onLoginComplete(
1138
- user,
1139
- filteredIdentityRequest
1140
- );
1141
-
1142
- return (
1143
- 'Successfully called onLoginComplete on forwarder ' + name
1144
- );
1145
- } catch (e) {
1146
- return {
1147
- error:
1148
- 'Error calling onLoginComplete on forwarder ' +
1149
- name +
1150
- '; ' +
1151
- e,
1152
- };
1153
- }
1154
- } else {
1155
- return (
1156
- "Can't call onLoginComplete on forwader " +
1157
- name +
1158
- ', not initialized'
1159
- );
1160
- }
1161
- }
1162
-
1163
- function onLogoutComplete(user, filteredIdentityRequest) {
1164
- if (isInitialized) {
1165
- try {
1166
- self.identityHandler.onLogoutComplete(
1167
- user,
1168
- filteredIdentityRequest
1169
- );
1170
-
1171
- return (
1172
- 'Successfully called onLogoutComplete on forwarder ' + name
1173
- );
1174
- } catch (e) {
1175
- return {
1176
- error:
1177
- 'Error calling onLogoutComplete on forwarder ' +
1178
- name +
1179
- '; ' +
1180
- e,
1181
- };
1182
- }
1183
- } else {
1184
- return (
1185
- "Can't call onLogoutComplete on forwader " +
1186
- name +
1187
- ', not initialized'
1188
- );
1189
- }
1190
- }
1191
-
1192
- function onModifyComplete(user, filteredIdentityRequest) {
1193
- if (isInitialized) {
1194
- try {
1195
- self.identityHandler.onModifyComplete(
1196
- user,
1197
- filteredIdentityRequest
1198
- );
1199
-
1200
- return (
1201
- 'Successfully called onModifyComplete on forwarder ' + name
1202
- );
1203
- } catch (e) {
1204
- return {
1205
- error:
1206
- 'Error calling onModifyComplete on forwarder ' +
1207
- name +
1208
- '; ' +
1209
- e,
1210
- };
1211
- }
1212
- } else {
1213
- return (
1214
- "Can't call onModifyComplete on forwader " +
1215
- name +
1216
- ', not initialized'
1217
- );
1218
- }
1219
- }
1220
-
1221
- function setOptOut(isOptingOutBoolean) {
1222
- if (isInitialized) {
1223
- try {
1224
- self.initialization.setOptOut(isOptingOutBoolean);
1225
-
1226
- return 'Successfully called setOptOut on forwarder ' + name;
1227
- } catch (e) {
1228
- return {
1229
- error:
1230
- 'Error calling setOptOut on forwarder ' +
1231
- name +
1232
- '; ' +
1233
- e,
1234
- };
1235
- }
1236
- } else {
1237
- return (
1238
- "Can't call setOptOut on forwader " +
1239
- name +
1240
- ', not initialized'
1241
- );
1242
- }
1243
- }
1244
-
1245
- this.init = initForwarder;
1246
- this.process = processEvent;
1247
- this.setUserAttribute = setUserAttribute;
1248
- this.removeUserAttribute = removeUserAttribute;
1249
- this.onUserIdentified = onUserIdentified;
1250
- this.setUserIdentity = setUserIdentity;
1251
- this.onIdentifyComplete = onIdentifyComplete;
1252
- this.onLoginComplete = onLoginComplete;
1253
- this.onLogoutComplete = onLogoutComplete;
1254
- this.onModifyComplete = onModifyComplete;
1255
- this.setOptOut = setOptOut;
1256
- };
1257
-
1258
- function getId() {
1259
- return moduleId;
1260
- }
1261
-
1262
- function isObject(val) {
1263
- return (
1264
- val != null && typeof val === 'object' && Array.isArray(val) === false
1265
- );
1266
- }
1267
-
1268
- function register(config) {
1269
- if (!config) {
1270
- console.log(
1271
- 'You must pass a config object to register the kit ' + name
1272
- );
1273
- return;
1274
- }
1275
-
1276
- if (!isObject(config)) {
1277
- console.log(
1278
- "'config' must be an object. You passed in a " + typeof config
1279
- );
1280
- return;
1281
- }
1282
-
1283
- if (isObject(config.kits)) {
1284
- config.kits[name] = {
1285
- constructor: constructor,
1286
- };
1287
- } else {
1288
- config.kits = {};
1289
- config.kits[name] = {
1290
- constructor: constructor,
1291
- };
1292
- }
1293
- console.log(
1294
- 'Successfully registered ' + name + ' to your mParticle configuration'
1295
- );
1296
- }
1297
-
1298
- if (typeof window !== 'undefined') {
1299
- if (window && window.mParticle && window.mParticle.addForwarder) {
1300
- window.mParticle.addForwarder({
1301
- name: name,
1302
- constructor: constructor,
1303
- getId: getId,
1304
- });
1305
- }
1306
- }
1307
-
1308
- var webKitWrapper = {
1309
- register: register,
1310
- };
1311
- var webKitWrapper_1 = webKitWrapper.register;
1312
-
1313
- exports.default = webKitWrapper;
1314
- exports.register = webKitWrapper_1;
1315
-
1316
- return exports;
1317
-
1318
- }({}));