Skip to content

Commit

Permalink
Merge branch 'develop' into feat.intercom-add-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
manish339k authored Jun 19, 2024
2 parents c914e63 + 1a01362 commit 13b70d6
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ class Prometheus {
type: 'counter',
labelNames: ['destination_id'],
},
{
name: 'braze_alias_missconfigured_count',
help: 'braze_alias_missconfigured_count',
type: 'counter',
labelNames: ['destination_id'],
},
{
name: 'mixpanel_batch_engage_pack_size',
help: 'mixpanel_batch_engage_pack_size',
Expand Down
119 changes: 118 additions & 1 deletion src/v0/destinations/braze/braze.util.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require('lodash');
const { handleHttpRequest } = require('../../../adapters/network');
const { BrazeDedupUtility, addAppId, getPurchaseObjs } = require('./util');
const { BrazeDedupUtility, addAppId, getPurchaseObjs, setAliasObject } = require('./util');
const { processBatch } = require('./util');
const {
removeUndefinedAndNullValues,
Expand Down Expand Up @@ -1421,3 +1421,120 @@ describe('getPurchaseObjs', () => {
]);
});
});

describe('setAliasObject function', () => {
// Test when integrationsObj has both alias_name and alias_label
test('should set user_alias from integrationsObj if alias_name and alias_label are defined', () => {
const payload = {};
const result = setAliasObject(payload, {
anonymousId: '12345',
integrations: {
BRAZE: {
alias: {
alias_name: 'user123',
alias_label: 'customer_id',
},
},
},
});

expect(result).toEqual({
user_alias: {
alias_name: 'user123',
alias_label: 'customer_id',
},
});
});

// Test when integrationsObj is missing alias_name or alias_label
test('should set user_alias with anonymousId as alias_name and "rudder_id" as alias_label if integrationsObj does not have alias_name or alias_label', () => {
const message = {
anonymousId: '12345',
};
const payload = {};
const result = setAliasObject(payload, message);

expect(result).toEqual({
user_alias: {
alias_name: '12345',
alias_label: 'rudder_id',
},
});
});

// Test when message has no anonymousId and integrationsObj is missing
test('should return payload unchanged if message has no anonymousId and integrationsObj is missing', () => {
const message = {};
const payload = {};
const result = setAliasObject(payload, message);

expect(result).toEqual(payload);
});

test('should set user_alias from integrationsObj if alias_name and alias_label are defined', () => {
const payload = {};
const result = setAliasObject(payload, {
anonymousId: '12345',
integrations: {
BRAZE: {
alias: {
alias_name: 'user123',
alias_label: 'customer_id',
},
},
},
});

expect(result).toEqual({
user_alias: {
alias_name: 'user123',
alias_label: 'customer_id',
},
});
});

test('should set user_alias from integrationsObj if alias_name and alias_label either is not defined', () => {
const payload = {};
const result = setAliasObject(payload, {
anonymousId: '12345',
integrations: {
BRAZE: {
alias: {
alias_name: null,
alias_label: 'customer_id',
},
},
},
});

expect(result).toEqual({
user_alias: {
alias_name: '12345',
alias_label: 'rudder_id',
},
});
});

test('should set user_alias from integrationsObj if alias_name and alias_label either is not defined', () => {
const payload = {};
const result = setAliasObject(payload, {
anonymousId: '12345',
userID: 'user123',
integrations: {
BRAZE: {
alias: {
alias_name: 'rudder_id-123',
alias_label: 'customer_id',
},
},
},
});

expect(result).toEqual({
user_alias: {
alias_name: 'rudder_id-123',
alias_label: 'customer_id',
},
});
});
});
26 changes: 16 additions & 10 deletions src/v0/destinations/braze/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const {
setExternalIdOrAliasObject,
getPurchaseObjs,
setExternalId,
setAliasObjectWithAnonId,
setAliasObject,
collectStatsForAliasFailure,
collectStatsForAliasMissConfigurations,
} = require('./util');
const tags = require('../../util/tags');
const { EventType, MappedToDestinationKey } = require('../../../constants');
Expand All @@ -27,6 +28,7 @@ const {
simpleProcessRouterDestSync,
simpleProcessRouterDest,
isNewStatusCodesAccepted,
getDestinationExternalID,
} = require('../../util');
const {
ConfigCategory,
Expand Down Expand Up @@ -82,7 +84,7 @@ function buildResponse(message, destination, properties, endpoint) {

function getIdentifyPayload(message) {
let payload = {};
payload = setAliasObjectWithAnonId(payload, message);
payload = setAliasObject(payload, message);
payload = setExternalId(payload, message);
return { aliases_to_identify: [payload], merge_behavior: 'merge' };
}
Expand Down Expand Up @@ -202,12 +204,6 @@ function getUserAttributesObject(message, mappingJson, destination) {
* @param {*} destination
*/
async function processIdentify(message, destination) {
// override userId with externalId in context(if present) and event is mapped to destination
const mappedToDestination = get(message, MappedToDestinationKey);
if (mappedToDestination) {
adduserIdFromExternalId(message);
}

const identifyPayload = getIdentifyPayload(message);
const identifyEndpoint = getIdentifyEndpoint(getEndpointFromConfig(destination));
const { processedResponse: brazeIdentifyResp } = await handleHttpRequest(
Expand Down Expand Up @@ -511,10 +507,19 @@ async function process(event, processParams = { userStore: new Map() }, reqMetad
processParams,
);
break;
case EventType.IDENTIFY:
case EventType.IDENTIFY: {
category = ConfigCategory.IDENTIFY;
if (message.anonymousId) {
// override userId with externalId in context(if present) and event is mapped to destination
const mappedToDestination = get(message, MappedToDestinationKey);
if (mappedToDestination) {
adduserIdFromExternalId(message);
}
const brazeExternalID =
getDestinationExternalID(message, 'brazeExternalId') || message.userId;
if (message.anonymousId && brazeExternalID) {
await processIdentify(message, destination);
} else {
collectStatsForAliasMissConfigurations(destination.ID);
}
response = processTrackWithUserAttributes(
message,
Expand All @@ -524,6 +529,7 @@ async function process(event, processParams = { userStore: new Map() }, reqMetad
reqMetadata,
);
break;
}
case EventType.GROUP:
response = processGroup(message, destination);
break;
Expand Down
30 changes: 19 additions & 11 deletions src/v0/destinations/braze/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,18 @@ function setExternalId(payload, message) {
return payload;
}

function setAliasObjectWithAnonId(payload, message) {
if (message.anonymousId) {
function setAliasObject(payload, message) {
const integrationsObj = getIntegrationsObj(message, 'BRAZE');
if (
isDefinedAndNotNull(integrationsObj?.alias?.alias_name) &&
isDefinedAndNotNull(integrationsObj?.alias?.alias_label)
) {
const { alias_name, alias_label } = integrationsObj.alias;
payload.user_alias = {
alias_name,
alias_label,
};
} else if (message.anonymousId) {
payload.user_alias = {
alias_name: message.anonymousId,
alias_label: 'rudder_id',
Expand All @@ -537,7 +547,7 @@ function setExternalIdOrAliasObject(payload, message) {

// eslint-disable-next-line no-underscore-dangle
payload._update_existing_only = false;
return setAliasObjectWithAnonId(payload, message);
return setAliasObject(payload, message);
}

function addMandatoryPurchaseProperties(productId, price, currencyCode, quantity, timestamp) {
Expand Down Expand Up @@ -687,16 +697,13 @@ const collectStatsForAliasFailure = (brazeResponse, destinationId) => {
const { aliases_processed: aliasesProcessed, errors } = brazeResponse;
if (aliasesProcessed === 0) {
stats.increment('braze_alias_failure_count', { destination_id: destinationId });

if (Array.isArray(errors)) {
logger.info('Braze Alias Failure Errors:', {
destinationId,
errors,
});
}
}
};

const collectStatsForAliasMissConfigurations = (destinationId) => {
stats.increment('braze_alias_missconfigured_count', { destination_id: destinationId });
};

module.exports = {
BrazeDedupUtility,
CustomAttributeOperationUtil,
Expand All @@ -707,7 +714,8 @@ module.exports = {
getPurchaseObjs,
setExternalIdOrAliasObject,
setExternalId,
setAliasObjectWithAnonId,
setAliasObject,
addMandatoryPurchaseProperties,
collectStatsForAliasFailure,
collectStatsForAliasMissConfigurations,
};
Loading

0 comments on commit 13b70d6

Please sign in to comment.