diff --git a/src/v0/destinations/mp/util.js b/src/v0/destinations/mp/util.js index e02d5d675c..3d8fec35ba 100644 --- a/src/v0/destinations/mp/util.js +++ b/src/v0/destinations/mp/util.js @@ -1,7 +1,6 @@ const lodash = require('lodash'); const set = require('set-value'); const get = require('get-value'); -const Handlebars = require('handlebars'); const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib'); const { isDefined, @@ -309,6 +308,10 @@ function trimTraits(traits, contextTraits, setOnceProperties) { * @param {string} userDefinedEventTemplate - The user-defined event template to be used for generating the event name. * @throws {ConfigurationError} If the event template is missing. * @returns {string} The generated custom event name. + * @example + * const userDefinedEventTemplate = "Viewed {{ category }} {{ name }} Page"; + * const message = {name: 'Home', properties: {category: 'Index'}}; + * output: "Viewed Index Home Page" */ const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) => { if (!userDefinedEventTemplate) { @@ -316,8 +319,21 @@ const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) 'Event template is missing. Please provide a valid event template in `Page/Screen Event Name Template` setting', ); } - const eventTemplate = Handlebars.compile(userDefinedEventTemplate); - return eventTemplate(message); + + let eventName = userDefinedEventTemplate + .replace('{{ category }}', message.properties?.category || '') + .trim(); + eventName = eventName.replace('{{ name }}', message.name || '').trim(); + // Remove any extra space between placeholders + eventName = eventName.replace(/\s{2,}/g, ' '); + + // Check if any placeholders remain + if (eventName.includes('{{')) { + // Handle the case where either name or category is missing + eventName = eventName.replace(/{{\s*\w+\s*}}/g, ''); + } + + return eventName.trim(); }; module.exports = { diff --git a/src/v0/destinations/mp/util.test.js b/src/v0/destinations/mp/util.test.js index 96839fe2d0..b227a8509d 100644 --- a/src/v0/destinations/mp/util.test.js +++ b/src/v0/destinations/mp/util.test.js @@ -450,10 +450,26 @@ describe('Unit test cases for trimTraits', () => { }); describe('generatePageOrScreenCustomEventName', () => { - it('should generate a custom event name when userDefinedEventTemplate contains handlebars and message object is provided', () => { - const message = { name: 'Home' }; - const userDefinedEventTemplate = 'Viewed a {{ name }} page'; - const expected = 'Viewed a Home page'; + it('should generate a custom event name when userDefinedEventTemplate contains event template and message object is provided', () => { + const message = { name: 'Doc', properties: { category: 'Integration' } }; + const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page'; + const expected = 'Viewed Integration Doc page'; + const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); + expect(result).toBe(expected); + }); + + it('should generate a custom event name when userDefinedEventTemplate contains event template and category or name is missing in message object', () => { + const message = { name: 'Doc' }; + const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page'; + const expected = 'Viewed Doc page'; + const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); + expect(result).toBe(expected); + }); + + it('should generate a custom event name when userDefinedEventTemplate contains only category or name placeholder and message object is provided', () => { + const message = { name: 'Doc', properties: { category: 'Integration' } }; + const userDefinedEventTemplate = 'Viewed {{ name }} page'; + const expected = 'Viewed Doc page'; const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); expect(result).toBe(expected); }); @@ -466,26 +482,18 @@ describe('generatePageOrScreenCustomEventName', () => { }).toThrow(ConfigurationError); }); - it('should return the userDefinedEventTemplate when it does not contain handlebars', () => { - const message = { name: 'Home' }; + it('should return the userDefinedEventTemplate when it does not contain placeholder {{}}', () => { + const message = { name: 'Index' }; const userDefinedEventTemplate = 'Viewed a Home page'; const expected = 'Viewed a Home page'; const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); expect(result).toBe(expected); }); - it('should return a event name when message object is not provided', () => { - const message = undefined; - const userDefinedEventTemplate = 'Viewed a {{ path }} page'; - const expected = 'Viewed a page'; - const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); - expect(result).toBe(expected); - }); - - it('should return a custom event name when userDefinedEventTemplate contains multiple handlebar and message object is provided', () => { - const message = { name: 'Home', properties: { category: 'Index' } }; - const userDefinedEventTemplate = 'Viewed a {{ properties.category }} {{ name }} page'; - const expected = 'Viewed a Index Home page'; + it('should return a event name when message object is not provided/empty', () => { + const message = {}; + const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page'; + const expected = 'Viewed page'; const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate); expect(result).toBe(expected); });