Skip to content

Commit

Permalink
refactor: use json templates for custom mappings
Browse files Browse the repository at this point in the history
updated test cases as some cases are not valid.
  • Loading branch information
koladilip committed Jun 10, 2024
1 parent 448cf20 commit 6a3d583
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"@ndhoule/extend": "^2.0.0",
"@pyroscope/nodejs": "^0.2.9",
"@rudderstack/integrations-lib": "^0.2.8",
"@rudderstack/workflow-engine": "^0.7.9",
"@rudderstack/json-template-engine": "^0.11.0",
"@rudderstack/workflow-engine": "^0.8.0",
"@shopify/jest-koa-mocks": "^5.1.1",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
Expand Down
56 changes: 25 additions & 31 deletions src/v0/destinations/ga4/customMappingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const {
removeUndefinedAndNullValues,
isHybridModeEnabled,
getIntegrationsObj,
applyCustomMappings,
} = require('../../util');
const { trackCommonConfig, ConfigCategory, mappingConfig } = require('./config');
const { mapWithJsonPath } = require('../../util/mapWithJSONPath');

const findGA4Events = (eventsMapping, event) => {
// Find the event using destructuring and early return
Expand All @@ -39,7 +39,7 @@ const findGA4Events = (eventsMapping, event) => {
return validMappings;
};

const handleCustomMappings = (message, Config) => {
const handleCustomMappings = async (message, Config) => {
const { eventsMapping } = Config;

let rsEvent = '';
Expand Down Expand Up @@ -102,36 +102,35 @@ const handleCustomMappings = (message, Config) => {
return buildDeliverablePayload(rawPayload, Config);
}

const processedPayloads = validMappings.map((mapping) => {
const eventName = mapping.destEventName;
// reserved event names are not allowed
if (isReservedEventName(eventName)) {
throw new InstrumentationError(`[GA4]:: Reserved event name: ${eventName} are not allowed`);
}
// validation for ga4 event name
validateEventName(eventName);
const processedPayloads = await Promise.all(
validMappings.map(async (mapping) => {
const eventName = mapping.destEventName;
// reserved event names are not allowed
if (isReservedEventName(eventName)) {
throw new InstrumentationError(`[GA4]:: Reserved event name: ${eventName} are not allowed`);
}
// validation for ga4 event name
validateEventName(eventName);

// Add common top level payload
let ga4BasicPayload = constructPayload(message, trackCommonConfig);
ga4BasicPayload = addClientDetails(ga4BasicPayload, message, Config);
// Add common top level payload
let ga4BasicPayload = constructPayload(message, trackCommonConfig);
ga4BasicPayload = addClientDetails(ga4BasicPayload, message, Config);

const eventPropertiesMappings = mapping.eventProperties || {};
const eventPropertiesMappings = mapping.eventProperties || [];

const ga4MappedPayload = {};
const ga4MappedPayload = await applyCustomMappings(message, eventPropertiesMappings);

for (const propertyMapping of eventPropertiesMappings) {
mapWithJsonPath(message, ga4MappedPayload, propertyMapping.from, propertyMapping.to);
}
removeUndefinedAndNullRecurse(ga4MappedPayload);
removeUndefinedAndNullRecurse(ga4MappedPayload);

boilerplateOperations(ga4MappedPayload, message, Config, eventName);
boilerplateOperations(ga4MappedPayload, message, Config, eventName);

if (isDefinedAndNotNull(ga4BasicPayload)) {
return { ...ga4BasicPayload, ...ga4MappedPayload };
} else {
return ga4MappedPayload;
}
});
if (isDefinedAndNotNull(ga4BasicPayload)) {
return { ...ga4BasicPayload, ...ga4MappedPayload };
} else {
return ga4MappedPayload;
}
}),
);

return processedPayloads.map((processedPayload) =>
buildDeliverablePayload(processedPayload, Config),
Expand Down Expand Up @@ -162,11 +161,6 @@ const boilerplateOperations = (ga4Payload, message, Config, eventName) => {
if (!isEmptyObject(consents)) {
ga4Payload.consent = consents;
}

// sanitize user properties
if (ga4Payload.user_properties) {
sanitizeUserProperties(ga4Payload.user_properties);
}
};

module.exports = {
Expand Down
13 changes: 13 additions & 0 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const {
OAuthSecretError,
getErrorRespEvents,
} = require('@rudderstack/integrations-lib');

const { JsonTemplateEngine, PathType } = require('@rudderstack/json-template-engine');
const logger = require('../../logger');
const stats = require('../../util/stats');
const { DestCanonicalNames, DestHandlerMap } = require('../../constants/destinationCanonicalNames');
Expand Down Expand Up @@ -2246,6 +2248,16 @@ const validateEventAndLowerCaseConversion = (event, isMandatory, convertToLowerC
return convertToLowerCase ? event.toString().toLowerCase() : event.toString();
};

const applyCustomMappings = (message, mappings) => {
const flatMappings = mappings.map((mapping) => ({
input: mapping.from,
output: mapping.to,
}));
return JsonTemplateEngine.create(flatMappings, { defaultPathType: PathType.JSON }).evaluate(
message,
);
};

// ========================================================================
// EXPORTS
// ========================================================================
Expand All @@ -2254,6 +2266,7 @@ module.exports = {
ErrorMessage,
addExternalIdToTraits,
adduserIdFromExternalId,
applyCustomMappings,
base64Convertor,
batchMultiplexedEvents,
checkEmptyStringInarray,
Expand Down

0 comments on commit 6a3d583

Please sign in to comment.