Skip to content

Commit

Permalink
feat: add support of custom page/screen event name in mixpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Feb 15, 2024
1 parent 6a364fb commit 6c01c7a
Show file tree
Hide file tree
Showing 4 changed files with 414 additions and 349 deletions.
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 @@ const {
groupEventsByEndpoint,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
} = require('./util');
const { CommonUtils } = require('../../../util/common');

Expand Down Expand Up @@ -298,17 +299,25 @@ const processIdentifyEvents = async (message, type, destination) => {
};

const processPageOrScreenEvents = (message, type, destination) => {
const {
token,
identityMergeApi,
useUserDefinedPageEventName,
userDefinedPageEventString,
useUserDefinedScreenEventName,
userDefinedScreenEventString,
} = 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 @@ const processPageOrScreenEvents = (message, type, destination) => {
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, userDefinedPageEventString)
: 'Loaded a Page';
} else {
eventName = useUserDefinedScreenEventName
? generatePageOrScreenCustomEventName(message, userDefinedScreenEventString)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L347 was not covered by tests
: 'Loaded a Screen';
}

const payload = {
event: eventName,
properties,
Expand Down
27 changes: 27 additions & 0 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,32 @@ function trimTraits(traits, contextTraits, setOnceProperties) {
};
}

/**
* Generates a custom event name for a page or screen.
*
* @param {Object} message - The message object.
* @param {string} userDefinedEventString - The user-defined event string.
* @returns {string} - The generated custom event name.
* @throws {InstrumentationError} - If the event does not contain the configured path.
* @example
*
* const message = {name: 'Home'};
* const userDefinedEventString = 'Viewed a {{ name }} page';
* -> Viewed a Home page
*/
const generatePageOrScreenCustomEventName = (message, userDefinedEventString) => {
const getMessagePath = userDefinedEventString?.match(/{\s*([^{}]+)\s*}/)?.[1]?.trim();
if (!getMessagePath) return userDefinedEventString;

const value = get(message, getMessagePath);
if (!value) {
throw new InstrumentationError(
`'Use Custom Page/Screen Event Name' setting is enabled but the event does not contain the ${getMessagePath} configured in 'Page/Screen Event Name Format' setting`,
);
}
return userDefinedEventString?.trim().replaceAll(/{{([^{}]+)}}/g, value);
};

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

0 comments on commit 6c01c7a

Please sign in to comment.