Skip to content

Commit

Permalink
feat: add event mapping support for branch destination (#3135)
Browse files Browse the repository at this point in the history
* feat: add event mapping support for branch destination

* refactor: address review comments
  • Loading branch information
Gauravudia committed Feb 29, 2024
1 parent 9afcd7b commit cc94bba
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/v0/destinations/branch/transform.js
Expand Up @@ -13,6 +13,7 @@ const {
simpleProcessRouterDest,
} = require('../../util');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { getMappedEventNameFromConfig } = require('./utils');

function responseBuilder(payload, message, destination, category) {
const response = defaultRequestConfig();
Expand Down Expand Up @@ -213,24 +214,21 @@ function getCommonPayload(message, category, evName) {
return rawPayload;
}

// function getTrackPayload(message) {
// const rawPayload = {};
// const { name, category } = getCategoryAndName(message.event);
// rawPayload.name = name;
//
// return commonPayload(message, rawPayload, category);
// }

function processMessage(message, destination) {
let evName;
let category;
switch (message.type) {
case EventType.TRACK:
case EventType.TRACK: {
if (!message.event) {
throw new InstrumentationError('Event name is required');
}
({ evName, category } = getCategoryAndName(message.event));
const eventNameFromConfig = getMappedEventNameFromConfig(message, destination);
if (eventNameFromConfig) {
evName = eventNameFromConfig;
}
break;
}
case EventType.IDENTIFY:
({ evName, category } = getCategoryAndName(message.userId));
break;
Expand Down
24 changes: 24 additions & 0 deletions src/v0/destinations/branch/utils.js
@@ -0,0 +1,24 @@
const { getHashFromArray } = require('../../util');

/**
* Retrieves the mapped event name from the given config.
*
* @param {object} message - The message object containing the event.
* @param {object} destination - The destination object containing the events mapping configuration.
* @returns {string} - The mapped event name, or undefined if not found.
*/
const getMappedEventNameFromConfig = (message, destination) => {
let eventName;
const { event } = message;
const { eventsMapping } = destination.Config;

// if event is mapped on dashboard, use the mapped event name
if (Array.isArray(eventsMapping) && eventsMapping.length > 0) {
const keyMap = getHashFromArray(eventsMapping, 'from', 'to', false);
eventName = keyMap[event];
}

return eventName;
};

module.exports = { getMappedEventNameFromConfig };
18 changes: 18 additions & 0 deletions src/v0/destinations/branch/utils.test.js
@@ -0,0 +1,18 @@
const { getMappedEventNameFromConfig } = require('./utils');
describe('getMappedEventNameFromConfig', () => {
it('should return the mapped event name when it exists in the events mapping configuration', () => {
const message = { event: 'Order Completed' };
const destination = {
Config: { eventsMapping: [{ from: 'Order Completed', to: 'PURCHASE' }] },
};
const result = getMappedEventNameFromConfig(message, destination);
expect(result).toBe('PURCHASE');
});

it('should return undefined when the event mapping is not created', () => {
const message = { event: 'Order Completed' };
const destination = { Config: {} };
const result = getMappedEventNameFromConfig(message, destination);
expect(result).toBeUndefined();
});
});
156 changes: 156 additions & 0 deletions test/integrations/destinations/branch/processor/data.ts
Expand Up @@ -1490,4 +1490,160 @@ export const data = [
},
},
},
{
name: 'branch',
description: 'Map event name to branch standard event name in track call',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
destination: {
Config: {
branchKey: 'test_branch_key',
eventsMapping: [
{
from: 'Order Completed',
to: 'PURCHASE',
},
],
useNativeSDK: false,
},
DestinationDefinition: {
DisplayName: 'Branch Metrics',
ID: '1WTpBSTiL3iAUHUdW7rHT4sawgU',
Name: 'BRANCH',
},
Enabled: true,
ID: '1WTpIHpH7NTBgjeiUPW1kCUgZGI',
Name: 'branch test',
Transformations: [],
},
message: {
anonymousId: 'anonId123',
channel: 'web',
context: {
app: {
build: '1.0.0',
name: 'RudderLabs JavaScript SDK',
namespace: 'com.rudderlabs.javascript',
version: '1.0.0',
},
device: {
adTrackingEnabled: true,
advertisingId: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
id: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
manufacturer: 'Google',
model: 'AOSP on IA Emulator',
name: 'generic_x86_arm',
type: 'ios',
attTrackingStatus: 3,
},
ip: '0.0.0.0',
library: {
name: 'RudderLabs JavaScript SDK',
version: '1.0.0',
},
locale: 'en-US',
os: {
name: 'iOS',
version: '14.4.1',
},
screen: {
density: 2,
},
traits: {
anonymousId: 'anonId123',
email: 'test_user@gmail.com',
},
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
},
event: 'Order Completed',
integrations: {
All: true,
},
messageId: 'ea5cfab2-3961-4d8a-8187-3d1858c90a9f',
originalTimestamp: '2020-01-17T04:53:51.185Z',
properties: {
name: 't-shirt',
revenue: '10',
currency: 'USD',
key1: 'value1',
key2: 'value2',
order_id: 'order123',
},
receivedAt: '2020-01-17T10:23:52.688+05:30',
request_ip: '[::1]:64059',
sentAt: '2020-01-17T04:53:52.667Z',
timestamp: '2020-01-17T10:23:51.206+05:30',
type: 'track',
userId: 'userId123',
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://api2.branch.io/v2/event/standard',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
params: {},
body: {
JSON: {
branch_key: 'test_branch_key',
name: 'PURCHASE',
content_items: [
{
$product_name: 't-shirt',
},
],
event_data: {
revenue: '10',
currency: 'USD',
},
custom_data: {
key1: 'value1',
key2: 'value2',
order_id: 'order123',
},
user_data: {
os: 'iOS',
os_version: '14.4.1',
app_version: '1.0.0',
screen_dpi: 2,
developer_identity: 'userId123',
idfa: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
idfv: '3f034872-5e28-45a1-9eda-ce22a3e36d1a',
limit_ad_tracking: false,
model: 'AOSP on IA Emulator',
user_agent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
},
},
XML: {},
JSON_ARRAY: {},
FORM: {},
},
files: {},
userId: 'anonId123',
},
statusCode: 200,
},
],
},
},
},
];

0 comments on commit cc94bba

Please sign in to comment.