Skip to content

Commit

Permalink
Merge branch 'develop' into fb_pixel_v1_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepdsvs authored Mar 1, 2024
2 parents 05ade78 + 0eb2393 commit f8f7eec
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 385 deletions.
48 changes: 25 additions & 23 deletions src/v0/destinations/facebook_conversions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,26 @@ const populateCustomDataBasedOnCategory = (customData, message, category, catego
);

const contentCategory = eventTypeCustomData.content_category;
let contentType;
let defaultContentType;
if (contentIds.length > 0) {
contentType = 'product';
defaultContentType = 'product';
} else if (contentCategory) {
contentIds.push(contentCategory);
contents.push({
id: contentCategory,
quantity: 1,
});
contentType = 'product_group';
defaultContentType = 'product_group';
}
const contentType =
message.properties?.content_type ||
getContentType(message, defaultContentType, categoryToContent, DESTINATION.toLowerCase());

eventTypeCustomData = {
...eventTypeCustomData,
content_ids: contentIds,
contents,
content_type: getContentType(
message,
contentType,
categoryToContent,
DESTINATION.toLowerCase(),
),
content_type: contentType,
content_category: getContentCategory(contentCategory),
};
break;
Expand All @@ -125,18 +123,20 @@ const populateCustomDataBasedOnCategory = (customData, message, category, catego
case 'payment info entered':
case 'product added to wishlist': {
const contentCategory = eventTypeCustomData.content_category;
const contentType = eventTypeCustomData.content_type;
const contentType =
message.properties?.content_type ||
getContentType(
message,
eventTypeCustomData.content_type,
categoryToContent,
DESTINATION.toLowerCase(),
);
const { contentIds, contents } = populateContentsAndContentIDs([message.properties]);
eventTypeCustomData = {
...eventTypeCustomData,
content_ids: contentIds,
contents,
content_type: getContentType(
message,
contentType,
categoryToContent,
DESTINATION.toLowerCase(),
),
content_type: contentType,
content_category: getContentCategory(contentCategory),
};
validateProductSearchedData(eventTypeCustomData);
Expand All @@ -151,18 +151,20 @@ const populateCustomDataBasedOnCategory = (customData, message, category, catego
);

const contentCategory = eventTypeCustomData.content_category;
const contentType = eventTypeCustomData.content_type;
const contentType =
message.properties?.content_type ||
getContentType(
message,
eventTypeCustomData.content_type,
categoryToContent,
DESTINATION.toLowerCase(),
);

eventTypeCustomData = {
...eventTypeCustomData,
content_ids: contentIds,
contents,
content_type: getContentType(
message,
contentType,
categoryToContent,
DESTINATION.toLowerCase(),
),
content_type: contentType,
content_category: getContentCategory(contentCategory),
num_items: contentIds.length,
};
Expand Down
34 changes: 22 additions & 12 deletions src/v0/destinations/facebook_pixel/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ const getActionSource = (payload, channel) => {
* Handles order completed and checkout started types of specific events
*/
const handleOrder = (message, categoryToContent) => {
const { products, revenue } = message.properties;
const value = formatRevenue(revenue);

const contentType = getContentType(message, 'product', categoryToContent);
const contentIds = [];
const contents = [];
const {
products,
revenue,
category,
quantity,
price,
currency,
contentName,
delivery_category: deliveryCategory,
} = message.properties;
const value = formatRevenue(revenue);
let { content_type: contentType } = message.properties;
contentType = contentType || getContentType(message, 'product', categoryToContent);
const contentIds = [];
const contents = [];

if (products) {
if (products.length > 0 && Array.isArray(products)) {
products.forEach((singleProduct) => {
Expand Down Expand Up @@ -109,10 +111,17 @@ const handleOrder = (message, categoryToContent) => {
* Handles product list viewed
*/
const handleProductListViewed = (message, categoryToContent) => {
let contentType;
let defaultContentType;
const contentIds = [];
const contents = [];
const { products, category, quantity, value, contentName } = message.properties;
const {
products,
category,
quantity,
value,
contentName,
content_type: contentType,
} = message.properties;
if (products && products.length > 0 && Array.isArray(products)) {
products.forEach((product, index) => {
if (isObject(product)) {
Expand All @@ -132,20 +141,20 @@ const handleProductListViewed = (message, categoryToContent) => {
}

if (contentIds.length > 0) {
contentType = 'product';
defaultContentType = 'product';
// for viewContent event content_ids and content arrays are not mandatory
} else if (category) {
contentIds.push(category);
contents.push({
id: category,
quantity: 1,
});
contentType = 'product_group';
defaultContentType = 'product_group';
}

return {
content_ids: contentIds,
content_type: getContentType(message, contentType, categoryToContent),
content_type: contentType || getContentType(message, defaultContentType, categoryToContent),
contents,
content_category: getContentCategory(category),
content_name: contentName,
Expand All @@ -165,7 +174,8 @@ const handleProduct = (message, categoryToContent, valueFieldIdentifier) => {
const useValue = valueFieldIdentifier === 'properties.value';
const contentId =
message.properties?.product_id || message.properties?.sku || message.properties?.id;
const contentType = getContentType(message, 'product', categoryToContent);
const contentType =
message.properties?.content_type || getContentType(message, 'product', categoryToContent);
const contentName = message.properties.product_name || message.properties.name || '';
const contentCategory = message.properties.category || '';
const currency = message.properties.currency || 'USD';
Expand Down
26 changes: 23 additions & 3 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
groupEventsByEndpoint,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
} = require('./util');
const { CommonUtils } = require('../../../util/common');

Expand Down Expand Up @@ -297,17 +298,25 @@ const processIdentifyEvents = async (message, type, destination) => {
};

const processPageOrScreenEvents = (message, type, destination) => {
const {
token,
identityMergeApi,
useUserDefinedPageEventName,
userDefinedPageEventTemplate,
useUserDefinedScreenEventName,
userDefinedScreenEventTemplate,
} = destination.Config;
const mappedProperties = constructPayload(message, mPEventPropertiesConfigJson);
let properties = {
...get(message, 'context.traits'),
...message.properties,
...mappedProperties,
token: destination.Config.token,
token,
distinct_id: message.userId || message.anonymousId,
time: toUnixTimestampInMS(message.timestamp || message.originalTimestamp),
...buildUtmParams(message.context?.campaign),
};
if (destination.Config?.identityMergeApi === 'simplified') {
if (identityMergeApi === 'simplified') {
properties = {
...properties,
distinct_id: message.userId || `$device:${message.anonymousId}`,
Expand All @@ -326,7 +335,18 @@ const processPageOrScreenEvents = (message, type, destination) => {
properties.$browser = browser.name;
properties.$browser_version = browser.version;
}
const eventName = type === 'page' ? 'Loaded a Page' : 'Loaded a Screen';

let eventName;
if (type === 'page') {
eventName = useUserDefinedPageEventName
? generatePageOrScreenCustomEventName(message, userDefinedPageEventTemplate)
: 'Loaded a Page';
} else {
eventName = useUserDefinedScreenEventName
? generatePageOrScreenCustomEventName(message, userDefinedScreenEventTemplate)
: 'Loaded a Screen';
}

const payload = {
event: eventName,
properties,
Expand Down
44 changes: 43 additions & 1 deletion src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const lodash = require('lodash');
const set = require('set-value');
const get = require('get-value');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
isDefined,
constructPayload,
Expand All @@ -16,6 +16,7 @@ const {
IsGzipSupported,
isObject,
isDefinedAndNotNullAndNotEmpty,
isDefinedAndNotNull,
} = require('../../util');
const {
ConfigCategory,
Expand Down Expand Up @@ -301,6 +302,46 @@ function trimTraits(traits, contextTraits, setOnceProperties) {
};
}

/**
* Generates a custom event name for a page or screen.
*
* @param {Object} message - The message object
* @param {string} userDefinedEventTemplate - The user-defined event template to be used for generating the event name.
* @throws {ConfigurationError} If the event template is missing.
* @returns {string} The generated custom event name.
* @example
* const userDefinedEventTemplate = "Viewed {{ category }} {{ name }} Page";
* const message = {name: 'Home', properties: {category: 'Index'}};
* output: "Viewed Index Home Page"
*/
const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) => {
if (!userDefinedEventTemplate) {
throw new ConfigurationError(
'Event name template is not configured. Please provide a valid value for the `Page/Screen Event Name Template` in the destination dashboard.',
);
}

let eventName = userDefinedEventTemplate;

if (isDefinedAndNotNull(message.properties?.category)) {
// Replace {{ category }} with actual values
eventName = eventName.replace(/{{\s*category\s*}}/g, message.properties.category);
} else {
// find {{ category }} surrounded by whitespace characters and replace it with a single whitespace character
eventName = eventName.replace(/\s{{\s*category\s*}}\s/g, ' ');
}

if (isDefinedAndNotNull(message.name)) {
// Replace {{ name }} with actual values
eventName = eventName.replace(/{{\s*name\s*}}/g, message.name);
} else {
// find {{ name }} surrounded by whitespace characters and replace it with a single whitespace character
eventName = eventName.replace(/\s{{\s*name\s*}}\s/g, ' ');
}

return eventName;
};

module.exports = {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
Expand All @@ -309,4 +350,5 @@ module.exports = {
generateBatchedPayloadForArray,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
};
Loading

0 comments on commit f8f7eec

Please sign in to comment.