Skip to content

Commit

Permalink
feat: custom property support AWIN (#3325)
Browse files Browse the repository at this point in the history
* feat: custom property support AWIN

* feat: adding awin property pattern check logic
  • Loading branch information
shrouti1507 authored Apr 30, 2024
1 parent e57d7e0 commit fdecaf3
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/v0/destinations/awin/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions src/v0/destinations/awin/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { getHashFromArray } = require('@rudderstack/integrations-lib');
const lodash = require('lodash');

/**
Expand Down Expand Up @@ -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,
};
31 changes: 30 additions & 1 deletion src/v0/destinations/awin/utils.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
});
});
Loading

0 comments on commit fdecaf3

Please sign in to comment.