Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: snapchat conversion introduce missing fields #2704

Merged
merged 11 commits into from
Oct 11, 2023
45 changes: 36 additions & 9 deletions src/v0/destinations/snapchat_conversion/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,31 @@ function buildResponse(apiKey, payload) {
* @param {*} message
* @returns updatedPayload
*/
function populateHashedValues(payload, message) {
const updatedPayload = payload;
const populateHashedValues = (payload, message) => {
const email = getFieldValueFromMessage(message, 'email');
const phone = getNormalizedPhoneNumber(message);
const ip = message.context?.ip || message.request_ip;

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');
const updatedPayload = {
...payload,
hashed_first_name_sha: firstName
? getHashedValue(firstName.toString().toLowerCase().trim())
: undefined,
hashed_middle_name_sha: middleName
? getHashedValue(middleName.toString().toLowerCase().trim())
: undefined,
hashed_last_name_sha: lastName
? getHashedValue(lastName.toString().toLowerCase().trim())
: undefined,
hashed_city_sha: city ? getHashedValue(city.toString().toLowerCase().trim()) : undefined,
hashed_zip: zip ? getHashedValue(zip.toString().toLowerCase().trim()) : undefined,
hashed_state_sha: state ? getHashedValue(state.toString().toLowerCase().trim()) : undefined,
};
if (email) {
updatedPayload.hashed_email = getHashedValue(email.toString().toLowerCase().trim());
}
Expand All @@ -84,10 +103,10 @@ function populateHashedValues(payload, message) {
);
}
return updatedPayload;
}
};

// Returns the response for the track event after constructing the payload and setting necessary fields
function trackResponseBuilder(message, { Config }, mappedEvent) {
const trackResponseBuilder = (message, { Config }, mappedEvent) => {
let payload = {};
const event = mappedEvent.trim().replace(/\s+/g, '_');

Expand Down Expand Up @@ -190,11 +209,19 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
} else {
throw new InstrumentationError(`Event ${event} doesn't match with Snapchat Events!`);
}

payload.description = get(message, 'properties.description');
if (Array.isArray(message.properties?.brands)) {
payload.brands = get(message, 'properties.brands');
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
}
payload.customer_status = get(message, 'properties.customer_status');
payload.uuid_c1 = get(message, 'properties.uuid_c1');
payload.level = get(message, 'properties.level');
payload.click_id = get(message, 'properties.click_id');
payload.event_tag = get(message, 'properties.event_tag');
payload = populateHashedValues(payload, message);

payload.country = getFieldValueFromMessage(message, 'country'); // Must be provided as a two letter ISO 3166 alpha-2 country code.
payload.region = getFieldValueFromMessage(message, 'region');
payload.user_agent = message.context?.userAgent?.toString().toLowerCase();
payload = populateHashedValues(payload, message);

if (
!payload.hashed_email &&
Expand Down Expand Up @@ -245,7 +272,7 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
// build response
const response = buildResponse(apiKey, payload);
return response;
}
};

