Skip to content

Commit

Permalink
feat(mixpanel): add incremental properties support (#2550)
Browse files Browse the repository at this point in the history
* feat(mixpanel): add incremental properties support

* docs: add comment

* test: update testcase
  • Loading branch information
Gauravudia authored and Sanjay-Veernala committed Sep 19, 2023
1 parent 9122dd2 commit 46c4ccc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
39 changes: 39 additions & 0 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ const processRevenueEvents = (message, destination, revenueValue) => {
return responseBuilderSimple(payload, message, 'revenue', destination.Config);
};

/**
* This function is used to process the incremental properties
* ref :- https://developer.mixpanel.com/reference/profile-numerical-add
* @param {*} message
* @param {*} destination
* @param {*} propIncrements
* @returns
*/
const processIncrementalProperties = (message, destination, propIncrements) => {
const payload = {
$add: {},
$token: destination.Config.token,
$distinct_id: message.userId || message.anonymousId,
};

if (destination?.Config.identityMergeApi === 'simplified') {
payload.$distinct_id = message.userId || `$device:${message.anonymousId}`;
}

Object.keys(message.properties).forEach((prop) => {
const value = message.properties[prop];
if (value && propIncrements.includes(prop)) {
payload.$add[prop] = value;
}
});

return Object.keys(payload.$add).length > 0
? responseBuilderSimple(payload, message, 'incremental_properties', destination.Config)
: null;
};

const getEventValueForTrackEvent = (message, destination) => {
const mappedProperties = constructPayload(message, mPEventPropertiesConfigJson);
// This is to conform with SDKs sending timestamp component with messageId
Expand Down Expand Up @@ -178,6 +209,14 @@ const processTrack = (message, destination) => {
if (revenue) {
returnValue.push(processRevenueEvents(message, destination, revenue));
}

if (Array.isArray(destination.Config.propIncrements)) {
const propIncrements = destination.Config.propIncrements.map((item) => item.property);
const response = processIncrementalProperties(message, destination, propIncrements);
if (response) {
returnValue.push(response);
}
}
returnValue.push(getEventValueForTrackEvent(message, destination));
return returnValue;
};
Expand Down
5 changes: 1 addition & 4 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const {
combineBatchRequestsWithSameJobIds,
combineBatchRequestsWithSameJobIds2,
} = require('./util');
const { combineBatchRequestsWithSameJobIds } = require('./util');

const destinationMock = {
Config: {
Expand Down
25 changes: 22 additions & 3 deletions test/__tests__/data/mp_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,18 @@
"apiKey": "dummyApiKey",
"token": "dummyApiKey",
"prefixProperties": true,
"useNativeSDK": false
"useNativeSDK": false,
"propIncrements": [
{
"property": "counter"
},
{
"property": "item_purchased"
},
{
"property": "number_of_logins"
}
]
},
"DestinationDefinition": {
"DisplayName": "Kiss Metrics",
Expand Down Expand Up @@ -493,7 +504,10 @@
"originalTimestamp": "2020-01-24T06:29:02.364Z",
"properties": {
"currency": "USD",
"revenue": 45.89
"revenue": 45.89,
"counter": 1,
"item_purchased": "2",
"number_of_logins": ""
},
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"request_ip": "[::1]:53710",
Expand Down Expand Up @@ -582,7 +596,12 @@
"apiKey": "dummyApiKey",
"token": "dummyApiKey",
"prefixProperties": true,
"useNativeSDK": false
"useNativeSDK": false,
"propIncrements": [
{
"property": ""
}
]
},
"DestinationDefinition": {
"DisplayName": "Kiss Metrics",
Expand Down
20 changes: 19 additions & 1 deletion test/__tests__/data/mp_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@
"files": {},
"userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca"
},
{
"version": "1",
"type": "REST",
"method": "POST",
"endpoint": "https://api.mixpanel.com/engage/",
"headers": {},
"params": {},
"body": {
"JSON": {},
"JSON_ARRAY": {
"batch": "[{\"$add\":{\"counter\":1,\"item_purchased\":\"2\"},\"$token\":\"dummyApiKey\",\"$distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\"}]"
},
"XML": {},
"FORM": {}
},
"files": {},
"userId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca"
},
{
"version": "1",
"type": "REST",
Expand All @@ -138,7 +156,7 @@
"body": {
"JSON": {},
"JSON_ARRAY": {
"batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"[email protected]\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]"
"batch": "[{\"event\":\"test revenue MIXPANEL\",\"properties\":{\"currency\":\"USD\",\"revenue\":45.89,\"counter\":1,\"item_purchased\":\"2\",\"number_of_logins\":\"\",\"city\":\"Disney\",\"country\":\"USA\",\"email\":\"[email protected]\",\"firstName\":\"Mickey\",\"ip\":\"0.0.0.0\",\"$current_url\":\"https://docs.rudderstack.com/destinations/mixpanel\",\"$screen_dpi\":2,\"mp_lib\":\"RudderLabs JavaScript SDK\",\"$app_build_number\":\"1.0.0\",\"$app_version_string\":\"1.0.5\",\"$insert_id\":\"a6a0ad5a-bd26-4f19-8f75-38484e580fc7\",\"token\":\"dummyApiKey\",\"distinct_id\":\"e6ab2c5e-2cda-44a9-a962-e2f67df78bca\",\"time\":1579847342,\"$browser\":\"Chrome\",\"$browser_version\":\"79.0.3945.117\"}}]"
},
"XML": {},
"FORM": {}
Expand Down

0 comments on commit 46c4ccc

Please sign in to comment.