diff --git a/src/v0/destinations/awin/utils.js b/src/v0/destinations/awin/utils.js index f0daea9b99..e0a3536f2c 100644 --- a/src/v0/destinations/awin/utils.js +++ b/src/v0/destinations/awin/utils.js @@ -30,15 +30,39 @@ const areAllValuesDefined = (obj) => lodash.every(lodash.values(obj), (value) => !lodash.isUndefined(value)); const buildProductPayloadString = (payload) => { - // URL-encode each value, and join back with the same key. const encodedPayload = Object.entries(payload).reduce((acc, [key, value]) => { - // Encode each value. Assuming that all values are either strings or can be - // safely converted to strings. acc[key] = encodeURIComponent(value); return acc; }, {}); - return `AW:P|${encodedPayload.advertiserId}|${encodedPayload.orderReference}|${encodedPayload.productId}|${encodedPayload.productName}|${encodedPayload.productItemPrice}|${encodedPayload.productQuantity}|${encodedPayload.productSku}|${encodedPayload.commissionGroupCode}|${encodedPayload.productCategory}`; + const { + advertiserId, + orderReference, + productId, + productName, + productItemPrice, + productQuantity, + productSku, + commissionGroupCode, + productCategory, + ...customProperties + } = encodedPayload; + + // Build the base string without custom properties + let baseString = `AW:P|${advertiserId}|${orderReference}|${productId}|${productName}|${productItemPrice}|${productQuantity}|${productSku}|${commissionGroupCode}|${productCategory}`; + + // Check if there are any custom properties + if (Object.keys(customProperties).length > 0) { + // Map custom properties to strings with indexed keys, starting from 1 + const customPropsString = Object.values(customProperties) + .map((value, index) => `&p${index + 1}=${value}`) + .join(''); + // Append the custom properties to the base string + baseString += customPropsString; + } + + // Return the final payload string + return baseString; }; // ref: https://wiki.awin.com/index.php/Advertiser_Tracking_Guide/Product_Level_Tracking#PLT_Via_Conversion_Pixel @@ -52,6 +76,9 @@ const trackProduct = (properties, advertiserId, commissionParts) => { const productsArray = properties.products; let productIndex = 0; productsArray.forEach((product) => { + // eslint-disable-next-line @typescript-eslint/naming-convention + const { product_id, productId, name, price, quantity, sku, category, ...customProperties } = + product; const productPayloadNew = { advertiserId, orderReference: @@ -59,13 +86,14 @@ const trackProduct = (properties, advertiserId, commissionParts) => { properties.orderId || properties.orderReference || properties.order_reference, - productId: product.product_id || product.productId, - productName: product.name, - productItemPrice: product.price, - productQuantity: product.quantity, - productSku: product.sku || '', + productId: product_id || productId, + productName: name, + productItemPrice: price, + productQuantity: quantity, + productSku: sku || '', commissionGroupCode: commissionParts || 'DEFAULT', - productCategory: product.category || '', + productCategory: category || '', + ...customProperties, }; if (areAllValuesDefined(productPayloadNew)) { transformedProductInfoObj[`bd[${productIndex}]`] = diff --git a/test/integrations/destinations/awin/data.ts b/test/integrations/destinations/awin/data.ts index 8ca294b5fb..239875f9da 100644 --- a/test/integrations/destinations/awin/data.ts +++ b/test/integrations/destinations/awin/data.ts @@ -1158,4 +1158,167 @@ export const data = [ }, }, }, + { + name: 'awin', + description: 'Track call- with product array and custom properties', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + advertiserId: '1234', + eventsToTrack: [ + { + eventName: 'abc', + }, + { + eventName: 'prop2', + }, + { + eventName: 'prop3', + }, + ], + }, + }, + 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', + products: [ + { + product_id: '123', + name: 'Product 1', + price: 10, + quantity: 1, + sku: undefined, + category: 'Category 1', + customProp1: 'customVal1', + customProp2: 'customVal2', + customProp3: 'customVal3', + }, + { + 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|123|Product%201|10|1||sales%3A500|Category%201&p1=customVal1&p2=customVal2&p3=customVal3', + 'bd[1]': 'AW:P|1234|QW123|456|Product%202|20|2|SKU456|sales%3A500|', + }, + body: { + JSON: {}, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, ];