Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into chore.clean-up-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Sai Sankeerth committed Jun 19, 2024
2 parents 7d2a13c + 1a01362 commit f7cf56c
Show file tree
Hide file tree
Showing 16 changed files with 1,736 additions and 495 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,
};
53 changes: 8 additions & 45 deletions src/v0/destinations/fb_custom_audience/recordTransform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* eslint-disable no-const-assign */
const lodash = require('lodash');
const get = require('get-value');
const {
InstrumentationError,
ConfigurationError,
getErrorRespEvents,
} = require('@rudderstack/integrations-lib');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const { schemaFields } = require('./config');
const { MappedToDestinationKey } = require('../../../constants');
const stats = require('../../../util/stats');
Expand All @@ -15,8 +11,8 @@ const {
checkSubsetOfArray,
returnArrayOfSubarrays,
getSuccessRespEvents,
generateErrorObject,
} = require('../../util');
const { getErrorResponse, createFinalResponse } = require('../../util/recordUtils');
const {
ensureApplicableFormat,
getUpdatedDataElement,
Expand All @@ -26,19 +22,6 @@ const {
getDataSource,
} = require('./util');

function getErrorMetaData(inputs, acceptedOperations) {
const metadata = [];
// eslint-disable-next-line no-restricted-syntax
for (const key in inputs) {
if (!acceptedOperations.includes(key)) {
inputs[key].forEach((input) => {
metadata.push(input.metadata);
});
}
}
return metadata;
}

const processRecordEventArray = (
recordChunksArray,
userSchema,
Expand Down Expand Up @@ -177,8 +160,6 @@ async function processRecordInputs(groupedRecordInputs) {
record.message.action?.toLowerCase(),
);

const finalResponse = [];

let insertResponse;
let deleteResponse;
let updateResponse;
Expand Down Expand Up @@ -238,32 +219,14 @@ async function processRecordInputs(groupedRecordInputs) {
);
}

const eventTypes = ['update', 'insert', 'delete'];
const errorMetaData = [];
const errorMetaDataObject = getErrorMetaData(groupedRecordsByAction, eventTypes);
if (errorMetaDataObject.length > 0) {
errorMetaData.push(errorMetaDataObject);
}
const errorResponse = getErrorResponse(groupedRecordsByAction);

const error = new InstrumentationError('Invalid action type in record event');
const errorObj = generateErrorObject(error);
const errorResponseList = errorMetaData.map((metadata) =>
getErrorRespEvents(metadata, errorObj.status, errorObj.message, errorObj.statTags),
const finalResponse = createFinalResponse(
deleteResponse,
insertResponse,
updateResponse,
errorResponse,
);

if (deleteResponse && deleteResponse.batchedRequest.length > 0) {
finalResponse.push(deleteResponse);
}
if (insertResponse && insertResponse.batchedRequest.length > 0) {
finalResponse.push(insertResponse);
}
if (updateResponse && updateResponse.batchedRequest.length > 0) {
finalResponse.push(updateResponse);
}
if (errorResponseList.length > 0) {
finalResponse.push(...errorResponseList);
}

if (finalResponse.length === 0) {
throw new InstrumentationError(
'Missing valid parameters, unable to generate transformed payload',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getMappingConfig } = require('../../util');

const BASE_ENDPOINT = 'https://googleads.googleapis.com/v15/customers';
const BASE_ENDPOINT = 'https://googleads.googleapis.com/v16/customers';
const CONFIG_CATEGORIES = {
AUDIENCE_LIST: { type: 'audienceList', name: 'offlineDataJobs' },
ADDRESSINFO: { type: 'addressInfo', name: 'addressInfo' },
Expand Down
Loading

0 comments on commit f7cf56c

Please sign in to comment.