Skip to content

Commit

Permalink
fix: remove hashing on twclid for twitter ads (#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai authored and Sanjay-Veernala committed Sep 19, 2023
1 parent f7d0441 commit bc01bdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
76 changes: 37 additions & 39 deletions src/v0/destinations/twitter_ads/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const {
simpleProcessRouterDest,
} = require('../../util');
const { EventType } = require('../../../constants');
const {
ConfigCategories,
mappingConfig,
BASE_URL
} = require('./config');
const { ConfigCategories, mappingConfig, BASE_URL } = require('./config');

const { InstrumentationError, OAuthSecretError, ConfigurationError } = require('../../util/errorTypes');
const {
InstrumentationError,
OAuthSecretError,
ConfigurationError,
} = require('../../util/errorTypes');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { getAuthHeaderForRequest } = require("./util");
const { getAuthHeaderForRequest } = require('./util');

const getOAuthFields = ({ secret }) => {
if (!secret) {
Expand All @@ -27,7 +27,7 @@ const getOAuthFields = ({ secret }) => {
consumerKey: secret.consumerKey,
consumerSecret: secret.consumerSecret,
accessToken: secret.accessToken,
accessTokenSecret: secret.accessTokenSecret
accessTokenSecret: secret.accessTokenSecret,
};
return oAuthObject;
};
Expand All @@ -42,7 +42,7 @@ function buildResponse(message, requestJson, metadata, endpointUrl) {
const request = {
url: response.endpoint,
method: response.method,
body: response.body.JSON
body: response.body.JSON,
};

const oAuthObject = getOAuthFields(metadata);
Expand All @@ -60,17 +60,20 @@ function prepareUrl(message, destination) {
}

function populateEventId(event, requestJson, destination) {

const eventNameToIdMappings = destination.Config.twitterAdsEventNames;
let eventId = "";
let eventId = '';

if (eventNameToIdMappings) {
const eventObj = eventNameToIdMappings.find(obj => obj.rudderEventName?.trim().toLowerCase() === event?.toString().toLowerCase());
const eventObj = eventNameToIdMappings.find(
(obj) => obj.rudderEventName?.trim().toLowerCase() === event?.toString().toLowerCase(),
);
eventId = eventObj?.twitterEventId;
}

if(!eventId) {
throw new ConfigurationError(`[TWITTER ADS]: Event - '${event}' do not have a corresponding eventId in configuration. Aborting`);
if (!eventId) {
throw new ConfigurationError(
`[TWITTER ADS]: Event - '${event}' do not have a corresponding eventId in configuration. Aborting`,
);
}

return eventId;
Expand All @@ -79,14 +82,16 @@ function populateEventId(event, requestJson, destination) {
function populateContents(requestJson) {
const reqJson = { ...requestJson };
if (reqJson.contents) {
const transformedContents = requestJson.contents.map(obj => ({
...(obj.id && { content_id: obj.id }),
...(obj.groupId && { content_group_id: obj.groupId }),
...(obj.name && { content_name: obj.name }),
...(obj.price && { content_price: parseFloat(obj.price) }),
...(obj.type && { content_type: obj.type }),
...(obj.quantity && { num_items: parseInt(obj.quantity, 10) })
})).filter(tfObj => Object.keys(tfObj).length > 0);
const transformedContents = requestJson.contents
.map((obj) => ({
...(obj.id && { content_id: obj.id }),
...(obj.groupId && { content_group_id: obj.groupId }),
...(obj.name && { content_name: obj.name }),
...(obj.price && { content_price: parseFloat(obj.price) }),
...(obj.type && { content_type: obj.type }),
...(obj.quantity && { num_items: parseInt(obj.quantity, 10) }),
}))
.filter((tfObj) => Object.keys(tfObj).length > 0);
if (transformedContents.length > 0) {
reqJson.contents = transformedContents;
}
Expand All @@ -96,33 +101,34 @@ function populateContents(requestJson) {

// process track call
function processTrack(message, metadata, destination) {

let requestJson = constructPayload(message, mappingConfig[ConfigCategories.TRACK.name]);

requestJson.event_id = requestJson.event_id || populateEventId(message.event, requestJson, destination);
requestJson.event_id =
requestJson.event_id || populateEventId(message.event, requestJson, destination);

requestJson.conversion_time = isDefinedAndNotNull(requestJson.conversion_time)
? requestJson.conversion_time : message.timestamp;
? requestJson.conversion_time
: message.timestamp;

const identifiers = [];

if (message.properties.email) {
let email = message.properties.email.trim();
if (email) {
email = email.toLowerCase();
identifiers.push({hashed_email: sha256(email)})
identifiers.push({ hashed_email: sha256(email) });
}
}

if (message.properties.phone) {
const phone = message.properties.phone.trim();
if (phone) {
identifiers.push({hashed_phone_number: sha256(phone)})
identifiers.push({ hashed_phone_number: sha256(phone) });
}
}

if (message.properties.twclid) {
identifiers.push({twclid: sha256(message.properties.twclid)});
identifiers.push({ twclid: message.properties.twclid });
}

requestJson = populateContents(requestJson);
Expand All @@ -131,33 +137,26 @@ function processTrack(message, metadata, destination) {

const endpointUrl = prepareUrl(message, destination);

return buildResponse(
message,
requestJson,
metadata,
endpointUrl
);
return buildResponse(message, requestJson, metadata, endpointUrl);
}

function validateRequest(message) {

const { properties } = message;

if (!properties) {
throw new InstrumentationError(
'[TWITTER ADS]: properties must be present in event. Aborting message',
'[TWITTER ADS]: properties must be present in event. Aborting message',
);
}

if (!properties.email && !properties.phone && !properties.twclid) {
throw new InstrumentationError(
'[TWITTER ADS]: one of twclid, phone or email must be present in properties.',
'[TWITTER ADS]: one of twclid, phone or email must be present in properties.',
);
}
}

function process(event) {

const { message, metadata, destination } = event;

validateRequest(message);
Expand All @@ -169,7 +168,6 @@ function process(event) {
}

throw new InstrumentationError(`Message type ${messageType} not supported`);

}

const processRouterDest = async (inputs, reqMetadata) => {
Expand Down
6 changes: 3 additions & 3 deletions test/__tests__/data/twitter_ads.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"hashed_phone_number": "b308962b96b40cce7981493a372db9478edae79f83c2d8ca6cd15a39566f8c56"
},
{
"twclid": "18beb4813723e788a1d79bcbf80802538ec813aa19ded2e9c21cbf08bed6bee3"
"twclid": "543"
}
]
}
Expand Down Expand Up @@ -360,7 +360,7 @@
"hashed_phone_number": "b308962b96b40cce7981493a372db9478edae79f83c2d8ca6cd15a39566f8c56"
},
{
"twclid": "18beb4813723e788a1d79bcbf80802538ec813aa19ded2e9c21cbf08bed6bee3"
"twclid": "543"
}
]
}
Expand Down Expand Up @@ -692,7 +692,7 @@
"hashed_phone_number": "b308962b96b40cce7981493a372db9478edae79f83c2d8ca6cd15a39566f8c56"
},
{
"twclid": "18beb4813723e788a1d79bcbf80802538ec813aa19ded2e9c21cbf08bed6bee3"
"twclid": "543"
}
]
}
Expand Down

0 comments on commit bc01bdf

Please sign in to comment.