Skip to content

Commit

Permalink
fix: edits for exclusion keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Nov 13, 2023
1 parent 55715dd commit 682fc95
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/v0/destinations/mp/data/MPIdentifyConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
},
{
"destKey": "$first_name",
"sourceKeys": "firstName",
"sourceKeys": [
"firstName",
"firstname",
"first_name",
"traits.firstName",
"traits.firstname",
"traits.first_name",
"context.traits.firstName",
"context.traits.firstname",
"context.traits.first_name"
],
"required": false,
"sourceFromGenericMap": true
},
Expand Down
26 changes: 20 additions & 6 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const { CommonUtils } = require('../../../util/common');

// ref: https://help.mixpanel.com/hc/en-us/articles/115004613766-Default-Properties-Collected-by-Mixpanel
const mPEventPropertiesConfigJson = mappingConfig[ConfigCategory.EVENT_PROPERTIES.name];
const mPIdentifyConfigJson = mappingConfig[ConfigCategory.IDENTIFY.name];

const setImportCredentials = (destConfig) => {
const endpoint =
Expand Down Expand Up @@ -234,9 +235,10 @@ const processTrack = (message, destination) => {
return returnValue;
};

function trimTraits(traits, setOnceProperties) {
function trimTraits(traits, contextTraits, setOnceProperties) {
// Create a copy of the original traits object
const traitsCopy = { ...traits };
const contextTraitsCopy = { ...contextTraits };

// Initialize setOnce object
const setOnce = {};
Expand All @@ -247,11 +249,20 @@ function trimTraits(traits, setOnceProperties) {
setOnce[property] = traitsCopy[property];
delete traitsCopy[property];
}
if (contextTraitsCopy.hasOwnProperty(property)) {
if(!setOnce.hasOwnProperty(property)) {
setOnce[property] = contextTraitsCopy[property];
}
delete contextTraitsCopy[property];
}
});

const sentOnceTransform = constructPayload(setOnce, mPIdentifyConfigJson);

return {
traits: traitsCopy,
setOnce,
contextTraits: contextTraitsCopy,
sentOnceTransform,
};
}

Expand All @@ -268,26 +279,29 @@ const processIdentifyEvents = async (message, type, destination) => {
const messageClone = { ...message };
let seggregatedTraits = {};
const returnValue = [];
let setOnceProperties = [];

// making payload for set_once properties
if (
destination.Config.setOnceProperties &&
Object.keys(destination.Config.setOnceProperties).length > 0
) {
const setOnceProperties = parseConfigArray(destination.Config.setOnceProperties, 'property');
setOnceProperties = parseConfigArray(destination.Config.setOnceProperties, 'property');
seggregatedTraits = trimTraits(
getFieldValueFromMessage(messageClone, 'traits'),
messageClone.traits,
messageClone.context.traits,
setOnceProperties,
);
messageClone.traits = seggregatedTraits.traits;
messageClone.context.traits = seggregatedTraits.contextTraits;
returnValue.push(
createSetOnceResponse(messageClone, type, destination, seggregatedTraits.setOnce),
);
}

// Creating the user profile
// https://developer.mixpanel.com/reference/profile-set
returnValue.push(createIdentifyResponse(messageClone, type, destination, responseBuilderSimple));
returnValue.push(createIdentifyResponse(messageClone, type, destination, responseBuilderSimple, setOnceProperties));

if (
destination.Config?.identityMergeApi !== 'simplified' &&
Expand Down Expand Up @@ -496,7 +510,7 @@ const processRouterDest = async (inputs, reqMetadata) => {
destination: event.destination,
};
}

// console.log('event', JSON.stringify(event));
let processedEvents = await process(event);
processedEvents = CommonUtils.toArray(processedEvents);
return processedEvents.map((res) => ({
Expand Down
13 changes: 9 additions & 4 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const mPProfileIosConfigJson = mappingConfig[ConfigCategory.PROFILE_IOS.name];
* @param {*} useNewMapping a variable to support backward compatibility
* @returns
*/
const getTransformedJSON = (message, mappingJson, useNewMapping) => {
const getTransformedJSON = (message, mappingJson, useNewMapping, setOnceProperties) => {
let rawPayload = constructPayload(message, mappingJson);
if (
isDefined(rawPayload.$geo_source) &&
Expand All @@ -47,11 +47,16 @@ const getTransformedJSON = (message, mappingJson, useNewMapping) => {
set(rawPayload, '$name', getFullName(message));
}

let newExclusionList = [...MP_IDENTIFY_EXCLUSION_LIST];
if (setOnceProperties.length > 0) {
newExclusionList = [...newExclusionList, ...setOnceProperties];
}

rawPayload = extractCustomFields(
message,
rawPayload,
['traits', 'context.traits'],
MP_IDENTIFY_EXCLUSION_LIST,
newExclusionList,
);

/*
Expand Down Expand Up @@ -99,11 +104,11 @@ const getTransformedJSON = (message, mappingJson, useNewMapping) => {
* @param {*} responseBuilderSimple function to generate response
* @returns
*/
const createIdentifyResponse = (message, type, destination, responseBuilderSimple) => {
const createIdentifyResponse = (message, type, destination, responseBuilderSimple, setOnceProperties) => {
// this variable is used for supporting backward compatibility
const { useNewMapping, token } = destination.Config;
// user payload created
const properties = getTransformedJSON(message, mPIdentifyConfigJson, useNewMapping);
const properties = getTransformedJSON(message, mPIdentifyConfigJson, useNewMapping, setOnceProperties);

const payload = {
$set: properties,
Expand Down
40 changes: 39 additions & 1 deletion test/integrations/destinations/mp/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,42 @@ const sampleDestination = {
Transformations: [],
};

export { sampleDestination, defaultMockFns };
const destinationWithSetOnceProperty = {
ID: '2Xz4szkZeBMJRgO5TfvLclsxkxz',
Name: 'Mixpanel Dev',
DestinationDefinition: {
ID: '2WcAA63MqbQVSIrgPR0mXuxF4ut',
Name: 'MP',
DisplayName: 'Mixpanel',
ResponseRules: {}
},
Config: {
apiSecret: 'dummySecret',
dataResidency: 'us',
eventDelivery: false,
eventDeliveryTS: 1699848284554,
identityMergeApi: 'simplified',
setOnceProperties: [
{
property: 'nationality'
}
],
superProperties: [
{
property: 'random'
}
],
token: 'dummyToken',
useNativeSDK: false,
useNewMapping: false,
userDeletionApi: 'engage',
whitelistedEvents: []
},
Enabled: true,
WorkspaceID: 123345,
Transformations: [],
IsProcessorEnabled: true,
RevisionID: 12345
}

export { sampleDestination, defaultMockFns, destinationWithSetOnceProperty };
Loading

0 comments on commit 682fc95

Please sign in to comment.