Skip to content

Commit

Permalink
feat: add custom utm parameters to mixpanel (#2771)
Browse files Browse the repository at this point in the history
* feat: add custom utm parameters to mixpanel

* test: add testcases

* refactor: add optional chaining

* refactor: add isObject check
  • Loading branch information
Gauravudia authored Oct 30, 2023
1 parent 3387cb0 commit 9c4fcd3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
24 changes: 0 additions & 24 deletions src/v0/destinations/mp/data/MPEventPropertiesConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@
"sourceKeys": "context.screen.density",
"destKey": "$screen_dpi"
},
{
"sourceKeys": "context.campaign.name",
"destKey": "utm_campaign"
},
{
"sourceKeys": "context.campaign.source",
"destKey": "utm_source"
},
{
"sourceKeys": "context.campaign.medium",
"destKey": "utm_medium"
},
{
"sourceKeys": "context.campaign.term",
"destKey": "utm_term"
},
{
"sourceKeys": "context.campaign.content",
"destKey": "utm_content"
},
{
"sourceKeys": "context.campaign.test",
"destKey": "utm_test"
},
{
"sourceKeys": "context.network.carrier",
"destKey": "$carrier"
Expand Down
3 changes: 3 additions & 0 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
const {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
buildUtmParams,
combineBatchRequestsWithSameJobIds,
groupEventsByEndpoint,
batchEvents,
Expand Down Expand Up @@ -179,6 +180,7 @@ const getEventValueForTrackEvent = (message, destination) => {
token: destination.Config.token,
distinct_id: message.userId || message.anonymousId,
time: unixTimestamp,
...buildUtmParams(message.context?.campaign),
};

if (destination.Config?.identityMergeApi === 'simplified') {
Expand Down Expand Up @@ -267,6 +269,7 @@ const processPageOrScreenEvents = (message, type, destination) => {
token: destination.Config.token,
distinct_id: message.userId || message.anonymousId,
time: toUnixTimestamp(message.timestamp),
...buildUtmParams(message.context?.campaign),
};
if (destination.Config?.identityMergeApi === 'simplified') {
properties = {
Expand Down
31 changes: 31 additions & 0 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
getSuccessRespEvents,
defaultBatchRequestConfig,
IsGzipSupported,
isObject,
} = require('../../util');
const {
ConfigCategory,
Expand Down Expand Up @@ -173,6 +174,35 @@ const removeDuplicateMetadata = (mergedBatches) => {
});
};

/**
* Builds UTM parameters from a campaign object.
*
* @param {Object} campaign - The campaign object containing the campaign details.
* @returns {Object} - The object containing the UTM parameters extracted from the campaign object.
*
* @example
* const campaign = {
* name: 'summer_sale',
* source: 'newsletter',
* medium: 'email'
* };
* { utm_campaign: 'summer_sale', utm_source: 'newsletter', utm_medium: 'email' }
*/
const buildUtmParams = (campaign) => {
const utmParams = {};
if (isObject(campaign)) {
Object.keys(campaign).forEach((key) => {
if (key === 'name') {
utmParams.utm_campaign = campaign[key];
} else {
utmParams[`utm_${key}`] = campaign[key];
}
});
}

return utmParams;
};

/**
* Group events with the same endpoint together in batches
* @param {*} events - An array of events
Expand Down Expand Up @@ -295,6 +325,7 @@ const combineBatchRequestsWithSameJobIds = (inputBatches) => {
module.exports = {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
buildUtmParams,
groupEventsByEndpoint,
generateBatchedPayloadForArray,
batchEvents,
Expand Down
32 changes: 32 additions & 0 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
groupEventsByEndpoint,
batchEvents,
generateBatchedPayloadForArray,
buildUtmParams,
} = require('./util');
const { FEATURE_GZIP_SUPPORT } = require('../../util/constant');

Expand Down Expand Up @@ -570,4 +571,35 @@ describe('Mixpanel utils test', () => {
expect(result).toEqual(expectedBatchedRequest);
});
});

describe('Unit test cases for buildUtmParams', () => {
it('should return an empty object when campaign is undefined', () => {
const campaign = undefined;
const result = buildUtmParams(campaign);
expect(result).toEqual({});
});

it('should return an empty object when campaign is an empty object', () => {
const campaign = {};
const result = buildUtmParams(campaign);
expect(result).toEqual({});
});

it('should return an empty object when campaign is not an object', () => {
const campaign = [{ name: 'test' }];
const result = buildUtmParams(campaign);
expect(result).toEqual({});
});

it('should handle campaign object with null/undefined values', () => {
const campaign = { name: null, source: 'rudder', medium: 'rudder', test: undefined };
const result = buildUtmParams(campaign);
expect(result).toEqual({
utm_campaign: null,
utm_source: 'rudder',
utm_medium: 'rudder',
test: undefined,
});
});
});
});
22 changes: 20 additions & 2 deletions test/integrations/destinations/mp/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export const data = [
initial_referrer: 'https://docs.rudderstack.com',
initial_referring_domain: 'docs.rudderstack.com',
},
campaign: {
name: 'test_name',
source: 'rudder',
medium: 'test_medium',
term: 'test_tem',
content: 'test_content',
test: 'test',
keyword: 'test_keyword',
},
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',
},
Expand Down Expand Up @@ -85,7 +94,7 @@ export const data = [
JSON: {},
JSON_ARRAY: {
batch:
'[{"event":"Loaded a Page","properties":{"ip":"0.0.0.0","$user_id":"hjikl","$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":"dd266c67-9199-4a52-ba32-f46ddde67312","token":"dummyApiKey","distinct_id":"hjikl","time":1579847342,"name":"Contact Us","$browser":"Chrome","$browser_version":"79.0.3945.117"}}]',
'[{"event":"Loaded a Page","properties":{"ip":"0.0.0.0","campaign_id":"test_name","$user_id":"hjikl","$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":"dd266c67-9199-4a52-ba32-f46ddde67312","token":"dummyApiKey","distinct_id":"hjikl","time":1579847342,"utm_campaign":"test_name","utm_source":"rudder","utm_medium":"test_medium","utm_term":"test_tem","utm_content":"test_content","utm_test":"test","utm_keyword":"test_keyword","name":"Contact Us","$browser":"Chrome","$browser_version":"79.0.3945.117"}}]',
},
XML: {},
FORM: {},
Expand Down Expand Up @@ -600,6 +609,15 @@ export const data = [
initial_referrer: 'https://docs.rudderstack.com',
initial_referring_domain: 'docs.rudderstack.com',
},
campaign: {
name: 'test_name',
source: 'rudder',
medium: 'test_medium',
term: 'test_tem',
content: 'test_content',
test: 'test',
keyword: 'test_keyword',
},
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',
},
Expand Down Expand Up @@ -687,7 +705,7 @@ export const data = [
JSON: {},
JSON_ARRAY: {
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"}}]',
'[{"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","campaign_id":"test_name","$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,"utm_campaign":"test_name","utm_source":"rudder","utm_medium":"test_medium","utm_term":"test_tem","utm_content":"test_content","utm_test":"test","utm_keyword":"test_keyword","$browser":"Chrome","$browser_version":"79.0.3945.117"}}]',
},
XML: {},
FORM: {},
Expand Down

0 comments on commit 9c4fcd3

Please sign in to comment.