Skip to content

Commit

Permalink
fix: adding product array support
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Feb 9, 2024
1 parent acabc11 commit 8656239
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 113 deletions.
13 changes: 3 additions & 10 deletions src/v0/destinations/bluecore/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ const CONFIG_CATEGORIES = {
},
};

// const EVENT_NAME_MAPPING = {
// 'Product Viewed': 'viewed_product',
// 'Products Searched': 'search',
// 'Product Added': 'add_to_cart',
// 'Product Removed': 'remove_from_cart',
// 'Product Added to Wishlist': 'wishlist',
// 'Order Completed': 'purchase',
// 'Identify': 'identify',
// };

const EVENT_NAME_MAPPING = [
{
src: ['Product Viewed'],
Expand Down Expand Up @@ -50,10 +40,13 @@ const EVENT_NAME_MAPPING = [
},
];

const BLUECORE_EXCLUSION_FIELDS = ['query','order_id','total']

const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname);
module.exports = {
CONFIG_CATEGORIES,
MAPPING_CONFIG,
EVENT_NAME_MAPPING,
BASE_URL,
BLUECORE_EXCLUSION_FIELDS
};
168 changes: 91 additions & 77 deletions src/v0/destinations/bluecore/data/bluecoreTrackConfig.json
Original file line number Diff line number Diff line change
@@ -1,78 +1,92 @@
[
{
"destKey": "properties.distinct_id",
"sourceKeys": ["userId", "anonymousId", "email"],
"required": false
},
{
"destKey": "properties.customer.name",
"sourceKeys": "name",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.first_name",
"sourceKeys": "firstName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.last_name",
"sourceKeys": "lastName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.age",
"sourceKeys": ["context.traits.age","traits.age"],
"required": false
},
{
"destKey": "properties.customer.sex",
"sourceKeys": ["traits.gender", "context.traits.gender","traits.sex", "context.traits.sex"],
"required": false
},
{
"destKey": "properties.customer.address",
"sourceKeys": "address",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.email",
"sourceKeys": "email",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.search_term",
"sourceKeys": "properties.query",
"required": false
},
{
"destKey": "properties.order_id",
"sourceKeys": "properties.order_id",
"required": false
},
{
"destKey": "properties.total",
"sourceKeys": "properties.total",
"required": false
},
{
"destKey": "properties.client",
"sourceKeys": "context.app.version",
"required": false
},
{
"destKey": "properties.device",
"sourceKeys": "context.device.model",
"required": false
},
{
"destKey": "properties.products",
"sourceKeys": "properties.products",
"required": false
}
]

{
"destKey": "properties.distinct_id",
"sourceKeys": [
"userId",
"anonymousId",
"email"
],
"required": false
},
{
"destKey": "properties.customer.name",
"sourceKeys": "name",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.first_name",
"sourceKeys": "firstName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.last_name",
"sourceKeys": "lastName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.age",
"sourceKeys": [
"context.traits.age",
"traits.age"
],
"required": false
},
{
"destKey": "properties.customer.sex",
"sourceKeys": [
"traits.gender",
"context.traits.gender",
"traits.sex",
"context.traits.sex"
],
"required": false
},
{
"destKey": "properties.customer.address",
"sourceKeys": "address",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.email",
"sourceKeys": "email",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.search_term",
"sourceKeys": "properties.query",
"required": false
},
{
"destKey": "properties.order_id",
"sourceKeys": "properties.order_id",
"required": false
},
{
"destKey": "properties.total",
"sourceKeys": "properties.total",
"required": false
},
{
"destKey": "properties.client",
"sourceKeys": "context.app.version",
"required": false
},
{
"destKey": "properties.device",
"sourceKeys": "context.device.model",
"required": false
},
{
"destKey": "properties.products",
"sourceKeys": [
"properties.products",
"properties"
],
"required": false
}
]
29 changes: 4 additions & 25 deletions src/v0/destinations/bluecore/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: Add support for product array
// TODO : experiment if a custom event without distinct_id as an identifier reaches bluecore or not

const { isDefinedAndNotNull, ConfigurationError, TransformationError,
Expand All @@ -8,7 +7,6 @@ const {
constructPayload,
ErrorMessage,
defaultRequestConfig,
getValueFromMessage,
simpleProcessRouterDest,
} = require('../../util');

Expand All @@ -17,33 +15,14 @@ const {
CONFIG_CATEGORIES,
BASE_URL,
} = require('./config');
const { verifyPayload, deduceTrackEventName } = require('./util');
const { verifyPayload, deduceTrackEventName, addProductArray } = require('./util');


const addProductArray = (message, payload, eventName) => {
switch (eventName) {
case 'viewed_product':
case 'add_to_cart':
case 'remove_from_cart':
case 'wishlist':
if (isDefinedAndNotNull(message.properties.products)) {
return message.properties.products;
}
break;
case 'purchase':
if (isDefinedAndNotNull(message.properties.products)) {
return message.properties.products;
}
break;
default:
break;
}
};

const trackResponseBuilder = (message, category, { Config }, eventName) => {
const payload = constructPayload(message, MAPPING_CONFIG[category.name]);
// TODO: add support for product array
if(eventName !== 'optin' || eventName !== 'unsubscribe') {
payload.properties.product = addProductArray(message, payload, eventName);
if(eventName !== 'optin' || eventName !== 'unsubscribe' || eventName !== 'search' ) {
payload.properties.product = addProductArray(message, payload.products, eventName);
}
payload.event = eventName;
verifyPayload(payload, message);
Expand Down
15 changes: 14 additions & 1 deletion src/v0/destinations/bluecore/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,21 @@ const deduceTrackEventName = (trackEventName, Config) => {
return trackEventName;
};

const addProductArray = (message, products, eventName) => {
if (!isDefinedAndNotNull(products)) {
throw new InstrumentationError(`Product array is required for ${eventName} event`);
}
const productArray = Array.isArray(products) ? products : [products];
productArray.forEach(({ product_id, query, order_id, total, ...rest }) => ({
id: product_id,
...rest
}));
return productArray;
}

module.exports = {
verifyPayload,
deduceTrackEventName
deduceTrackEventName,
addProductArray
};

0 comments on commit 8656239

Please sign in to comment.