Skip to content

Commit

Permalink
refactor: use sync implementation of json template
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 10, 2024
1 parent db70d02 commit b5fc015
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
47 changes: 22 additions & 25 deletions src/v0/destinations/ga4_v2/customMappingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
buildDeliverablePayload,
GA4_PARAMETERS_EXCLUSION,
prepareUserProperties,
sanitizeUserProperties,
} = require('../ga4/utils');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const {
Expand All @@ -39,7 +38,7 @@ const findGA4Events = (eventsMapping, event) => {
return validMappings;
};

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

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

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);
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`);

Check warning on line 108 in src/v0/destinations/ga4_v2/customMappingsHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/ga4_v2/customMappingsHandler.js#L108

Added line #L108 was not covered by tests
}
// 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 = await applyCustomMappings(message, eventPropertiesMappings);
const ga4MappedPayload = applyCustomMappings(message, eventPropertiesMappings);

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;

Check warning on line 128 in src/v0/destinations/ga4_v2/customMappingsHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/ga4_v2/customMappingsHandler.js#L127-L128

Added lines #L127 - L128 were not covered by tests
}
});

return processedPayloads.map((processedPayload) =>
buildDeliverablePayload(processedPayload, Config),
Expand Down
2 changes: 1 addition & 1 deletion src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ const applyCustomMappings = (message, mappings) => {
input: mapping.from,
output: mapping.to,
}));
return JsonTemplateEngine.create(flatMappings, { defaultPathType: PathType.JSON }).evaluate(
return JsonTemplateEngine.createAsSync(flatMappings, { defaultPathType: PathType.JSON }).evaluate(
message,
);
};
Expand Down

0 comments on commit b5fc015

Please sign in to comment.