@mparticle/web-braze-kit 4.0.2 → 4.1.1

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/README.md CHANGED
@@ -19,7 +19,7 @@ Note that the following is only one example. Everywhere you manually call `appb
19
19
  window.appboy.display.destroyFeed();
20
20
  ```
21
21
 
22
- Step 2: Roll out code changes to be used before February 15, 2023:
22
+ Step 2: Roll out code changes prior to opting in to V4
23
23
  ```javascript
24
24
  if (window.appboy) {
25
25
  window.appboy.display.destroyFeed();
@@ -27,12 +27,14 @@ if (window.appboy) {
27
27
  window.braze.destroyFeed();
28
28
  }
29
29
  ```
30
- Step 3: After February 1, 2023, you can simplify your code after the mParticle Braze Web kit, which includes Braze SDK V4, has been released to our CDN:
30
+ Step 3: Opt into Version 4. Simply navigate to your connection settings and selection `Version 4` from the new Braze Web SDK Version drop down.
31
+
32
+ Step 4: After you opt in, you can simplify your code. We recommend testing and waiting at least 24 hours between opting in and removing previous instances of `appboy` and doing thorough testing of your application in a development environment to ensure everything is working:
31
33
  ```javascript
32
34
  window.braze.destroyFeed();
33
35
  ```
34
36
 
35
- Step 4: Push Notifications via service-worker.js
37
+ Step 5: Push Notifications via service-worker.js
36
38
  If you use Push Notifications, we have updated the `service-worker.js` file. In our testing, Braze’s push notifications work as expected regardless of what version of the service-worker is used, but we recommend updating this file to ensure future compatibility. In your `service-worker.js` file, update the code to reference `https://static.mparticle.com/sdk/js/braze/service-worker-4.2.0.js` instead of `https://static.mparticle.com/sdk/js/braze/service-worker-3.5.0.js`. Your `service-worker.js` file should now contain:
37
39
 
