From fdecaf36d91db7540d6f68a013e4f7fb2a36ebaa Mon Sep 17 00:00:00 2001 From: shrouti1507 <60211312+shrouti1507@users.noreply.github.com> Date: Tue, 30 Apr 2024 10:32:14 +0530 Subject: [PATCH] feat: custom property support AWIN (#3325) * feat: custom property support AWIN * feat: adding awin property pattern check logic --- src/v0/destinations/awin/transform.js | 9 +- src/v0/destinations/awin/utils.js | 19 ++ src/v0/destinations/awin/utils.test.js | 31 +- test/integrations/destinations/awin/data.ts | 317 ++++++++++++++++++++ 4 files changed, 373 insertions(+), 3 deletions(-) diff --git a/src/v0/destinations/awin/transform.js b/src/v0/destinations/awin/transform.js index 0d7fd95c33..68dd9d62e1 100644 --- a/src/v0/destinations/awin/transform.js +++ b/src/v0/destinations/awin/transform.js @@ -2,10 +2,10 @@ const { InstrumentationError, ConfigurationError } = require('@rudderstack/integ const { BASE_URL, ConfigCategory, mappingConfig } = require('./config'); const { defaultRequestConfig, constructPayload, simpleProcessRouterDest } = require('../../util'); -const { getParams, trackProduct } = require('./utils'); +const { getParams, trackProduct, populateCustomTransactionProperties } = require('./utils'); const responseBuilder = (message, { Config }) => { - const { advertiserId, eventsToTrack } = Config; + const { advertiserId, eventsToTrack, customFieldMap } = Config; const { event, properties } = message; let finalParams = {}; @@ -22,10 +22,15 @@ const responseBuilder = (message, { Config }) => { if (eventsList.includes(event)) { params = getParams(payload.params, advertiserId); const productTrackObject = trackProduct(properties, advertiserId, params.parts); + const customTransactionProperties = populateCustomTransactionProperties( + properties, + customFieldMap, + ); finalParams = { ...params, ...productTrackObject, + ...customTransactionProperties, }; } else { throw new InstrumentationError( diff --git a/src/v0/destinations/awin/utils.js b/src/v0/destinations/awin/utils.js index f0daea9b99..715fb5818d 100644 --- a/src/v0/destinations/awin/utils.js +++ b/src/v0/destinations/awin/utils.js @@ -1,3 +1,4 @@ +const { getHashFromArray } = require('@rudderstack/integrations-lib'); const lodash = require('lodash'); /** @@ -77,8 +78,26 @@ const trackProduct = (properties, advertiserId, commissionParts) => { return transformedProductInfoObj; }; +// ref: https://wiki.awin.com/index.php/Advertiser_Tracking_Guide/Product_Level_Tracking#PLT_Via_Conversion_Pixel +const populateCustomTransactionProperties = (properties, customFieldMap) => { + const customObject = {}; + const customPropertyPattern = '^\\s*p\\d+\\s*$'; + const regex = new RegExp(customPropertyPattern, 'i'); + const propertyMap = getHashFromArray(customFieldMap, 'from', 'to', false); + Object.entries(propertyMap).forEach(([rudderProperty, awinProperty]) => { + if (regex.test(awinProperty)) { + const fieldValue = properties[rudderProperty]; + if (fieldValue) { + customObject[awinProperty] = fieldValue; + } + } + }); + return customObject; +}; + module.exports = { getParams, trackProduct, buildProductPayloadString, + populateCustomTransactionProperties, }; diff --git a/src/v0/destinations/awin/utils.test.js b/src/v0/destinations/awin/utils.test.js index e60c07e96c..ca7d079b1b 100644 --- a/src/v0/destinations/awin/utils.test.js +++ b/src/v0/destinations/awin/utils.test.js @@ -1,4 +1,8 @@ -const { buildProductPayloadString, trackProduct } = require('./utils'); +const { + buildProductPayloadString, + trackProduct, + populateCustomTransactionProperties, +} = require('./utils'); describe('buildProductPayloadString', () => { // Should correctly build the payload string with all fields provided @@ -163,3 +167,28 @@ describe('trackProduct', () => { }); }); }); + +describe('populateCustomTransactionProperties', () => { + // The function should correctly map properties from the input object to the output object based on the customFieldMap. + it('should correctly map properties from the input object to the output object based on the customFieldMap', () => { + const properties = { + rudderProperty1: 'value1', + rudderProperty2: 123, + rudderProperty3: 'value3', + rudderProperty4: 234, + }; + const customFieldMap = [ + { from: 'rudderProperty1', to: 'p1' }, + { from: 'rudderProperty2', to: 'p2' }, + { from: 'rudderProperty4', to: 'anotherp2' }, + ]; + const expectedOutput = { + p1: 'value1', + p2: 123, + }; + + const result = populateCustomTransactionProperties(properties, customFieldMap); + + expect(result).toEqual(expectedOutput); + }); +}); diff --git a/test/integrations/destinations/awin/data.ts b/test/integrations/destinations/awin/data.ts index 8ca294b5fb..5a7fcfb50f 100644 --- a/test/integrations/destinations/awin/data.ts +++ b/test/integrations/destinations/awin/data.ts @@ -1158,4 +1158,321 @@ export const data = [ }, }, }, + { + name: 'awin', + description: 'Track call: with custom transaction property', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + advertiserId: '1234', + eventsToTrack: [ + { + eventName: 'abc', + }, + { + eventName: 'prop2', + }, + { + eventName: 'prop3', + }, + ], + customFieldMap: [ + { + from: 'customTransactionValue1', + to: 'p1', + }, + { + from: 'customTransactionValue2', + to: 'p2', + }, + ], + }, + }, + message: { + type: 'track', + event: 'abc', + sentAt: '2022-01-20T13:39:21.033Z', + userId: 'user123456001', + channel: 'web', + properties: { + currency: 'INR', + voucherCode: '1bcu1', + amount: 125, + customTransactionValue1: 'val1', + customTransactionValue2: 'val2', + }, + context: { + os: { + name: '', + version: '', + }, + app: { + name: 'RudderLabs JavaScript SDK', + build: '1.0.0', + version: '1.2.20', + namespace: 'com.rudderlabs.javascript', + }, + page: { + url: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/ourSdk.html', + path: '/Testing/App_for_LaunchDarkly/ourSdk.html', + title: 'Document', + search: '', + tab_url: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/ourSdk.html', + referrer: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/', + initial_referrer: '$direct', + referring_domain: '127.0.0.1:7307', + initial_referring_domain: '', + }, + locale: 'en-US', + screen: { + width: 1440, + height: 900, + density: 2, + innerWidth: 536, + innerHeight: 689, + }, + traits: { + city: 'Pune', + name: 'First User', + email: 'firstUser@testmail.com', + title: 'VP', + gender: 'female', + avatar: 'https://i.pravatar.cc/300', + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.2.20', + }, + campaign: {}, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36', + }, + rudderId: '553b5522-c575-40a7-8072-9741c5f9a647', + messageId: '831f1fa5-de84-4f22-880a-4c3f23fc3f04', + anonymousId: 'bf412108-0357-4330-b119-7305e767823c', + integrations: { + All: true, + }, + originalTimestamp: '2022-01-20T13:39:21.032Z', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://www.awin1.com/sread.php', + headers: {}, + params: { + amount: 125, + ch: 'aw', + cr: 'INR', + tt: 'ss', + tv: '2', + vc: '1bcu1', + merchant: '1234', + parts: 'DEFAULT:125', + testmode: '0', + p1: 'val1', + p2: 'val2', + }, + body: { + JSON: {}, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: 'awin', + description: 'Track call- with product array, along with transactional custom property', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + advertiserId: '1234', + eventsToTrack: [ + { + eventName: 'abc', + }, + { + eventName: 'prop2', + }, + { + eventName: 'prop3', + }, + ], + customFieldMap: [ + { + from: 'customTransactionValue1', + to: 'p1', + }, + { + from: 'customTransactionValue2', + to: 'p2', + }, + ], + }, + }, + message: { + type: 'track', + event: 'prop2', + sentAt: '2022-01-20T13:39:21.033Z', + userId: 'user123456001', + channel: 'web', + properties: { + currency: 'INR', + voucherCode: '1bcu1', + amount: 500, + commissionGroup: 'sales', + cks: 'new', + testMode: '1', + order_id: 'QW123', + customTransactionValue1: 'val1', + customTransactionValue2: 'val2', + products: [ + { + price: 10, + quantity: 1, + sku: undefined, + category: 'Category 1', + }, + { + product_id: '456', + name: 'Product 2', + price: 20, + quantity: 2, + sku: 'SKU456', + category: undefined, + }, + ], + }, + context: { + os: { + name: '', + version: '', + }, + app: { + name: 'RudderLabs JavaScript SDK', + build: '1.0.0', + version: '1.2.20', + namespace: 'com.rudderlabs.javascript', + }, + page: { + url: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/ourSdk.html', + path: '/Testing/App_for_LaunchDarkly/ourSdk.html', + title: 'Document', + search: '', + tab_url: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/ourSdk.html', + referrer: 'http://127.0.0.1:7307/Testing/App_for_LaunchDarkly/', + initial_referrer: '$direct', + referring_domain: '127.0.0.1:7307', + initial_referring_domain: '', + }, + locale: 'en-US', + screen: { + width: 1440, + height: 900, + density: 2, + innerWidth: 536, + innerHeight: 689, + }, + traits: { + city: 'Pune', + name: 'First User', + email: 'firstUser@testmail.com', + title: 'VP', + gender: 'female', + avatar: 'https://i.pravatar.cc/300', + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.2.20', + }, + campaign: {}, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36', + }, + rudderId: '553b5522-c575-40a7-8072-9741c5f9a647', + messageId: '831f1fa5-de84-4f22-880a-4c3f23fc3f04', + anonymousId: 'bf412108-0357-4330-b119-7305e767823c', + integrations: { + All: true, + }, + originalTimestamp: '2022-01-20T13:39:21.032Z', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://www.awin1.com/sread.php', + headers: {}, + params: { + amount: 500, + ch: 'aw', + parts: 'sales:500', + cr: 'INR', + tt: 'ss', + tv: '2', + vc: '1bcu1', + cks: 'new', + merchant: '1234', + testmode: '1', + ref: 'QW123', + 'bd[0]': 'AW:P|1234|QW123|456|Product%202|20|2|SKU456|sales%3A500|', + p1: 'val1', + p2: 'val2', + }, + body: { + JSON: {}, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, ];