Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of custom page/screen event name in mixpanel #3098

Merged
merged 8 commits into from
Mar 1, 2024
26 changes: 23 additions & 3 deletions src/v0/destinations/mp/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
groupEventsByEndpoint,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
} = require('./util');
const { CommonUtils } = require('../../../util/common');

Expand Down Expand Up @@ -298,17 +299,25 @@
};

const processPageOrScreenEvents = (message, type, destination) => {
const {
token,
identityMergeApi,
useUserDefinedPageEventName,
userDefinedPageEventTemplate,
useUserDefinedScreenEventName,
userDefinedScreenEventTemplate,
} = destination.Config;
const mappedProperties = constructPayload(message, mPEventPropertiesConfigJson);
let properties = {
...get(message, 'context.traits'),
...message.properties,
...mappedProperties,
token: destination.Config.token,
token,
distinct_id: message.userId || message.anonymousId,
time: toUnixTimestampInMS(message.timestamp || message.originalTimestamp),
...buildUtmParams(message.context?.campaign),
};
if (destination.Config?.identityMergeApi === 'simplified') {
if (identityMergeApi === 'simplified') {
properties = {
...properties,
distinct_id: message.userId || `$device:${message.anonymousId}`,
Expand All @@ -327,7 +336,18 @@
properties.$browser = browser.name;
properties.$browser_version = browser.version;
}
const eventName = type === 'page' ? 'Loaded a Page' : 'Loaded a Screen';

let eventName;
if (type === 'page') {
eventName = useUserDefinedPageEventName
? generatePageOrScreenCustomEventName(message, userDefinedPageEventTemplate)
: 'Loaded a Page';
} else {
eventName = useUserDefinedScreenEventName

Check warning on line 346 in src/v0/destinations/mp/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/transform.js#L346

Added line #L346 was not covered by tests
? generatePageOrScreenCustomEventName(message, userDefinedScreenEventTemplate)
: 'Loaded a Screen';
}

const payload = {
event: eventName,
properties,
Expand Down
38 changes: 37 additions & 1 deletion src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const lodash = require('lodash');
const set = require('set-value');
const get = require('get-value');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
isDefined,
constructPayload,
Expand Down Expand Up @@ -301,6 +301,41 @@
};
}

/**
* Generates a custom event name for a page or screen.
*
* @param {Object} message - The message object
* @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) => {
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
if (!userDefinedEventTemplate) {
throw new ConfigurationError(

Check warning on line 318 in src/v0/destinations/mp/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/util.js#L318

Added line #L318 was not covered by tests
'Event name template is not configured. Please provide a valid value for the `Page/Screen Event Name Template` in the destination dashboard.',
);
}

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, '');

Check warning on line 333 in src/v0/destinations/mp/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/util.js#L333

Added line #L333 was not covered by tests
}

return eventName.trim();
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
};

module.exports = {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
Expand All @@ -309,4 +344,5 @@
generateBatchedPayloadForArray,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
};
Loading
Loading