38
40
  ```javascript
@@ -9172,12 +9172,13 @@ window.braze = require$$0;
9172
9172
  var name = 'Appboy',
9173
9173
  suffix = 'v4',
9174
9174
  moduleId = 28,
9175
- version = '4.0.2',
9175
+ version = '4.1.1',
9176
9176
  MessageType = {
9177
9177
  PageView: 3,
9178
9178
  PageEvent: 4,
9179
9179
  Commerce: 16,
9180
- };
9180
+ },
9181
+ CommerceEventType = mParticle.CommerceEventType;
9181
9182
 
9182
9183
  var clusterMapping = {
9183
9184
  '01': 'sdk.iad-01.braze.com',
@@ -9222,32 +9223,94 @@ var constructor = function () {
9222
9223
  dob: 'setDateOfBirth',
9223
9224
  };
9224
9225
 
9226
+ var bundleCommerceEventData = false;
9227
+
9228
+ // A purchase event can either log a single event with all products
9229
+ // or multiple purchase events (one per product)
9225
9230
  function logPurchaseEvent(event) {
9226
9231
  var reportEvent = false;
9232
+
9233
+ if (bundleCommerceEventData) {
9234
+ reportEvent = logSinglePurchaseEventWithProducts(event);
9235
+ } else {
9236
+ reportEvent = logPurchaseEventPerProduct(event);
9237
+ }
9238
+ return reportEvent === true;
9239
+ }
9240
+
9241
+ function logSinglePurchaseEventWithProducts(event) {
9242
+ var quantity = 1;
9243
+ var eventAttributes = mergeObjects(event.EventAttributes, {
9244
+ products: [],
9245
+ });
9246
+ var eventName = getCommerceEventName(event.EventCategory);
9247
+
9248
+ // All commerce events except for promotion/impression events will have a
9249
+ // ProductAction property, but if this ever changes in the future, this
9250
+ // check will prevent errors
9251
+ if (!event.ProductAction) {
9252
+ return false;
9253
+ }
9254
+
9255
+ if (event.ProductAction.TransactionId) {
9256
+ eventAttributes['Transaction Id'] =
9257
+ event.ProductAction.TransactionId;
9258
+ }
9259
+
9260
+ if (
9261
+ event.ProductAction.ProductList &&
9262
+ event.ProductAction.ProductList.length
9263
+ ) {
9264
+ eventAttributes.products = addProducts(
9265
+ event.ProductAction.ProductList
9266
+ );
9267
+ }
9268
+
9269
+ kitLogger(
9270
+ 'braze.logPurchase',
9271
+ eventName,
9272
+ event.ProductAction.TotalAmount,
9273
+ event.CurrencyCode,
9274
+ quantity,
9275
+ eventAttributes
9276
+ );
9277
+
9278
+ reportEvent = braze.logPurchase(
9279
+ eventName,
9280
+ event.ProductAction.TotalAmount,
9281
+ event.CurrencyCode,
9282
+ quantity,
9283
+ eventAttributes
9284
+ );
9285
+ }
9286
+
9287
+ function logPurchaseEventPerProduct(event) {
9227
9288
  if (event.ProductAction.ProductList) {
9228
- event.ProductAction.ProductList.forEach(function (product) {
9229
- if (product.Attributes == null) {
9230
- product.Attributes = {};
9231
- }
9232
- product.Attributes['Sku'] = product.Sku;
9289
+ event.ProductAction.ProductList.forEach(function(product) {
9290
+ var productName;
9233
9291
 
9234
- var sanitizedProductName;
9235
9292
  if (forwarderSettings.forwardSkuAsProductName === 'True') {
9236
- sanitizedProductName = getSanitizedValueForBraze(
9237
- String(product.Sku)
9238
- );
9293
+ productName = product.Sku;
9239
9294
  } else {
9240
- sanitizedProductName = getSanitizedValueForBraze(
9241
- String(product.Name)
9242
- );
9295
+ productName = product.Name;
9296
+ }
9297
+ var sanitizedProductName = getSanitizedValueForBraze(
9298
+ productName
9299
+ );
9300
+
9301
+ if (product.Attributes == null) {
9302
+ product.Attributes = {};
9243
9303
  }
9244
9304
 
9305
+ product.Attributes['Sku'] = product.Sku;
9306
+
9245
9307
  var productAttributes = mergeObjects(product.Attributes, {
9246
9308
  'Transaction Id': event.ProductAction.TransactionId,
9247
9309
  });
9248
9310
 
9249
- var sanitizedProperties =
9250
- getSanitizedCustomProperties(productAttributes);
9311
+ var sanitizedProperties = getSanitizedCustomProperties(
9312
+ productAttributes
9313
+ );
9251
9314
 
9252
9315
  if (sanitizedProperties == null) {
9253
9316
  return (
@@ -9278,6 +9341,57 @@ var constructor = function () {
9278
9341
  return reportEvent === true;
9279
9342
  }
9280
9343
 
9344
+ function getCommerceEventName(eventType) {
9345
+ const eventNamePrefix = 'eCommerce';
9346
+ let eventName;
9347
+
9348
+ switch (eventType) {
9349
+ case CommerceEventType.ProductAddToCart:
9350
+ eventName = 'add_to_cart';
9351
+ break;
9352
+ case CommerceEventType.ProductRemoveFromCart:
9353
+ eventName = 'remove_from_cart';
9354
+ break;
9355
+ case CommerceEventType.ProductCheckout:
9356
+ eventName = 'checkout';
9357
+ break;
9358
+ case CommerceEventType.ProductCheckoutOption:
9359
+ eventName = 'checkout_option';
9360
+ break;
9361
+ case CommerceEventType.ProductClick:
9362
+ eventName = 'click';
9363
+ break;
9364
+ case CommerceEventType.ProductViewDetail:
9365
+ eventName = 'view_detail';
9366
+ break;
9367
+ case CommerceEventType.ProductPurchase:
9368
+ eventName = 'purchase';
9369
+ break;
9370
+ case CommerceEventType.ProductRefund:
9371
+ eventName = 'refund';
9372
+ break;
9373
+ case CommerceEventType.ProductAddToWishlist:
9374
+ eventName = 'add_to_wishlist';
9375
+ break;
9376
+ case CommerceEventType.ProductRemoveFromWishlist:
9377
+ eventName = 'remove_from_wishlist';
9378
+ break;
9379
+ case CommerceEventType.PromotionView:
9380
+ eventName = 'view';
9381
+ break;
9382
+ case CommerceEventType.PromotionClick:
9383
+ eventName = 'click';
9384
+ break;
9385
+ case CommerceEventType.ProductImpression:
9386
+ eventName = 'Impression';
9387
+ break;
9388
+ default:
9389
+ eventName = 'unknown';
9390
+ break;
9391
+ }
9392
+ return [eventNamePrefix, eventName].join(' - ');
9393
+ }
9394
+
9281
9395
  function logBrazePageViewEvent(event) {
9282
9396
  var sanitizedEventName,
9283
9397
  sanitizedAttrs,
@@ -9402,29 +9516,8 @@ var constructor = function () {
9402
9516
  function processEvent(event) {
9403
9517
  var reportEvent = false;
9404
9518
 
9405
- if (
9406
- event.EventDataType == MessageType.Commerce &&
9407
- event.EventCategory == mParticle.CommerceEventType.ProductPurchase
9408
- ) {
9409
- reportEvent = logPurchaseEvent(event);
9410
- } else if (event.EventDataType == MessageType.Commerce) {
9411
- var listOfPageEvents =
9412
- mParticle.eCommerce.expandCommerceEvent(event);
9413
- if (listOfPageEvents != null) {
9414
- for (var i = 0; i < listOfPageEvents.length; i++) {
9415
- // finalLoopResult keeps track of if any logBrazeEvent in this loop returns true or not
9416
- var finalLoopResult = false;
9417
- try {
9418
- reportEvent = logBrazeEvent(listOfPageEvents[i]);
9419
- if (reportEvent === true) {
9420
- finalLoopResult = true;
9421
- }
9422
- } catch (err) {
9423
- return 'Error logging page event' + err.message;
9424
- }
9425
- }
9426
- reportEvent = finalLoopResult === true;
9427
- }
9519
+ if (event.EventDataType == MessageType.Commerce) {
9520
+ reportEvent = logCommerceEvent(event);
9428
9521
  } else if (event.EventDataType == MessageType.PageEvent) {
9429
9522
  reportEvent = logBrazeEvent(event);
9430
9523
  } else if (event.EventDataType == MessageType.PageView) {
@@ -9444,6 +9537,173 @@ var constructor = function () {
9444
9537
  }
9445
9538
  }
9446
9539
 
9540
+ // mParticle commerce events use different Braze methods depending on if they are
9541
+ // a purchase event or a non-purchase commerce event
9542
+ function logCommerceEvent(event) {
9543
+ if (event.EventCategory === CommerceEventType.ProductPurchase) {
9544
+ reportEvent = logPurchaseEvent(event);
9545
+ return reportEvent === true;
9546
+ } else {
9547
+ reportEvent = logNonPurchaseCommerceEvent(event);
9548
+ return reportEvent === true;
9549
+ }
9550
+ }
9551
+
9552
+ // A non-purchase commerce event can either log a single event with all products
9553
+ // or one event per product when the commerce event is expanded
9554
+ function logNonPurchaseCommerceEvent(event) {
9555
+ if (bundleCommerceEventData) {
9556
+ return logNonPurchaseCommerceEventWithProducts(event);
9557
+ } else {
9558
+ return logExpandedNonPurchaseCommerceEvents(event);
9559
+ }
9560
+ }
9561
+
9562
+ function logNonPurchaseCommerceEventWithProducts(mpEvent) {
9563
+ const commerceEventAttrs = {};
9564
+ const eventName = getCommerceEventName(mpEvent.EventCategory);
9565
+
9566
+ try {
9567
+ switch (mpEvent.EventCategory) {
9568
+ case CommerceEventType.PromotionClick:
9569
+ case CommerceEventType.PromotionView:
9570
+ commerceEventAttrs.promotions = addPromotions(
9571
+ mpEvent.PromotionAction
9572
+ );
9573
+ break;
9574
+ case CommerceEventType.ProductImpression:
9575
+ commerceEventAttrs.impressions = addImpressions(
9576
+ mpEvent.ProductImpressions
9577
+ );
9578
+ break;
9579
+ default:
9580
+ if (mpEvent.ProductAction.ProductList) {
9581
+ commerceEventAttrs.products = addProducts(
9582
+ mpEvent.ProductAction.ProductList
9583
+ );
9584
+ }
9585
+ var transactionId = mpEvent.ProductAction.TransactionId;
9586
+ var totalAmount = mpEvent.ProductAction.TotalAmount;
9587
+ var taxAmount = mpEvent.ProductAction.TaxAmount;
9588
+ var shippingAmount = mpEvent.ProductAction.ShippingAmount;
9589
+ var affiliation = mpEvent.ProductAction.Affiliation;
9590
+
9591
+ if (transactionId) {
9592
+ commerceEventAttrs['Transaction Id'] = transactionId;
9593
+ }
9594
+ if (totalAmount) {
9595
+ commerceEventAttrs['Total Amount'] = totalAmount;
9596
+ }
9597
+ if (taxAmount) {
9598
+ commerceEventAttrs['Tax Amount'] = taxAmount;
9599
+ }
9600
+ if (shippingAmount) {
9601
+ commerceEventAttrs['Shipping Amount'] = shippingAmount;
9602
+ }
9603
+ if (affiliation) {
9604
+ commerceEventAttrs['Affiliation'] = affiliation;
9605
+ }
9606
+ }
9607
+
9608
+ var sanitizedProperties = getSanitizedCustomProperties(
9609
+ mpEvent.EventAttributes
9610
+ );
9611
+
9612
+ const brazeEvent = {
9613
+ EventName: eventName,
9614
+ EventAttributes: mergeObjects(
9615
+ commerceEventAttrs,
9616
+ sanitizedProperties
9617
+ ),
9618
+ };
9619
+
9620
+ reportEvent = logBrazeEvent(brazeEvent);
9621
+ return reportEvent;
9622
+ } catch (err) {
9623
+ return 'Error logging commerce event' + err.message;
9624
+ }
9625
+ }
9626
+
9627
+ function addPromotions(promotionAction) {
9628
+ if (promotionAction && promotionAction.PromotionList) {
9629
+ return promotionAction.PromotionList;
9630
+ }
9631
+ return [];
9632
+ }
9633
+
9634
+ function addImpressions(productImpressions) {
9635
+ if (productImpressions.length) {
9636
+ return productImpressions.map(function(impression) {
9637
+ return {
9638
+ 'Product Impression List': impression.ProductImpressionList,
9639
+ products: addProducts(impression.ProductList),
9640
+ };
9641
+ });
9642
+ } else {
9643
+ return [];
9644
+ }
9645
+ }
9646
+
9647
+ function addProducts(productList) {
9648
+ const productArray = [];
9649
+ if (!productList || productList.length === 0) {
9650
+ return productArray;
9651
+ }
9652
+
9653
+ productList.forEach(function(product) {
9654
+ {
9655
+ var sanitizedProduct = parseProduct(
9656
+ getSanitizedCustomProperties(product)
9657
+ );
9658
+ productArray.push(sanitizedProduct);
9659
+ }
9660
+ });
9661
+
9662
+ return productArray;
9663
+ }
9664
+
9665
+ function parseProduct(_product) {
9666
+ var product = {};
9667
+
9668
+ for (var key in _product) {
9669
+ switch (key) {
9670
+ case 'Sku':
9671
+ product.Id = _product[key];
9672
+ break;
9673
+ case 'CouponCode':
9674
+ product['Coupon Code'] = _product[key];
9675
+ break;
9676
+ case 'TotalAmount':
9677
+ product['Total Product Amount'] = _product[key];
9678
+ break;
9679
+ default:
9680
+ product[key] = _product[key];
9681
+ }
9682
+ }
9683
+
9684
+ return product;
9685
+ }
9686
+
9687
+ function logExpandedNonPurchaseCommerceEvents(event) {
9688
+ var listOfPageEvents = mParticle.eCommerce.expandCommerceEvent(event);
9689
+ if (listOfPageEvents !== null) {
9690
+ for (var i = 0; i < listOfPageEvents.length; i++) {
9691
+ // finalLoopResult keeps track of if any logBrazeEvent in this loop returns true or not
9692
+ var finalLoopResult = false;
9693
+ try {
9694
+ reportEvent = logBrazeEvent(listOfPageEvents[i]);
9695
+ if (reportEvent === true) {
9696
+ finalLoopResult = true;
9697
+ }
9698
+ } catch (err) {
9699
+ return 'Error logging page event' + err.message;
9700
+ }
9701
+ }
9702
+ reportEvent = finalLoopResult === true;
9703
+ }
9704
+ return reportEvent;
9705
+ }
9706
+
9447
9707
  function removeUserAttribute(key) {
9448
9708
  if (!(key in DefaultAttributeMethods)) {
9449
9709
  var sanitizedKey = getSanitizedValueForBraze(key);
@@ -9600,13 +9860,15 @@ var constructor = function () {
9600
9860
  if (!self.logger) {
9601
9861
  // create a logger
9602
9862
  self.logger = {
9603
- verbose: function () {},
9863
+ verbose: function() {},
9604
9864
  };
9605
9865
  }
9606
9866
  // eslint-disable-line no-unused-vars
9607
9867
  mpCustomFlags = customFlags;
9608
9868
  try {
9609
9869
  forwarderSettings = settings;
9870
+ bundleCommerceEventData =
9871
+ forwarderSettings.bundleCommerceEventData === 'True';
9610
9872
  reportingService = service;
9611
9873
  // 30 min is Braze default
9612
9874
  options.sessionTimeoutInSeconds =
@@ -9773,7 +10035,7 @@ var constructor = function () {
9773
10035
  var nonMethodArguments = Array.prototype.slice.call(arguments, 1);
9774
10036
  msg += '\n' + method + ':\n';
9775
10037
 
9776
- nonMethodArguments.forEach(function (arg) {
10038
+ nonMethodArguments.forEach(function(arg) {
9777
10039
  if (isObject(arg) || Array.isArray(arg)) {
9778
10040
  msg += JSON.stringify(arg);
9779
10041
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-braze-kit",
3
- "version": "4.0.2",
3
+ "version": "4.1.1",
4
4
  "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
5
  "description": "mParticle integration sdk for Braze",
6
6
  "main": "dist/BrazeKit.common.js",