Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mixpanel): add support of simplified api #2042

Merged
merged 7 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const setImportCredentials = (destConfig) => {
const responseBuilderSimple = (parameters, message, eventType, destConfig) => {
const response = defaultRequestConfig();
response.method = defaultPostRequestConfig.requestMethod;
response.userId = message.anonymousId || message.userId;
response.userId = message.userId || message.anonymousId;
const encodedData = Buffer.from(JSON.stringify(removeUndefinedValues(parameters))).toString(
'base64',
);
Expand Down Expand Up @@ -133,7 +133,7 @@ const getEventValueForTrackEvent = (message, destination) => {
mappedProperties.$insert_id = mappedProperties.$insert_id.slice(-36);
}
const unixTimestamp = toUnixTimestamp(message.timestamp);
const properties = {
let properties = {
...message.properties,
...get(message, 'context.traits'),
...mappedProperties,
Expand All @@ -142,6 +142,15 @@ const getEventValueForTrackEvent = (message, destination) => {
time: unixTimestamp,
};

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

if (message.channel === 'web' && message.context?.userAgent) {
const browser = getBrowserInfo(message.context.userAgent);
properties.$browser = browser.name;
Expand Down Expand Up @@ -175,10 +184,15 @@ const processIdentifyEvents = async (message, type, destination) => {
// https://developer.mixpanel.com/reference/profile-set
returnValue = createIdentifyResponse(message, type, destination, responseBuilderSimple);

// If userId and anonymousId both are present and required credentials for /import
// endpoint are available we are creating the merging response below
// https://developer.mixpanel.com/reference/identity-merge
if (message.userId && message.anonymousId && isImportAuthCredentialsAvailable(destination)) {
if (
destination.Config?.identityMergeApi !== 'simplified' &&
message.userId &&
message.anonymousId &&
isImportAuthCredentialsAvailable(destination)
) {
// If userId and anonymousId both are present and required credentials for /import
// endpoint are available we are creating the merging response below
// https://developer.mixpanel.com/reference/identity-merge
const createUserResponse = await createUser(returnValue);
const status = createUserResponse?.status || 400;
if (!isHttpStatusSuccess(status)) {
Expand Down Expand Up @@ -333,6 +347,11 @@ const processSingleMessage = async (message, destination) => {
case EventType.IDENTIFY:
return processIdentifyEvents(message, message.type, destination);
case EventType.ALIAS:
if (destination.Config?.identityMergeApi === 'simplified') {
throw new InstrumentationError(
`Event type '${EventType.ALIAS}' is not supported when 'Simplified ID merge' api is selected in webapp`,
);
}
return processAliasEvents(message, message.type, destination);
case EventType.GROUP:
return processGroupEvents(message, message.type, destination);
Expand Down
5 changes: 5 additions & 0 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ const createIdentifyResponse = (message, type, destination, responseBuilderSimpl
$ip: get(message, 'context.ip') || message.request_ip,
$time: toUnixTimestamp(message.timestamp),
};

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

if (message.context?.active === false) {
parameters.$ignore_time = true;
}
Expand Down
281 changes: 281 additions & 0 deletions test/__tests__/data/mp_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -3556,5 +3556,286 @@
"userId": "hjikl",
"groupId": "testGroupId"
}
},
{
"description": "Track: set device id and user id when simplified id merge api is selected",
"destination": {
"Config": {
"apiKey": "apiKey123",
"token": "apiToken123",
"prefixProperties": true,
"identityMergeApi": "simplified"
}
},
"message": {
"anonymousId": "anonId01",
"channel": "mobile",
"context": {
"app": {
"build": "1",
"name": "LeanPlumIntegrationAndroid",
"namespace": "com.android.SampleLeanPlum",
"version": "1.0"
},
"device": {
"id": "5094f5704b9cf2b3",
"manufacturer": "Google",
"model": "Android SDK built for x86",
"name": "generic_x86",
"type": "ios",
"token": "test_device_token"
},
"library": {
"name": "com.rudderstack.android.sdk.core",
"version": "1.0.1-beta.1"
},
"locale": "en-US",
"network": {
"carrier": "Android",
"bluetooth": false,
"cellular": true,
"wifi": true
},
"os": {
"name": "iOS",
"version": "8.1.0"
},
"screen": {
"density": 420,
"height": 1794,
"width": 1080
},
"timezone": "Asia/Kolkata",
"userAgent": "Dalvik/2.1.0 (Linux; U; Android 8.1.0; Android SDK built for x86 Build/OSM1.180201.007)"
},
"event": "Product Viewed",
"integrations": {
"All": true
},
"userId": "userId01",
"messageId": "id2",
"properties": {
"name": "T-Shirt"
},
"type": "track",
"originalTimestamp": "2020-01-24T06:29:02.362Z",
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"sentAt": "2020-01-24T06:29:02.363Z",
"timestamp": "2020-01-24T11:59:02.402+05:30"
}
},
{
"description": "Identify: skip merge event when simplified id merge api is selected",
"destination": {
"Config": {
"apiKey": "apiKey123",
"token": "apiToken123",
"prefixProperties": true,
"identityMergeApi": "simplified"
}
},
"message": {
"anonymousId": "anonId01",
"channel": "web",
"context": {
"app": {
"build": "1.0.0",
"name": "RudderLabs JavaScript SDK",
"namespace": "com.rudderlabs.javascript",
"version": "1.0.5"
},
"ip": "0.0.0.0",
"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.5"
},
"locale": "en-GB",
"os": {
"name": "",
"version": ""
},
"screen": {
"density": 2
},
"traits": {
"city": "Disney",
"country": "USA",
"email": "mickey@disney.com",
"firstName": "Mickey",
"lastName": "Mouse",
"createdAt": "2020-01-23T08:54:02.362Z"
},
"page": {
"path": "/destinations/mixpanel",
"referrer": "",
"search": "",
"title": "",
"url": "https://docs.rudderstack.com/destinations/mixpanel",
"category": "destination",
"initial_referrer": "https://docs.rudderstack.com",
"initial_referring_domain": "docs.rudderstack.com"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
},
"integrations": {
"All": true
},
"page": {
"path": "/destinations/mixpanel",
"referrer": "",
"search": "",
"title": "",
"url": "https://docs.rudderstack.com/destinations/mixpanel",
"category": "destination",
"initial_referrer": "https://docs.rudderstack.com",
"initial_referring_domain": "docs.rudderstack.com"
},
"messageId": "2536eda4-d638-4c93-8014-8ffe3f083214",
"originalTimestamp": "2020-01-24T06:29:02.362Z",
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"request_ip": "[::1]:53709",
"sentAt": "2020-01-24T06:29:02.363Z",
"timestamp": "2020-01-24T11:59:02.402+05:30",
"type": "identify",
"userId": "userId01"
}
},
{
"description": "Identify: append $device: to deviceId while creating the user when simplified id merge api is selected",
"destination": {
"Config": {
"apiKey": "apiKey123",
"token": "apiToken123",
"identityMergeApi": "simplified"
}
},
"message": {
"anonymousId": "anonId01",
"channel": "web",
"context": {
"app": {
"build": "1.0.0",
"name": "RudderLabs JavaScript SDK",
"namespace": "com.rudderlabs.javascript",
"version": "1.0.5"
},
"ip": "0.0.0.0",
"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.5"
},
"locale": "en-GB",
"os": {
"name": "",
"version": ""
},
"screen": {
"density": 2
},
"traits": {
"city": "Disney",
"country": "USA",
"email": "mickey@disney.com",
"firstName": "Mickey",
"lastName": "Mouse",
"createdAt": "2020-01-23T08:54:02.362Z"
},
"page": {
"path": "/destinations/mixpanel",
"referrer": "",
"search": "",
"title": "",
"url": "https://docs.rudderstack.com/destinations/mixpanel",
"category": "destination",
"initial_referrer": "https://docs.rudderstack.com",
"initial_referring_domain": "docs.rudderstack.com"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
},
"integrations": {
"All": true
},
"page": {
"path": "/destinations/mixpanel",
"referrer": "",
"search": "",
"title": "",
"url": "https://docs.rudderstack.com/destinations/mixpanel",
"category": "destination",
"initial_referrer": "https://docs.rudderstack.com",
"initial_referring_domain": "docs.rudderstack.com"
},
"messageId": "2536eda4-d638-4c93-8014-8ffe3f083214",
"originalTimestamp": "2020-01-24T06:29:02.362Z",
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"request_ip": "[::1]:53709",
"sentAt": "2020-01-24T06:29:02.363Z",
"timestamp": "2020-01-24T11:59:02.402+05:30",
"type": "identify"
}
},
{
"description": "Unsupported alias call when simplified id merge api is selected",
"destination": {
"Config": {
"apiKey": "apiKey123",
"token": "apiToken123",
"identityMergeApi": "simplified"
}
},
"message": {
"anonymousId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca",
"channel": "web",
"context": {
"app": {
"build": "1.0.0",
"name": "RudderLabs JavaScript SDK",
"namespace": "com.rudderlabs.javascript",
"version": "1.0.5"
},
"ip": "0.0.0.0",
"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.5"
},
"locale": "en-GB",
"os": {
"name": "",
"version": ""
},
"screen": {
"density": 2
},
"traits": {
"city": "Disney",
"country": "USA",
"email": "mickey@disney.com",
"firstname": "Mickey"
},
"page": {
"path": "/destinations/mixpanel",
"referrer": "",
"search": "",
"title": "",
"url": "https://docs.rudderstack.com/destinations/mixpanel",
"category": "destination",
"initial_referrer": "https://docs.rudderstack.com",
"initial_referring_domain": "docs.rudderstack.com"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
},
"integrations": {
"All": true
},
"messageId": "79313729-7fe5-4204-963a-dc46f4205e4e",
"originalTimestamp": "2020-01-24T06:29:02.366Z",
"previousId": "e6ab2c5e-2cda-44a9-a962-e2f67df78bca",
"receivedAt": "2020-01-24T11:59:02.403+05:30",
"request_ip": "[::1]:53711",
"sentAt": "2020-01-24T06:29:02.366Z",
"timestamp": "2020-01-24T11:59:02.403+05:30",
"type": "alias",
"userId": "1234abc"
}
}
]
Loading