Skip to content

Commit

Permalink
feat: add delivery_category as part of contents, update flattening cu…
Browse files Browse the repository at this point in the history
…stom_data in FB Pixel (#2816)

* feat: remove contents and content_ids from flattening before setting in custom_data

* feat: add delivery_category as part of contents

* feat: update API version to v18.0

* chore: updated tests

* feat: revert v18 version update

* feat: added tests

* feat: added tests
  • Loading branch information
sandeepdsvs authored Nov 13, 2023
1 parent 916ea4c commit ee1f473
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/v0/destinations/facebook_pixel/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const OTHER_STANDARD_EVENTS = [
];

const FB_PIXEL_DEFAULT_EXCLUSION = ['opt_out', 'event_id', 'action_source'];
const FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING = ['content_ids', 'contents'];
const STANDARD_ECOMM_EVENTS_TYPE = [
CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.type,
CONFIG_CATEGORIES.PRODUCT_VIEWED.type,
Expand All @@ -109,6 +110,7 @@ module.exports = {
MAPPING_CONFIG,
ACTION_SOURCES_VALUES,
FB_PIXEL_DEFAULT_EXCLUSION,
FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING,
STANDARD_ECOMM_EVENTS_TYPE,
OTHER_STANDARD_EVENTS,
DESTINATION: 'FACEBOOK_PIXEL',
Expand Down
13 changes: 12 additions & 1 deletion src/v0/destinations/facebook_pixel/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
CONFIG_CATEGORIES,
MAPPING_CONFIG,
FB_PIXEL_DEFAULT_EXCLUSION,
FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING,
STANDARD_ECOMM_EVENTS_TYPE,
} = require('./config');
const { EventType } = require('../../../constants');
Expand Down Expand Up @@ -84,7 +85,12 @@ const responseBuilderSimple = (message, category, destination) => {

if (category.type !== 'identify') {
customData = flattenJson(
extractCustomFields(message, customData, ['properties'], FB_PIXEL_DEFAULT_EXCLUSION),
extractCustomFields(
message,
customData,
['properties'],
[...FB_PIXEL_DEFAULT_EXCLUSION, ...FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING],
),
);
if (standardPageCall && category.type === 'page') {
category.standard = true;
Expand Down Expand Up @@ -124,6 +130,11 @@ const responseBuilderSimple = (message, category, destination) => {
if (type === 'simple track') {
customData.value = message.properties?.revenue;
delete customData.revenue;
FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING.forEach((customDataParameter) => {
if (message.properties?.[customDataParameter]) {
customData[customDataParameter] = message.properties[customDataParameter];
}
});
}
}
} else {
Expand Down
22 changes: 20 additions & 2 deletions src/v0/destinations/facebook_pixel/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { isObject } = require('../../util');
const { ACTION_SOURCES_VALUES, CONFIG_CATEGORIES, OTHER_STANDARD_EVENTS } = require('./config');
const {
ACTION_SOURCES_VALUES,
CONFIG_CATEGORIES,
OTHER_STANDARD_EVENTS,
FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING,
} = require('./config');
const { getContentType, getContentCategory } = require('../../util/facebookUtils');

/** format revenue according to fb standards with max two decimal places.
Expand Down Expand Up @@ -54,7 +59,14 @@ const handleOrder = (message, categoryToContent) => {
const contentType = getContentType(message, 'product', categoryToContent);
const contentIds = [];
const contents = [];
const { category, quantity, price, currency, contentName } = message.properties;
const {
category,
quantity,
price,
currency,
contentName,
delivery_category: deliveryCategory,
} = message.properties;
if (products) {
if (products.length > 0 && Array.isArray(products)) {
products.forEach((singleProduct) => {
Expand All @@ -67,6 +79,7 @@ const handleOrder = (message, categoryToContent) => {
id: pId,
quantity: singleProduct.quantity || quantity || 1,
item_price: singleProduct.price || price,
delivery_category: singleProduct.delivery_category || deliveryCategory,
};
contents.push(content);
}
Expand Down Expand Up @@ -262,6 +275,11 @@ const populateCustomDataBasedOnCategory = (
case 'page': // executed when page call is done with standard PageView turned on
case 'otherStandard':
updatedCustomData = { ...customData };
FB_PIXEL_CUSTOM_DATA_EXCLUDE_FLATTENING.forEach((customDataParameter) => {
if (message.properties?.[customDataParameter]) {
updatedCustomData[customDataParameter] = message.properties[customDataParameter];
}
});
break;
default:
throw new InstrumentationError(`${category.standard} type of standard event does not exist`);
Expand Down
Loading

0 comments on commit ee1f473

Please sign in to comment.