Skip to content

Commit

Permalink
Merge branch 'develop' into feat.sourceV1Intro
Browse files Browse the repository at this point in the history
  • Loading branch information
anantjain45823 authored Oct 16, 2023
2 parents 53c9f67 + 45da19d commit 824b6e4
Show file tree
Hide file tree
Showing 17 changed files with 1,095 additions and 272 deletions.
30 changes: 29 additions & 1 deletion src/v0/destinations/facebook_pixel/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@ const CONFIG_CATEGORIES = {
PRODUCT_LIST_VIEWED: {
standard: true,
type: 'product list viewed',
eventName: 'ViewContent',
name: 'FBPIXELPSimpleCustomConfig',
},
PRODUCT_VIEWED: {
standard: true,
type: 'product viewed',
eventName: 'ViewContent',
name: 'FBPIXELPSimpleCustomConfig',
},
PRODUCT_ADDED: {
standard: true,
type: 'product added',
eventName: 'AddToCart',
name: 'FBPIXELPSimpleCustomConfig',
},
ORDER_COMPLETED: {
standard: true,
type: 'order completed',
eventName: 'Purchase',
name: 'FBPIXELPSimpleCustomConfig',
},
PRODUCTS_SEARCHED: {
standard: true,
type: 'products searched',
eventName: 'Search',
name: 'FBPIXELPSimpleCustomConfig',
},
CHECKOUT_STARTED: {
standard: true,
type: 'checkout started',
eventName: 'InitiateCheckout',
name: 'FBPIXELPSimpleCustomConfig',
},
OTHER_STANDARD: {
Expand All @@ -50,9 +56,15 @@ const CONFIG_CATEGORIES = {
PAGE_VIEW: {
standard: true,
type: 'page_view',
eventName: 'PageView',
name: 'FBPIXELPSimpleCustomConfig',
},
PAGE: {
standard: false,
type: 'page',
eventName: 'PageView',
name: 'FBPIXELPSimpleCustomConfig',
},
PAGE: { standard: false, type: 'page', name: 'FBPIXELPSimpleCustomConfig' },
};

const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname);
Expand All @@ -67,6 +79,21 @@ const ACTION_SOURCES_VALUES = [
'other',
];

const OTHER_STANDARD_EVENTS = [
'AddToWishlist',
'AddPaymentInfo',
'Lead',
'CompleteRegistration',
'Contact',
'CustomizeProduct',
'Donate',
'FindLocation',
'Schedule',
'StartTrial',
'SubmitApplication',
'Subscribe',
];

const FB_PIXEL_DEFAULT_EXCLUSION = ['opt_out', 'event_id', 'action_source'];
const STANDARD_ECOMM_EVENTS_TYPE = [
CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.type,
Expand All @@ -83,5 +110,6 @@ module.exports = {
ACTION_SOURCES_VALUES,
FB_PIXEL_DEFAULT_EXCLUSION,
STANDARD_ECOMM_EVENTS_TYPE,
OTHER_STANDARD_EVENTS,
DESTINATION: 'FACEBOOK_PIXEL',
};
181 changes: 35 additions & 146 deletions src/v0/destinations/facebook_pixel/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
getIntegrationsObj,
getValidDynamicFormConfig,
simpleProcessRouterDest,
getHashFromArray,
} = require('../../util');

const {
Expand All @@ -28,13 +29,19 @@ const {
handleProductListViewed,
handleOrder,
formingFinalResponse,
populateCustomDataBasedOnCategory,
getCategoryFromEvent,
} = require('./utils');

const { InstrumentationError, ConfigurationError } = require('../../util/errorTypes');

const responseBuilderSimple = (message, category, destination, categoryToContent) => {
const { Config } = destination;
const responseBuilderSimple = (message, category, destination) => {
const { Config, ID } = destination;
const { pixelId, accessToken } = Config;
let { categoryToContent } = Config;
if (Array.isArray(categoryToContent)) {
categoryToContent = getValidDynamicFormConfig(categoryToContent, 'from', 'to', 'FB_PIXEL', ID);
}

if (!pixelId) {
throw new ConfigurationError('Pixel Id not found. Aborting');
Expand All @@ -59,12 +66,15 @@ const responseBuilderSimple = (message, category, destination, categoryToContent

const userData = fetchUserData(message, Config);

let customData = {};
let commonData = {};

commonData = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.COMMON.name], 'fb_pixel');
const commonData = constructPayload(
message,
MAPPING_CONFIG[CONFIG_CATEGORIES.COMMON.name],
'fb_pixel',
);
commonData.action_source = getActionSource(commonData, message?.channel);

let customData = {};

if (category.type !== 'identify') {
customData = flattenJson(
extractCustomFields(message, customData, ['properties'], FB_PIXEL_DEFAULT_EXCLUSION),
Expand All @@ -82,74 +92,20 @@ const responseBuilderSimple = (message, category, destination, categoryToContent
customData,
blacklistPiiProperties,
whitelistPiiProperties,
category.standard,
integrationsObj,
);
message.properties = message.properties || {};
if (category.standard) {
switch (category.type) {
case 'product list viewed':
customData = {
...customData,
...handleProductListViewed(message, categoryToContent),
};
commonData.event_name = 'ViewContent';
break;
case 'product viewed':
customData = {
...customData,
...handleProduct(message, categoryToContent, valueFieldIdentifier),
};
commonData.event_name = 'ViewContent';
break;
case 'product added':
customData = {
...customData,
...handleProduct(message, categoryToContent, valueFieldIdentifier),
};
commonData.event_name = 'AddToCart';
break;
case 'order completed':
customData = {
...customData,
...handleOrder(message, categoryToContent),
};
commonData.event_name = 'Purchase';
break;
case 'products searched': {
customData = {
...customData,
...handleSearch(message),
};
commonData.event_name = 'Search';
break;
}
case 'checkout started': {
const orderPayload = handleOrder(message, categoryToContent);
delete orderPayload.content_name;
customData = {
...customData,
...orderPayload,
};
commonData.event_name = 'InitiateCheckout';
break;
}
case 'page_view': // executed when sending track calls but with standard type PageView
case 'page': // executed when page call is done with standard PageView turned on
customData = { ...customData };
commonData.event_name = 'PageView';
break;
case 'otherStandard':
customData = { ...customData };
commonData.event_name = category.event;
break;
default:
throw new InstrumentationError(
`${category.standard} type of standard event does not exist`,
);
}
commonData.event_name = category.eventName;
customData = populateCustomDataBasedOnCategory(
customData,
message,
category,
categoryToContent,
valueFieldIdentifier,
);
customData.currency = STANDARD_ECOMM_EVENTS_TYPE.includes(category.type)
? message.properties.currency || 'USD'
? message.properties?.currency || 'USD'
: undefined;
} else {
const { type } = category;
Expand All @@ -159,7 +115,7 @@ const responseBuilderSimple = (message, category, destination, categoryToContent
: `Viewed a ${type}`;
}
if (type === 'simple track') {
customData.value = message.properties ? message.properties.revenue : undefined;
customData.value = message.properties?.revenue;
delete customData.revenue;
}
}
Expand Down Expand Up @@ -189,57 +145,6 @@ const responseBuilderSimple = (message, category, destination, categoryToContent
);
};

function getCategoryFromEvent(checkEvent) {
let category;
switch (checkEvent) {
case CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.type:
case 'ViewContent':
category = CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED;
break;
case CONFIG_CATEGORIES.PRODUCT_VIEWED.type:
category = CONFIG_CATEGORIES.PRODUCT_VIEWED;
break;
case CONFIG_CATEGORIES.PRODUCT_ADDED.type:
case 'AddToCart':
category = CONFIG_CATEGORIES.PRODUCT_ADDED;
break;
case CONFIG_CATEGORIES.ORDER_COMPLETED.type:
case 'Purchase':
category = CONFIG_CATEGORIES.ORDER_COMPLETED;
break;
case CONFIG_CATEGORIES.PRODUCTS_SEARCHED.type:
case 'Search':
category = CONFIG_CATEGORIES.PRODUCTS_SEARCHED;
break;
case CONFIG_CATEGORIES.CHECKOUT_STARTED.type:
case 'InitiateCheckout':
category = CONFIG_CATEGORIES.CHECKOUT_STARTED;
break;
case 'AddToWishlist':
case 'AddPaymentInfo':
case 'Lead':
case 'CompleteRegistration':
case 'Contact':
case 'CustomizeProduct':
case 'Donate':
case 'FindLocation':
case 'Schedule':
case 'StartTrial':
case 'SubmitApplication':
case 'Subscribe':
category = CONFIG_CATEGORIES.OTHER_STANDARD;
category.event = checkEvent;
break;
case 'PageView':
category = CONFIG_CATEGORIES.PAGE_VIEW;
break;
default:
category = CONFIG_CATEGORIES.SIMPLE_TRACK;
break;
}
return category;
}

const processEvent = (message, destination) => {
if (!message.type) {
throw new InstrumentationError("'type' is missing");
Expand All @@ -265,29 +170,20 @@ const processEvent = (message, destination) => {
}

let eventsToEvents;
if (destination.Config.eventsToEvents)
if (Array.isArray(destination.Config.eventsToEvents)) {
eventsToEvents = getValidDynamicFormConfig(
destination.Config.eventsToEvents,
'from',
'to',
'FB_PIXEL',
destination.ID,
);
let categoryToContent;
if (destination.Config.categoryToContent)
categoryToContent = getValidDynamicFormConfig(
destination.Config.categoryToContent,
'from',
'to',
'FB_PIXEL',
destination.ID,
);
}

const { advancedMapping } = destination.Config;
let standard;
let standardTo = '';
let checkEvent;
const messageType = message.type.toLowerCase();
let category;
let mappedEvent;
switch (messageType) {
case EventType.IDENTIFY:
if (advancedMapping) {
Expand All @@ -309,24 +205,17 @@ const processEvent = (message, destination) => {
if (typeof message.event !== 'string') {
throw new InstrumentationError('event name should be string');
}
standard = eventsToEvents;
if (standard) {
standardTo = standard.reduce((filtered, standards) => {
if (standards.from.toLowerCase() === message.event.toLowerCase()) {
filtered = standards.to;
}
return filtered;
}, '');
if (eventsToEvents) {
const eventMappingHash = getHashFromArray(eventsToEvents);
mappedEvent = eventMappingHash[message.event.toLowerCase()];
}
checkEvent = standardTo !== '' ? standardTo : message.event.toLowerCase();

category = getCategoryFromEvent(checkEvent);
category = getCategoryFromEvent(mappedEvent || message.event.toLowerCase());
break;
default:
throw new InstrumentationError(`Message type ${messageType} not supported`);
}
// build the response
return responseBuilderSimple(message, category, destination, categoryToContent);
return responseBuilderSimple(message, category, destination);
};

const process = (event) => processEvent(event.message, event.destination);
Expand Down
Loading

0 comments on commit 824b6e4

Please sign in to comment.