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: onboard cordial source #3593

Merged
merged 9 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/v0/sources/cordial/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const eventsMapping = {
crdl_app_install: 'Application Installed',
crdl_app_open: 'Application Opened',
};

module.exports = { eventsMapping };
22 changes: 22 additions & 0 deletions src/v0/sources/cordial/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"sourceKeys": "event._id",
"destKeys": "properties.event_id"
},
{
"sourceKeys": "contact._id",
"destKeys": ["context.traits.userId", "userId"]
},
{
"sourceKeys": ["event.ts", "event.ats"],
"destKeys": ["timestamp", "sentAt", "originalTimestamp"]
},
{
"sourceKeys": "event.d",
"destKeys": "context.device"
},
{
"sourceKeys": "contact.channels.email.address",
"destKeys": ["context.traits.email", "properties.email"]
}
]
49 changes: 49 additions & 0 deletions src/v0/sources/cordial/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const md5 = require('md5');
const Message = require('../message');
const { CommonUtils } = require('../../../util/common');
const { generateUUID, isDefinedAndNotNull } = require('../../util');
const { eventsMapping } = require('./config');

const mapping = require('./mapping.json');

const processEvent = (event) => {
const message = new Message(`Cordial`);
let eventName = event.event?.a || event.event?.action;
if (eventName in eventsMapping) {
eventName = eventsMapping[eventName];

Check warning on line 13 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L13

Added line #L13 was not covered by tests
}
message.setEventType('track');
message.setEventName(eventName);
message.setPropertiesV2(event, mapping);

const externalId = [];
// setting up cordial contact_id to externalId
if (event.cID) {
externalId.push({

Check warning on line 22 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L22

Added line #L22 was not covered by tests
type: 'cordialContactId',
id: event.cID,
});
}
message.context.externalId = externalId;

if (!message.userId && event.email) {
// Treating userId as unique identifier
// If userId is not present, then generating it from email using md5 hash function
message.userId = md5(event.email);

Check warning on line 32 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L32

Added line #L32 was not covered by tests
}
if (!isDefinedAndNotNull(message.userId)) {
message.anonymousId = generateUUID();

Check warning on line 35 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L35

Added line #L35 was not covered by tests
}
// Due to multiple mappings to the same destination path object some are not showing up due to which we are doing the following
message.context.traits = { ...message.context.traits, ...event.contact };
message.properties = { ...message.properties, ...event.event.properties, ...event.event };
delete message.properties.properties;
return message;
};

const process = (events) => {
const eventsArray = CommonUtils.toArray(events);
return eventsArray.map(processEvent);
};

module.exports = { process };
Loading
Loading