Skip to content

Commit

Permalink
feat: refactor customerio identify call for v2 api support
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwal-ab committed Dec 7, 2023
1 parent 4b260e4 commit a286a55
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/v0/destinations/customerio/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ const identifyResponseBuilder = (userId, message) => {
if (!id) {
throw new InstrumentationError('userId or email is not present');
}
rawPayload.identifiers = { id };

const attributes = {};

// populate speced traits
const identityTrailts = getFieldValueFromMessage(message, 'traits') || {};
populateSpecedTraits(rawPayload, message);
populateSpecedTraits(attributes, message);

if (Object.keys(identityTrailts).length > 0) {
const traits = Object.keys(identityTrailts);
Expand All @@ -123,31 +126,22 @@ const identifyResponseBuilder = (userId, message) => {
trait !== 'anonymousId'
) {
const dotEscapedTrait = trait.replace('.', '\\.');
set(rawPayload, dotEscapedTrait, get(message, `${pathToTraits}.${trait}`));
set(attributes, dotEscapedTrait, get(message, `${pathToTraits}.${trait}`));
}
});
}

// populate user_properties (DEPRECATED)
if (message.user_properties) {
const userProps = Object.keys(message.user_properties);
userProps.forEach((prop) => {
const val = get(message, `user_properties.${prop}`);
set(rawPayload, prop, val);
});
}

// make user creation time
const createAt = getFieldValueFromMessage(message, 'createdAtOnly');
// set the created_at field if traits.createAt or context.traits.createAt is passed
if (createAt) {
set(rawPayload, 'created_at', Math.floor(new Date(createAt).getTime() / 1000));
set(attributes, 'created_at', Math.floor(new Date(createAt).getTime() / 1000));
}

// Impportant for historical import
if (getFieldValueFromMessage(message, 'historicalTimestamp')) {
set(
rawPayload,
attributes,
'_timestamp',
Math.floor(
new Date(getFieldValueFromMessage(message, 'historicalTimestamp')).getTime() / 1000,
Expand All @@ -156,10 +150,13 @@ const identifyResponseBuilder = (userId, message) => {
}
// anonymous_id needs to be sent for identify calls to merge with any previous anon track calls
if (message?.anonymousId) {
set(rawPayload, 'anonymous_id', message.anonymousId);
set(attributes, 'anonymous_id', message.anonymousId);
}
rawPayload.attributes = attributes;
const endpoint = IDENTITY_ENDPOINT.replace(':id', id);
const requestConfig = defaultPutRequestConfig;
rawPayload.type = 'person';
rawPayload.action = 'identify';

return { rawPayload, endpoint, requestConfig };
};
Expand Down Expand Up @@ -188,6 +185,8 @@ const aliasResponseBuilder = (message, userId) => {
[prev_cioProperty]: message.previousId,
},
};
rawPayload.type = 'person';
rawPayload.action = 'merge';

return { rawPayload, endpoint, requestConfig };
};
Expand Down Expand Up @@ -238,6 +237,8 @@ const defaultResponseBuilder = (message, evName, userId, evType, destination, me
}
endpoint = DEVICE_DELETE_ENDPOINT.replace(':id', id).replace(':device_id', token);
requestConfig = defaultDeleteRequestConfig;
rawPayload.action = 'delete_device';
rawPayload.type = 'person';
return { rawPayload, endpoint, requestConfig };
}

Expand Down Expand Up @@ -279,6 +280,12 @@ const defaultResponseBuilder = (message, evName, userId, evType, destination, me
isDeviceRelatedEvent && token
? DEVICE_REGISTER_ENDPOINT.replace(':id', id)
: USER_EVENT_ENDPOINT.replace(':id', id);
if (endpoint.includes(DEVICE_REGISTER_ENDPOINT)) {
rawPayload.action = 'add_device';
} else {
rawPayload.action = 'event';
}
rawPayload.type = 'person';
} else {
endpoint = ANON_EVENT_ENDPOINT;
// CustomerIO supports 100byte of event name for anonymous users
Expand Down

0 comments on commit a286a55

Please sign in to comment.