Skip to content

Commit

Permalink
fix: correct mapping for properties object for identify call (#2283)
Browse files Browse the repository at this point in the history
* fix: correct mapping for properties object for identify call

* feat: enable use of enforce email as primary identifier toggle to track call

* chore: refactor code
  • Loading branch information
ujjwal-ab committed Jun 20, 2023
1 parent 607bb0e commit d046cd2
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 29 deletions.
14 changes: 9 additions & 5 deletions src/v0/destinations/klaviyo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const identifyRequestHandler = async (message, category, destination) => {

propertyPayload = removeUndefinedAndNullValues(propertyPayload);
if (enforceEmailAsPrimary) {
if (!propertyPayload.email && !propertyPayload.phone_number) {
throw new InstrumentationError('None of email and phone are present in the payload');
}
delete propertyPayload.external_id;
customPropertyPayload = {
...customPropertyPayload,
Expand All @@ -78,8 +81,10 @@ const identifyRequestHandler = async (message, category, destination) => {
}
const data = {
type: 'profile',
attributes: propertyPayload,
properties: removeUndefinedAndNullValues(customPropertyPayload),
attributes: {
...propertyPayload,
properties: removeUndefinedAndNullValues(customPropertyPayload),
},
};
const payload = {
data: removeUndefinedAndNullValues(data),
Expand Down Expand Up @@ -125,8 +130,6 @@ const trackRequestHandler = (message, category, destination) => {
const eventName = eventNameMapping[event];
const eventMap = jsonNameMapping[eventName];
attributes.metric = { name: eventName };
// using identify to create customer properties
attributes.profile = createCustomerProperties(message);
const categ = CONFIG_CATEGORIES[eventMap];
attributes.properties = constructPayload(message.properties, MAPPING_CONFIG[categ.name]);
attributes.properties = {
Expand Down Expand Up @@ -186,8 +189,9 @@ const trackRequestHandler = (message, category, destination) => {
...attributes.properties,
...populateCustomFieldsFromTraits(message),
};
attributes.profile = createCustomerProperties(message);
}
// Map user properties to profile object
attributes.profile = createCustomerProperties(message, destination.Config);
if (message.timestamp) {
attributes.time = message.timestamp;
}
Expand Down
17 changes: 14 additions & 3 deletions src/v0/destinations/klaviyo/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {

const { BASE_ENDPOINT, MAPPING_CONFIG, CONFIG_CATEGORIES, MAX_BATCH_SIZE } = require('./config');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { NetworkError } = require('../../util/errorTypes');
const { NetworkError, InstrumentationError } = require('../../util/errorTypes');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const tags = require('../../util/tags');
const { handleHttpRequest } = require('../../../adapters/network');
Expand Down Expand Up @@ -135,12 +135,23 @@ const subscribeUserToList = (message, traitsInfo, destination) => {
};

// This function is used for creating and returning customer properties using mapping json
const createCustomerProperties = (message) => {
const createCustomerProperties = (message, Config) => {
const { enforceEmailAsPrimary } = Config;
let customerProperties = constructPayload(
message,
MAPPING_CONFIG[CONFIG_CATEGORIES.PROFILE.name],
);
customerProperties.$id = getFieldValueFromMessage(message, 'userId');
if (!enforceEmailAsPrimary) {
customerProperties.$id = getFieldValueFromMessage(message, 'userId');
} else {
if (!customerProperties.$email && !customerProperties.$phone_number) {
throw new InstrumentationError('None of email and phone are present in the payload');
}
customerProperties = {
...customerProperties,
_id: getFieldValueFromMessage(message, 'userId'),
};
}
customerProperties = removeUndefinedAndNullValues(customerProperties);
return customerProperties;
};
Expand Down
Loading

0 comments on commit d046cd2

Please sign in to comment.