Skip to content

Commit

Permalink
refactor: deprecate mixpanel /track endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Nov 16, 2023
1 parent ff80b88 commit c3e22d6
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 234 deletions.
2 changes: 0 additions & 2 deletions src/v0/destinations/mp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const MP_IDENTIFY_EXCLUSION_LIST = [
];

const GEO_SOURCE_ALLOWED_VALUES = [null, 'reverse_geocoding'];
const TRACK_MAX_BATCH_SIZE = 50;
const IMPORT_MAX_BATCH_SIZE = 2000;
const ENGAGE_MAX_BATCH_SIZE = 2000;
const GROUPS_MAX_BATCH_SIZE = 200;
Expand All @@ -65,7 +64,6 @@ module.exports = {
MP_IDENTIFY_EXCLUSION_LIST,
getCreateDeletionTaskEndpoint,
DISTINCT_ID_MAX_BATCH_SIZE,
TRACK_MAX_BATCH_SIZE,
IMPORT_MAX_BATCH_SIZE,
ENGAGE_MAX_BATCH_SIZE,
GROUPS_MAX_BATCH_SIZE,
Expand Down
63 changes: 22 additions & 41 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
BASE_ENDPOINT,
BASE_ENDPOINT_EU,
IMPORT_MAX_BATCH_SIZE,
TRACK_MAX_BATCH_SIZE,
ENGAGE_MAX_BATCH_SIZE,
GROUPS_MAX_BATCH_SIZE,
} = require('./config');
Expand All @@ -44,21 +43,19 @@ const mPEventPropertiesConfigJson = mappingConfig[ConfigCategory.EVENT_PROPERTIE
const setImportCredentials = (destConfig) => {
const endpoint =
destConfig.dataResidency === 'eu' ? `${BASE_ENDPOINT_EU}/import/` : `${BASE_ENDPOINT}/import/`;
const headers = { 'Content-Type': 'application/json' };
const params = { strict: destConfig.strictMode ? 1 : 0 };
const { apiSecret, serviceAccountUserName, serviceAccountSecret, projectId } = destConfig;
if (apiSecret) {
headers.Authorization = `Basic ${base64Convertor(`${apiSecret}:`)}`;
} else if (serviceAccountUserName && serviceAccountSecret && projectId) {
headers.Authorization = `Basic ${base64Convertor(
`${serviceAccountUserName}:${serviceAccountSecret}`,
)}`;
const { serviceAccountUserName, serviceAccountSecret, projectId, token } = destConfig;
let credentials;
if (serviceAccountUserName && serviceAccountSecret && projectId) {
credentials = `${serviceAccountUserName}:${serviceAccountSecret}`;
params.projectId = projectId;
} else {
throw new InstrumentationError(
'Event timestamp is older than 5 days and no API secret or service account credentials (i.e. username, secret and projectId) are provided in destination configuration',
);
credentials = `${token}:`;
}
const headers = {
'Content-Type': 'application/json',
Authorization: `Basic ${base64Convertor(credentials)}`,
};
return { endpoint, headers, params };
};

Expand All @@ -67,35 +64,25 @@ const responseBuilderSimple = (payload, message, eventType, destConfig) => {
response.method = defaultPostRequestConfig.requestMethod;
response.userId = message.userId || message.anonymousId;
response.body.JSON_ARRAY = { batch: JSON.stringify([removeUndefinedValues(payload)]) };
const { apiSecret, serviceAccountUserName, serviceAccountSecret, projectId, dataResidency } =
destConfig;
const { dataResidency } = destConfig;
const duration = getTimeDifference(message.timestamp);
switch (eventType) {
case EventType.ALIAS:
case EventType.TRACK:
case EventType.SCREEN:
case EventType.PAGE:
if (
!apiSecret &&
!(serviceAccountUserName && serviceAccountSecret && projectId) &&
duration.days <= 5
) {
response.endpoint =
dataResidency === 'eu' ? `${BASE_ENDPOINT_EU}/track/` : `${BASE_ENDPOINT}/track/`;
response.headers = {};
} else if (duration.years > 5) {
case EventType.PAGE: {
if (duration.years > 5) {
throw new InstrumentationError('Event timestamp should be within last 5 years');
} else {
const credentials = setImportCredentials(destConfig);
response.endpoint = credentials.endpoint;
response.headers = credentials.headers;
response.params = {
project_id: credentials.params?.projectId,
strict: credentials.params.strict,
};
break;
}
const credentials = setImportCredentials(destConfig);
response.endpoint = credentials.endpoint;
response.headers = credentials.headers;
response.params = {
project_id: credentials.params?.projectId,
strict: credentials.params.strict,
};
break;
}
case 'merge':
// eslint-disable-next-line no-case-declarations
const credentials = setImportCredentials(destConfig);
Expand Down Expand Up @@ -455,19 +442,13 @@ const processRouterDest = async (inputs, reqMetadata) => {
);

transformedPayloads = lodash.flatMap(transformedPayloads);
const { engageEvents, groupsEvents, trackEvents, importEvents, batchErrorRespList } =
const { engageEvents, groupsEvents, importEvents, batchErrorRespList } =
groupEventsByEndpoint(transformedPayloads);

const engageRespList = batchEvents(engageEvents, ENGAGE_MAX_BATCH_SIZE, reqMetadata);
const groupsRespList = batchEvents(groupsEvents, GROUPS_MAX_BATCH_SIZE, reqMetadata);
const trackRespList = batchEvents(trackEvents, TRACK_MAX_BATCH_SIZE, reqMetadata);
const importRespList = batchEvents(importEvents, IMPORT_MAX_BATCH_SIZE, reqMetadata);
const batchSuccessRespList = [
...engageRespList,
...groupsRespList,
...trackRespList,
...importRespList,
];
const batchSuccessRespList = [...engageRespList, ...groupsRespList, ...importRespList];

return [...batchSuccessRespList, ...batchErrorRespList];
}),
Expand Down
2 changes: 0 additions & 2 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ const groupEventsByEndpoint = (events) => {
const eventMap = {
engage: [],
groups: [],
track: [],
import: [],
};
const batchErrorRespList = [];
Expand All @@ -237,7 +236,6 @@ const groupEventsByEndpoint = (events) => {
return {
engageEvents: eventMap.engage,
groupsEvents: eventMap.groups,
trackEvents: eventMap.track,
importEvents: eventMap.import,
batchErrorRespList,
};
Expand Down
43 changes: 7 additions & 36 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('Mixpanel utils test', () => {
expect(result).toEqual({
engageEvents: [],
groupsEvents: [],
trackEvents: [],
importEvents: [],
batchErrorRespList: [],
});
Expand Down Expand Up @@ -78,7 +77,7 @@ describe('Mixpanel utils test', () => {
},
{
message: {
endpoint: '/track',
endpoint: '/import',
body: {
JSON_ARRAY: {
batch: '[{prop:4}]',
Expand Down Expand Up @@ -139,10 +138,10 @@ describe('Mixpanel utils test', () => {
},
},
],
trackEvents: [
importEvents: [
{
message: {
endpoint: '/track',
endpoint: '/import',
body: {
JSON_ARRAY: {
batch: '[{prop:4}]',
Expand All @@ -151,8 +150,6 @@ describe('Mixpanel utils test', () => {
userId: 'user1',
},
},
],
importEvents: [
{
message: {
endpoint: '/import',
Expand Down Expand Up @@ -267,7 +264,7 @@ describe('Mixpanel utils test', () => {
const input = [
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/track/',
endpoint: 'https://api.mixpanel.com/import/',
},
metadata: [
{
Expand All @@ -294,19 +291,6 @@ describe('Mixpanel utils test', () => {
statusCode: 200,
destination: destinationMock,
},
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/track/',
},
metadata: [
{
jobId: 5,
},
],
batched: true,
statusCode: 200,
destination: destinationMock,
},
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/engage/',
Expand Down Expand Up @@ -342,7 +326,7 @@ describe('Mixpanel utils test', () => {
{
batchedRequest: [
{
endpoint: 'https://api.mixpanel.com/track/',
endpoint: 'https://api.mixpanel.com/import/',
},
{
endpoint: 'https://api.mixpanel.com/engage/',
Expand All @@ -366,19 +350,6 @@ describe('Mixpanel utils test', () => {
statusCode: 200,
destination: destinationMock,
},
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/track/',
},
metadata: [
{
jobId: 5,
},
],
batched: true,
statusCode: 200,
destination: destinationMock,
},
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/import/',
Expand All @@ -400,7 +371,7 @@ describe('Mixpanel utils test', () => {
const input = [
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/track/',
endpoint: 'https://api.mixpanel.com/import/',
},
metadata: [
{
Expand Down Expand Up @@ -445,7 +416,7 @@ describe('Mixpanel utils test', () => {
const expectedOutput = [
{
batchedRequest: {
endpoint: 'https://api.mixpanel.com/track/',
endpoint: 'https://api.mixpanel.com/import/',
},

metadata: [
Expand Down
2 changes: 1 addition & 1 deletion test/integrations/destinations/mp/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaultMockFns = () => {
const sampleDestination = {
Config: {
apiKey: 'dummyApiKey',
token: 'dummyApiKey',
token: 'test_api_token',
prefixProperties: true,
useNativeSDK: false,
},
Expand Down
Loading

0 comments on commit c3e22d6

Please sign in to comment.