// Checks if there are any mapping events for the track event and returns them
function eventMappingHandler(message, destination) {
Expand Down
46 changes: 32 additions & 14 deletions test/__tests__/data/snapchat_conversion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"description": "Test case for Prodcuts Searched event for conversion type offline",
"description": "Test case for Products Searched event for conversion type offline",
"input": {
"message": {
"messageId": "ec5481b6-a926-4d2e-b293-0b3a77c4d3be",
Expand All @@ -9,7 +9,12 @@
"context": {
"traits": {
"email": "[email protected]",
"phone": "+91 2111111 "
"phone": "+91 2111111 ",
"firstName": "john",
"middleName": "victor",
"lastName": "doe",
"city": "some_city",
"state": "some_state"
},
"app": {
"build": "1.0.0",
Expand All @@ -26,7 +31,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand All @@ -52,7 +56,9 @@
"properties": {
"query": "t-shirts",
"event_conversion_type": "web",
"number_items": 4
"number_items": 4,
"click_id": "some_click_id",
"description": "Products Searched event for conversion type offline"
},
"integrations": {
"All": true
Expand Down Expand Up @@ -88,7 +94,13 @@
"timestamp": "1650625078",
"event_conversion_type": "OFFLINE",
"pixel_id": "dummyPixelId",
"number_items": 4
"number_items": 4,
"click_id": "some_click_id",
"description": "Products Searched event for conversion type offline",
"hashed_first_name_sha": "96d9632f363564cc3032521409cf22a852f2032eec099ed5967c0d000cec607a",
"hashed_last_name_sha": "799ef92a11af918e3fb741df42934f3b568ed2d93ac1df74f1b8d41a27932a6f",
"hashed_state_sha": "6db488fc98e30afdf67a05a6da916805b02891ce58f03970c6deff79129c5f1c",
"hashed_middle_name_sha": "99bde068af2d49ed7fc8b8fa79abe13a6059e0db320bb73459fd96624bb4b33f"
},
"JSON_ARRAY": {},
"XML": {},
Expand All @@ -107,7 +119,10 @@
"context": {
"traits": {
"email": "[email protected]",
"phone": "+91 2111111 "
"phone": "+91 2111111 ",
"country": "IN",
"zicode": "Sxp-12345",
"region": "some_region"
},
"app": {
"build": "1.0.0",
Expand Down Expand Up @@ -654,7 +669,10 @@
"context": {
"traits": {
"email": "[email protected]",
"phone": "+91 2111111 "
"phone": "+91 2111111 ",
"country": "IN",
"zipcode": "Sxp-12345",
"region": "some_region"
},
"app": {
"build": "1.0.0",
Expand Down Expand Up @@ -735,6 +753,9 @@
"event_type": "VIEW_CONTENT",
"item_ids": ["123", "123"],
"price": "56",
"country": "IN",
"hashed_zip": "cbb2704f5b334a0cec32e5463d1fd9355f6ef73987bfe0ebb8389b7617452152",
"region": "some_region",
"hashed_email": "73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2",
"hashed_phone_number": "bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492",
"hashed_mobile_ad_id": "f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2",
Expand Down Expand Up @@ -912,14 +933,15 @@
"type": "track",
"event": "Order Completed",
"properties": {
"brands": ["brand01", "brand02"],
"products": [
{
"product_id": "123",
"price": "14",
"quantity": 1
},
{
"product_id": "123",
"product_id": "124",
"price": 14,
"quantity": 3
}
Expand Down Expand Up @@ -952,7 +974,8 @@
"body": {
"JSON": {
"event_type": "PURCHASE",
"item_ids": ["123", "123"],
"item_ids": ["123", "124"],
"brands": ["brand01", "brand02"],
"price": "56",
"hashed_email": "73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2",
"hashed_phone_number": "bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492",
Expand Down Expand Up @@ -2025,7 +2048,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand Down Expand Up @@ -2227,7 +2249,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand Down Expand Up @@ -2331,7 +2352,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand Down Expand Up @@ -2403,7 +2423,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand Down Expand Up @@ -2501,7 +2520,6 @@
"type": "ios",
"attTrackingStatus": 3
},

"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"body": {
"JSON": {},
"JSON_ARRAY": {
"batch": "[{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"},{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"},{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"}]"
"batch": "[{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"},{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"},{\"search_string\":\"t-shirts\",\"event_type\":\"SEARCH\",\"user_agent\":\"mozilla/5.0 (macintosh; intel mac os x 10_15_2) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.88 safari/537.36\",\"hashed_email\":\"73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2\",\"hashed_phone_number\":\"bc77d64d7045fe44795ed926df37231a0cfb6ec6b74588c512790e9f143cc492\",\"hashed_idfv\":\"54bd0b26a3d39dad90f5149db49b9fd9ba885f8e35d1d94cae69273f5e657b9f\",\"hashed_mobile_ad_id\":\"f9779d734aaee50f16ee0011260bae7048f1d9a128c62b6a661077875701edd2\",\"timestamp\":\"1650625078\",\"event_conversion_type\":\"OFFLINE\",\"pixel_id\":\"dummyPixelId\"}]"
},
"XML": {},
"FORM": {}
Expand Down
Loading