From 2868d3c4c270f6b81b287290d37af70c269a7e27 Mon Sep 17 00:00:00 2001 From: Utsab Chowdhury Date: Fri, 1 Dec 2023 12:27:08 +0530 Subject: [PATCH] fix: add config optio to support custom props for purchase event --- src/v0/destinations/braze/braze.util.test.js | 106 +++++++++++++++---- src/v0/destinations/braze/transform.js | 2 +- src/v0/destinations/braze/util.js | 4 +- test/__tests__/data/braze_input.json | 96 +++++++++++++++++ test/__tests__/data/braze_output.json | 97 ++++++++++++----- 5 files changed, 258 insertions(+), 47 deletions(-) diff --git a/src/v0/destinations/braze/braze.util.test.js b/src/v0/destinations/braze/braze.util.test.js index 30eeef78df..9859e16152 100644 --- a/src/v0/destinations/braze/braze.util.test.js +++ b/src/v0/destinations/braze/braze.util.test.js @@ -1221,25 +1221,30 @@ describe('getPurchaseObjs', () => { }); test('products having extra properties', () => { - const output = getPurchaseObjs({ - properties: { - products: [ - { product_id: '123', price: 10.99, quantity: 2, random_extra_property_a: 'abc' }, - { product_id: '456', price: 5.49, quantity: 1, random_extra_property_b: 'efg' }, - { - product_id: '789', - price: 15.49, - quantity: 1, - random_extra_property_a: 'abc', - random_extra_property_b: 'efg', - random_extra_property_c: 'hij', - }, - ], - currency: 'USD', + const output = getPurchaseObjs( + { + properties: { + products: [ + { product_id: '123', price: 10.99, quantity: 2, random_extra_property_a: 'abc' }, + { product_id: '456', price: 5.49, quantity: 1, random_extra_property_b: 'efg' }, + { + product_id: '789', + price: 15.49, + quantity: 1, + random_extra_property_a: 'abc', + random_extra_property_b: 'efg', + random_extra_property_c: 'hij', + }, + ], + currency: 'USD', + }, + timestamp: '2023-08-04T12:34:56Z', + anonymousId: 'abc', }, - timestamp: '2023-08-04T12:34:56Z', - anonymousId: 'abc', - }); + { + sendPurchaseEventWithExtraProperties: true, + }, + ); expect(output).toEqual([ { product_id: '123', @@ -1290,4 +1295,69 @@ describe('getPurchaseObjs', () => { }, ]); }); + + test('products having extra properties with sendPurchaseEventWithExtraProperties as false', () => { + const output = getPurchaseObjs( + { + properties: { + products: [ + { product_id: '123', price: 10.99, quantity: 2, random_extra_property_a: 'abc' }, + { product_id: '456', price: 5.49, quantity: 1, random_extra_property_b: 'efg' }, + { + product_id: '789', + price: 15.49, + quantity: 1, + random_extra_property_a: 'abc', + random_extra_property_b: 'efg', + random_extra_property_c: 'hij', + }, + ], + currency: 'USD', + }, + timestamp: '2023-08-04T12:34:56Z', + anonymousId: 'abc', + }, + { + sendPurchaseEventWithExtraProperties: false, + }, + ); + expect(output).toEqual([ + { + product_id: '123', + price: 10.99, + currency: 'USD', + quantity: 2, + time: '2023-08-04T12:34:56Z', + _update_existing_only: false, + user_alias: { + alias_name: 'abc', + alias_label: 'rudder_id', + }, + }, + { + product_id: '456', + price: 5.49, + currency: 'USD', + quantity: 1, + time: '2023-08-04T12:34:56Z', + _update_existing_only: false, + user_alias: { + alias_name: 'abc', + alias_label: 'rudder_id', + }, + }, + { + product_id: '789', + price: 15.49, + currency: 'USD', + quantity: 1, + time: '2023-08-04T12:34:56Z', + _update_existing_only: false, + user_alias: { + alias_name: 'abc', + alias_label: 'rudder_id', + }, + }, + ]); + }); }); diff --git a/src/v0/destinations/braze/transform.js b/src/v0/destinations/braze/transform.js index b939e1f414..38a5947ded 100644 --- a/src/v0/destinations/braze/transform.js +++ b/src/v0/destinations/braze/transform.js @@ -315,7 +315,7 @@ function processTrackEvent(messageType, message, destination, mappingJson, proce typeof eventName === 'string' && eventName.toLowerCase() === 'order completed' ) { - const purchaseObjs = getPurchaseObjs(message); + const purchaseObjs = getPurchaseObjs(message, destination.Config); // del used properties delete properties.products; diff --git a/src/v0/destinations/braze/util.js b/src/v0/destinations/braze/util.js index 8c20360bb1..946c105cf3 100644 --- a/src/v0/destinations/braze/util.js +++ b/src/v0/destinations/braze/util.js @@ -539,7 +539,7 @@ function addMandatoryPurchaseProperties(productId, price, currencyCode, quantity }; } -function getPurchaseObjs(message) { +function getPurchaseObjs(message, Config) { // ref:https://www.braze.com/docs/api/objects_filters/purchase_object/ const validateForPurchaseEvent = (message) => { const { properties } = message; @@ -635,7 +635,7 @@ function getPurchaseObjs(message) { timestamp, ); const extraProperties = _.omit(product, ['product_id', 'sku', 'price', 'quantity', 'currency']); - if (Object.keys(extraProperties).length > 0) { + if (Object.keys(extraProperties).length > 0 && Config.sendPurchaseEventWithExtraProperties) { purchaseObj = { ...purchaseObj, properties: extraProperties }; } purchaseObj = setExternalIdOrAliasObject(purchaseObj, message); diff --git a/test/__tests__/data/braze_input.json b/test/__tests__/data/braze_input.json index e799cf2e82..8c3294e42b 100644 --- a/test/__tests__/data/braze_input.json +++ b/test/__tests__/data/braze_input.json @@ -1814,5 +1814,101 @@ "type": "track", "userId": "mickeyMouse" } + }, + { + "destination": { + "Config": { + "restApiKey": "dummyApiKey", + "prefixProperties": true, + "useNativeSDK": false, + "sendPurchaseEventWithExtraProperties": true + }, + "DestinationDefinition": { + "DisplayName": "Braze", + "ID": "1WhbSZ6uA3H5ChVifHpfL2H6sie", + "Name": "BRAZE" + }, + "Enabled": true, + "ID": "1WhcOCGgj9asZu850HvugU2C3Aq", + "Name": "Braze", + "Transformations": [] + }, + "message": { + "anonymousId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca", + "channel": "web", + "context": { + "app": { + "build": "1.0.0", + "name": "RudderLabs JavaScript SDK", + "namespace": "com.rudderlabs.javascript", + "version": "1.0.5" + }, + "ip": "0.0.0.0", + "library": { + "name": "RudderLabs JavaScript SDK", + "version": "1.0.5" + }, + "locale": "en-GB", + "os": { + "name": "", + "version": "" + }, + "screen": { + "density": 2 + }, + "traits": { + "city": "Disney", + "country": "USA", + "email": "mickey@disney.com", + "firstname": "Mickey" + }, + "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" + }, + "event": "Order Completed", + "integrations": { + "All": true + }, + "messageId": "aa5f5e44-8756-40ad-ad1e-b0d3b9fa710a", + "originalTimestamp": "2020-01-24T06:29:02.367Z", + "properties": { + "affiliation": "Google Store", + "checkout_id": "fksdjfsdjfisjf9sdfjsd9f", + "coupon": "hasbros", + "currency": "USD", + "discount": 2.5, + "order_id": "50314b8e9bcf000000000000", + "products": [ + { + "category": "Games", + "image_url": "https:///www.example.com/product/path.jpg", + "name": "Monopoly: 3rd Edition", + "price": 0, + "product_id": "507f1f77bcf86cd799439023", + "quantity": 1, + "sku": "45790-32", + "url": "https://www.example.com/product/path" + }, + { + "category": "Games", + "name": "Uno Card Game", + "price": 0, + "product_id": "505bd76785ebb509fc183724", + "quantity": 2, + "sku": "46493-32" + } + ], + "revenue": 25, + "shipping": 3, + "subtotal": 22.5, + "tax": 2, + "total": 27.5 + }, + "receivedAt": "2020-01-24T11:59:02.403+05:30", + "request_ip": "[::1]:53712", + "sentAt": "2020-01-24T06:29:02.368Z", + "timestamp": "2020-01-24T11:59:02.402+05:30", + "type": "track", + "userId": "" + } } ] diff --git a/test/__tests__/data/braze_output.json b/test/__tests__/data/braze_output.json index 7c7959f78f..0575326237 100644 --- a/test/__tests__/data/braze_output.json +++ b/test/__tests__/data/braze_output.json @@ -396,12 +396,6 @@ "currency": "USD", "price": 0, "product_id": "507f1f77bcf86cd799439011", - "properties": { - "category": "Games", - "image_url": "https:///www.example.com/product/path.jpg", - "name": "Monopoly: 3rd Edition", - "url": "https://www.example.com/product/path" - }, "quantity": 1, "time": "2020-01-24T11:59:02.402+05:30", "user_alias": { @@ -414,10 +408,6 @@ "currency": "USD", "price": 0, "product_id": "505bd76785ebb509fc183733", - "properties": { - "category": "Games", - "name": "Uno Card Game" - }, "quantity": 2, "time": "2020-01-24T11:59:02.402+05:30", "user_alias": { @@ -467,12 +457,6 @@ "currency": "USD", "price": 0, "product_id": "507f1f77bcf86cd799439011", - "properties": { - "category": "Games", - "image_url": "https:///www.example.com/product/path.jpg", - "name": "Monopoly: 3rd Edition", - "url": "https://www.example.com/product/path" - }, "quantity": 1, "time": "2020-01-24T11:59:02.402+05:30", "user_alias": { @@ -485,10 +469,6 @@ "currency": "USD", "price": 10, "product_id": "505bd76785ebb509fc183733", - "properties": { - "category": "Games", - "name": "Uno Card Game" - }, "quantity": 2, "time": "2020-01-24T11:59:02.402+05:30", "user_alias": { @@ -875,12 +855,6 @@ "purchases": [ { "product_id": "507f1f77bcf86cd799439011", - "properties": { - "category": "Games", - "image_url": "https:///www.example.com/product/path.jpg", - "name": "Monopoly: 3rd Edition", - "url": "https://www.example.com/product/path" - }, "price": 0, "currency": "USD", "quantity": 1, @@ -989,5 +963,76 @@ }, "files": {}, "userId": "mickeyMouse" + }, + { + "body": { + "FORM": {}, + "JSON": { + "attributes": [ + { + "_update_existing_only": false, + "city": "Disney", + "country": "USA", + "email": "mickey@disney.com", + "firstname": "Mickey", + "user_alias": { + "alias_label": "rudder_id", + "alias_name": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca" + } + } + ], + "partner": "RudderStack", + "purchases": [ + { + "_update_existing_only": false, + "currency": "USD", + "price": 0, + "product_id": "507f1f77bcf86cd799439023", + "properties": { + "category": "Games", + "image_url": "https:///www.example.com/product/path.jpg", + "name": "Monopoly: 3rd Edition", + "url": "https://www.example.com/product/path" + }, + "quantity": 1, + "time": "2020-01-24T11:59:02.402+05:30", + "user_alias": { + "alias_label": "rudder_id", + "alias_name": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca" + } + }, + { + "_update_existing_only": false, + "currency": "USD", + "price": 0, + "product_id": "505bd76785ebb509fc183724", + "properties": { + "category": "Games", + "name": "Uno Card Game" + }, + "quantity": 2, + "time": "2020-01-24T11:59:02.402+05:30", + "user_alias": { + "alias_label": "rudder_id", + "alias_name": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca" + } + } + ] + }, + "JSON_ARRAY": {}, + "XML": {} + }, + "endpoint": "https://rest.fra-01.braze.eu/users/track", + "files": {}, + "headers": { + "Accept": "application/json", + "Authorization": "Bearer dummyApiKey", + "Content-Type": "application/json" + }, + "method": "POST", + "params": {}, + "type": "REST", + "userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca", + "version": "1" } ]