Skip to content

Commit

Permalink
fix: Forward SKU as product name when ecommerce events are bundled
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 committed Jan 23, 2024
1 parent 88c8f32 commit 43d8106
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/BrazeKit-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
53 changes: 52 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 43d8106

Please sign in to comment.