From 5db53a5b0a17e779eeefea4d2f01688e61406270 Mon Sep 17 00:00:00 2001 From: Robert Ing Date: Tue, 23 Jan 2024 14:01:27 -0500 Subject: [PATCH] fix: Forward SKU as product name when ecommerce events are bundled (#47) --- src/BrazeKit-dev.js | 11 ++++++++-- test/tests.js | 53 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/BrazeKit-dev.js b/src/BrazeKit-dev.js index 980b63b..48a4754 100644 --- a/src/BrazeKit-dev.js +++ b/src/BrazeKit-dev.js @@ -70,6 +70,7 @@ var constructor = function () { }; var bundleCommerceEventData = false; + var forwardSkuAsProductName = false; // A purchase event can either log a single event with all products // or multiple purchase events (one per product) @@ -135,7 +136,7 @@ var constructor = function () { event.ProductAction.ProductList.forEach(function(product) { var productName; - if (forwarderSettings.forwardSkuAsProductName === 'True') { + if (forwardSkuAsProductName) { productName = product.Sku; } else { productName = product.Name; @@ -510,12 +511,16 @@ var constructor = function () { function parseProduct(_product) { var product = {}; - for (var key in _product) { switch (key) { case 'Sku': product.Id = _product[key]; break; + case 'Name': + product.Name = forwardSkuAsProductName + ? _product.Sku + : _product.Name; + break; case 'CouponCode': product['Coupon Code'] = _product[key]; break; @@ -724,6 +729,8 @@ var constructor = function () { forwarderSettings = settings; bundleCommerceEventData = forwarderSettings.bundleCommerceEventData === 'True'; + forwardSkuAsProductName = + forwarderSettings.forwardSkuAsProductName === 'True'; reportingService = service; // 30 min is Braze default options.sessionTimeoutInSeconds = diff --git a/test/tests.js b/test/tests.js index 45a8196..fcb69db 100644 --- a/test/tests.js +++ b/test/tests.js @@ -745,6 +745,57 @@ describe('Braze Forwarder', function() { window.braze.should.have.property('logPurchaseName', '12345'); }); + it('should log a purchase event with SKU in place of product name if forwardSkuAsProductName and bundleCommerceEventData are true', function() { + mParticle.forwarder.init( + { + apiKey: '123456', + forwardSkuAsProductName: 'True', + bundleCommerceEventData: 'True', + }, + reportService.cb, + true, + null + ); + + mParticle.forwarder.process({ + EventName: 'Test Purchase Event', + EventAttributes: {}, + EventDataType: MessageType.Commerce, + EventCategory: CommerceEventType.ProductPurchase, + CurrencyCode: 'USD', + ProductAction: { + TransactionId: 1234, + TotalAmount: 50, + ProductList: [ + { + Price: '50', + Name: '$Product $Name', + TotalAmount: 50, + Quantity: 1, + Attributes: { $$$attri$bute: '$$$$what$ever' }, + Sku: '12345', + }, + { + Price: '50', + Name: 'Another $Product $Name', + TotalAmount: 50, + Quantity: 1, + Attributes: { $$$attri$bute: '$$$$what$ever2' }, + Sku: '2345', + }, + ], + }, + }); + + window.braze.should.have.property('logPurchaseEventCalled', true); + braze.purchaseEventProperties[0][3].products[0].Name.should.equal( + '12345' + ); + braze.purchaseEventProperties[0][3].products[1].Name.should.equal( + '2345' + ); + }); + it('should log a page view with the page name if sendEventNameForPageView is true', function() { mParticle.forwarder.init( { @@ -1002,7 +1053,7 @@ describe('Braze Forwarder', function() { // Braze's API expects a year from us, this test will break every year, // since setting the age = 10 in 2021 will mean the user is born in 2011, // but setting it in 2023 means the year is 2013. - window.braze.getUser().yearOfBirth.should.equal(2013); + window.braze.getUser().yearOfBirth.should.equal(2014); window.braze.getUser().dayOfBirth.should.equal(1); window.braze.getUser().monthOfBirth.should.equal(1); window.braze.getUser().phoneSet.should.equal('1234567890');