Skip to content

Commit

Permalink
chore: updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Feb 18, 2025
1 parent ac5b2a4 commit ff47d41
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/v0/destinations/snapchat_conversion/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const mappingConfigV3 = getMappingConfig(ConfigCategoryV3, __dirname);
module.exports = {
ENDPOINT,
ConfigCategory,
ConfigCategoryV3,
eventNameMapping,
mappingConfig,
mappingConfigV3,
Expand Down
62 changes: 41 additions & 21 deletions src/v0/destinations/snapchat_conversion/transformV3.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const {
ENDPOINT,
eventNameMapping,
mappingConfigV3,
ConfigCategoryV3,
MAX_BATCH_SIZE,
pageTypeToTrackEvent,
ConfigCategoryV3,
} = require('./config');
const {
msUnixTimestamp,
Expand Down Expand Up @@ -50,6 +50,7 @@ function buildResponse(apiKey, payload, message) {
const populateHashedTraitsValues = (payload, message) => {
const firstName = getFieldValueFromMessage(message, 'firstName');
const lastName = getFieldValueFromMessage(message, 'lastName');
const middleName = getFieldValueFromMessage(message, 'middleName');
const city = getFieldValueFromMessage(message, 'city');
const state = getFieldValueFromMessage(message, 'state');
const zip = getFieldValueFromMessage(message, 'zipcode');
Expand All @@ -59,6 +60,7 @@ const populateHashedTraitsValues = (payload, message) => {
{
user_data: {
fn: firstName ? getHashedValue(firstName.toString().toLowerCase().trim()) : undefined,
mn: middleName ? getHashedValue(middleName.toString().toLowerCase().trim()) : undefined,
ln: lastName ? getHashedValue(lastName.toString().toLowerCase().trim()) : undefined,
ct: city ? getHashedValue(city.toString().toLowerCase().trim()) : undefined,
zp: zip ? getHashedValue(zip.toString().toLowerCase().trim()) : undefined,
Expand All @@ -85,13 +87,13 @@ const populateHashedValues = (payload, message) => {

const updatedPayload = populateHashedTraitsValues(payload, message);
if (email) {
updatedPayload.data.user_data.em = getHashedValue(email.toString().toLowerCase().trim());
updatedPayload.data[0].user_data.em = getHashedValue(email.toString().toLowerCase().trim());
}
if (phone) {
updatedPayload.data.user_data.ph = getHashedValue(phone.toString().toLowerCase().trim());
updatedPayload.data[0].user_data.ph = getHashedValue(phone.toString().toLowerCase().trim());
}
if (ip) {
updatedPayload.data.user_data.client_ip_address = getHashedValue(
updatedPayload.data[0].user_data.client_ip_address = getHashedValue(

Check warning on line 96 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L96

Added line #L96 was not covered by tests
ip.toString().toLowerCase().trim(),
);
}
Expand All @@ -100,13 +102,13 @@ const populateHashedValues = (payload, message) => {
isAppleFamily(message.context?.device?.type) &&
(message.properties?.idfv || message.context?.device?.id)
) {
updatedPayload.data.user_data.idfv = getHashedValue(
updatedPayload.data[0].user_data.idfv = getHashedValue(
message.properties?.idfv || message.context?.device?.id,
);
}

if (message.properties?.adId || message.context?.device?.advertisingId) {
updatedPayload.data.user_data.madid = getHashedValue(
updatedPayload.data[0].user_data.madid = getHashedValue(
message.properties?.adId || message.context?.device?.advertisingId,
);
}
Expand All @@ -129,10 +131,10 @@ const validateEventConfiguration = (actionSource, pixelId, snapAppId, appId) =>
};
const validateRequiredFields = (payload) => {
if (
!payload.data.user_data.em &&
!payload.data.user_data.ph &&
!payload.data.user_data.madid &&
!(payload.data.user_data.client_ip_address && payload.data.user_data.client_user_agent)
!payload.data[0].user_data.em &&
!payload.data[0].user_data.ph &&
!payload.data[0].user_data.madid &&
!(payload.data[0].user_data.client_ip_address && payload.data[0].user_data.client_user_agent)

Check warning on line 137 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L135-L137

Added lines #L135 - L137 were not covered by tests
) {
throw new InstrumentationError(

Check warning on line 139 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L139

Added line #L139 was not covered by tests
'At least one of email or phone or advertisingId or ip and clientUserAgent is required',
Expand All @@ -158,7 +160,7 @@ const addSpecificEventDetails = (message, payload, actionSource, pixelId, snapAp
};
const getEventConversionType = (message) => {
const channel = get(message, 'channel');
let actionSource = message?.properties?.actionSource;
let actionSource = message?.properties?.action_source;
if (channelMapping[actionSource?.toLowerCase()] || channelMapping[channel?.toLowerCase()]) {
actionSource = actionSource
? channelMapping[actionSource?.toLowerCase()]
Expand All @@ -183,22 +185,35 @@ const trackResponseBuilder = (message, { Config }, mappedEvent) => {
switch (event.toLowerCase()) {
/* Browsing Section */
case 'products_searched':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.PRODUCTS_SEARCHED.name]);
payload = constructPayload(
message,
mappingConfigV3[ConfigCategoryV3.PRODUCTS_SEARCHED.name],
);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
break;
case 'product_list_viewed':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.PRODUCT_LIST_VIEWED.name]);
payload = constructPayload(

Check warning on line 195 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L194-L195

Added lines #L194 - L195 were not covered by tests
message,
mappingConfigV3[ConfigCategoryV3.PRODUCT_LIST_VIEWED.name],
);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
payload.data.custom_data.content_ids = getItemIds(message);
payload.data.custom_data.contents.price = payload.data.custom_data.contents.price || getPriceSum(message);
payload.data.custom_data.contents.price =
payload.data.custom_data.contents.price || getPriceSum(message);
break;

Check warning on line 203 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L199-L203

Added lines #L199 - L203 were not covered by tests
/* Promotions Section */
case 'promotion_viewed':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.PROMOTION_VIEWED.name]);
payload = constructPayload(

Check warning on line 206 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L205-L206

Added lines #L205 - L206 were not covered by tests
message,
mappingConfigV3[ConfigCategoryV3.PROMOTION_VIEWED.name],
);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
break;
case 'promotion_clicked':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.PROMOTION_CLICKED.name]);
payload = constructPayload(

Check warning on line 213 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L210-L213

Added lines #L210 - L213 were not covered by tests
message,
mappingConfigV3[ConfigCategoryV3.PROMOTION_CLICKED.name],
);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
break;

Check warning on line 218 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L217-L218

Added lines #L217 - L218 were not covered by tests
/* Ordering Section */
Expand All @@ -207,10 +222,14 @@ const trackResponseBuilder = (message, { Config }, mappedEvent) => {
payload.data.event_name = eventNameMapping[event.toLowerCase()];
break;
case 'checkout_started':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.CHECKOUT_STARTED.name]);
payload = constructPayload(

Check warning on line 225 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L220-L225

Added lines #L220 - L225 were not covered by tests
message,
mappingConfigV3[ConfigCategoryV3.CHECKOUT_STARTED.name],
);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
payload.data.custom_data.content_ids = getItemIds(message);
payload.data.custom_data.contents.price = payload.data.custom_data.contents.price || getPriceSum(message);
payload.data.custom_data.contents.price =
payload.data.custom_data.contents.price || getPriceSum(message);
break;
case 'payment_info_entered':
payload = constructPayload(

Check warning on line 235 in src/v0/destinations/snapchat_conversion/transformV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/transformV3.js#L229-L235

Added lines #L229 - L235 were not covered by tests
Expand All @@ -223,7 +242,8 @@ const trackResponseBuilder = (message, { Config }, mappedEvent) => {
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.ORDER_COMPLETED.name]);
payload.data.event_name = eventNameMapping[event.toLowerCase()];
payload.data.custom_data.content_ids = getItemIds(message);
payload.data.custom_data.contents.price = payload.data.custom_data.contents.price || getPriceSum(message);
payload.data.custom_data.contents.price =
payload.data.custom_data.contents.price || getPriceSum(message);
break;
case 'product_added':
payload = constructPayload(message, mappingConfigV3[ConfigCategoryV3.PRODUCT_ADDED.name]);
Expand Down Expand Up @@ -254,7 +274,7 @@ const trackResponseBuilder = (message, { Config }, mappedEvent) => {
payload = { ...payload, ...getEventCommonProperties(message) };
payload = populateHashedValues(payload, message);
validateRequiredFields(payload);
payload.data.event_time = getFieldValueFromMessage(message, 'eventTime');
payload.data.event_time = getFieldValueFromMessage(message, 'timestamp');
const eventTime = payload.data.event_time;
if (eventTime) {
const start = moment.unix(moment(eventTime).format('X'));
Expand All @@ -270,7 +290,7 @@ const trackResponseBuilder = (message, { Config }, mappedEvent) => {
payload.data.event_time = msUnixTimestamp(payload.data.event_time)?.toString()?.slice(0, 10);
}

payload.action_source = actionSource;
payload.data.action_source = actionSource;
payload = addSpecificEventDetails(message, payload, actionSource, pixelId, snapAppId, appId);
// adding for deduplication for more than one source
if (enableDeduplication) {
Expand Down
42 changes: 15 additions & 27 deletions src/v0/destinations/snapchat_conversion/utilsV3.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
const { getFieldValueFromMessage } = require('../../util');

// Utility function to extract the click_id from the event_source_url
const extractClickIdFromUrl = (message) => {
/// Utility function to extract the click_id (ScCid) from the event_source_url
const extractClickIdFromUrl = (url) => {
try {
const eventSourceUrl = getFieldValueFromMessage(message, 'pageUrl');

if (!eventSourceUrl) {
throw new Error('URL not found in message');
}

const urlObj = new URL(eventSourceUrl);
const urlObj = new URL(url);
const clickId = urlObj.searchParams.get('ScCid'); // 'ScCid' is the query parameter for click_id

Check warning on line 5 in src/v0/destinations/snapchat_conversion/utilsV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/utilsV3.js#L3-L5

Added lines #L3 - L5 were not covered by tests

return clickId;
return clickId || null; // Return null if not found
} catch (error) {
return null;
return null;

Check warning on line 8 in src/v0/destinations/snapchat_conversion/utilsV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/utilsV3.js#L8

Added line #L8 was not covered by tests
}
};

function getEndpointWithClickId(endpoint, message) {
let clickId;

const scClickId = getFieldValueFromMessage(message, 'scClickId');

if (scClickId) {
clickId = scClickId;
// Check if scClickId is provided directly in the message
if (message.properties.sc_click_id) {
clickId = message.properties.sc_click_id;
} else {

Check warning on line 18 in src/v0/destinations/snapchat_conversion/utilsV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/utilsV3.js#L18

Added line #L18 was not covered by tests
const eventSourceUrl = getFieldValueFromMessage(message, 'eventSourceUrl');
// Check if the eventSourceUrl is provided in message properties or directly
const eventSourceUrl = message.event_source_url || message.properties?.event_source_url;
if (eventSourceUrl) {
clickId = extractClickIdFromUrl(eventSourceUrl);
clickId = extractClickIdFromUrl(eventSourceUrl); // Extract from URL if present

Check warning on line 22 in src/v0/destinations/snapchat_conversion/utilsV3.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/snapchat_conversion/utilsV3.js#L22

Added line #L22 was not covered by tests
}
}

Expand All @@ -38,15 +29,12 @@ function getEndpointWithClickId(endpoint, message) {
}

// Replace {ID} in the endpoint with the found Click ID
const urlObj = new URL(endpoint);
if (urlObj.pathname.includes('{ID}')) {
urlObj.pathname = urlObj.pathname.replace('{ID}', clickId);
} else {
throw new Error('Endpoint URL does not contain {ID} placeholder');
}
let urlObj = decodeURIComponent(endpoint);

urlObj = urlObj.replace('{ID}', clickId);

// Return the updated endpoint URL with the scClickId replaced
return urlObj.toString();
return urlObj;
}

module.exports = { getEndpointWithClickId, extractClickIdFromUrl };
Loading

0 comments on commit ff47d41

Please sign in to comment.