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 new destination ninetailed #3106

Merged
merged 12 commits into from
Feb 29, 2024
29 changes: 29 additions & 0 deletions src/cdk/v2/destinations/ninetailed/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { getMappingConfig } = require('../../../../v0/util');

const ConfigCategories = {
GENERAL: {
type: 'general',
name: 'generalPayloadMapping',
},
CONTEXT: {
type: 'context',
name: 'contextMapping',
},
TRACK: {
type: 'track',
name: 'trackMapping',
},
IDENTIFY: {
type: 'identify',
name: 'identifyMapping',
},
PAGE: {
type: 'page',
name: 'pageMapping',
},
};
const MAX_BATCH_SIZE = 200; // Maximum number of events to send in a single batch
const mappingConfig = getMappingConfig(ConfigCategories, __dirname);
const batchEndpoint =
'https://experience.ninetailed.co/v2/organizations/{{organisationId}}/environments/{{environment}}/events';
module.exports = { ConfigCategories, mappingConfig, batchEndpoint, MAX_BATCH_SIZE };
43 changes: 43 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/contextMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"sourceKeys": "context.app.name",
"required": true,
"destKey": "context.app.name"
},
{
"sourceKeys": "context.app.version",
"required": true,
"destKey": "context.app.version"
},
{
"sourceKeys": "context.campaign",
"destKey": "context.campaign"
},
{
"sourceKeys": "context.library.name",
"required": true,
"destKey": "context.library.name"
},
{
"sourceKeys": "context.library.version",
"required": true,
"destKey": "context.library.version"
},
{
"sourceKeys": "context.locale",
"destKey": "context.locale"
},
{
"sourceKeys": "context.page",
"destKey": "context.page"
},
{
"sourceKeys": "context.userAgent",
"destKey": "context.userAgent"
},
{
"sourceKeys": "context.location",
"required": true,
"destKey": "context.location"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"sourceKeys": "anonymousId",
"required": true,
"destKey": "anonymousId"
},
{
"sourceKeys": "messageId",
"required": true,
"destKey": "messageId"
},
{
"sourceKeys": "channel",
"required": true,
"destKey": "channel"
},
{
"sourceKeys": "type",
"destKey": "type"
},
{
"sourceKeys": "originalTimestamp",
"destKey": "originalTimestamp"
}
]
14 changes: 14 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/identifyMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"sourceKeys": "traits",
"sourceFromGenericMap": true,
"required": true,
"destKey": "traits"
},
{
"sourceKeys": "userIdOnly",
"sourceFromGenericMap": true,
"required": true,
"destKey": "userId"
}
]
7 changes: 7 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/pageMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"sourceKeys": "properties",
"required": true,
"destKey": "properties"
}
]
12 changes: 12 additions & 0 deletions src/cdk/v2/destinations/ninetailed/data/trackMapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"sourceKeys": "properties",
"required": true,
"destKey": "properties"
},
{
"sourceKeys": "event",
"required": true,
"destKey": "event"
}
]
33 changes: 33 additions & 0 deletions src/cdk/v2/destinations/ninetailed/procWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
bindings:
- name: EventType
path: ../../../../constants
- path: ../../bindings/jsontemplate
- name: defaultRequestConfig
path: ../../../../v0/util
- name: removeUndefinedAndNullValues
path: ../../../../v0/util
- path: ./utils

steps:
- name: messageType
template: |
.message.type.toLowerCase();
- name: validateInput
template: |
let messageType = $.outputs.messageType;
$.assert(messageType, "message Type is not present. Aborting");
$.assert(messageType in {{$.EventType.([.TRACK,.IDENTIFY,.PAGE])}}, "message type " + messageType + " is not supported");
$.assertConfig(.destination.Config.organisationId, "Organisation ID is not present. Aborting");
$.assertConfig(.destination.Config.environment, "Environment is not present. Aborting");
- name: preparePayload
template: |
const payload = $.constructFullPayload(.message);
$.context.payload = $.removeUndefinedAndNullValues(payload);

- name: buildResponse
template: |
const response = $.defaultRequestConfig();
response.body.JSON.events = [$.context.payload];
response.endpoint = $.getEndpoint(.destination.Config);
response.method = "POST";
response
59 changes: 59 additions & 0 deletions src/cdk/v2/destinations/ninetailed/rtWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
bindings:
- path: ./config
- name: handleRtTfSingleEventError
path: ../../../../v0/util/index
- path: ./utils
steps:
- name: validateInput
template: |
console.log('router input: ', ^)
$.assert(Array.isArray(^) && ^.length > 0, "Invalid event array")

- name: transform
externalWorkflow:
path: ./procWorkflow.yaml
loopOverInput: true

- name: successfulEvents
template: |
$.outputs.transform#idx.output.({
"output": .body.JSON.events[0],
"destination": ^[idx].destination,
"metadata": ^[idx].metadata
})[]
- name: failedEvents
template: |
$.outputs.transform#idx.error.(
$.handleRtTfSingleEventError(^[idx], .originalError ?? ., {})
)[]
- name: batchSuccessfulEvents
description: Batches the successfulEvents
template: |
console.log('successBatch: ', $.outputs.successfulEvents)
let batches = $.chunk($.outputs.successfulEvents, $.MAX_BATCH_SIZE);
batches@batch.({
"batchedRequest": {
"body": {
"JSON": {"events": ~r batch.output[]},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"version": "1",
"type": "REST",
"method": "POST",
"endpoint": $.getEndpoint(batch[0].destination.Config),
"headers": {
"Content-Type": "application/json"
},
"params": {},
"files": {}
},
"metadata": ~r batch.metadata[],
"batched": true,
"statusCode": 200,
"destination": batch[0].destination
})[];
- name: finalPayload
template: |
[...$.outputs.failedEvents, ...$.outputs.batchSuccessfulEvents]
38 changes: 38 additions & 0 deletions src/cdk/v2/destinations/ninetailed/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { mappingConfig, ConfigCategories, batchEndpoint } = require('./config');
const { constructPayload } = require('../../../../v0/util');

/**
* This fucntion constructs payloads based upon mappingConfig for all calls
* We build context as it has some specific payloads with default values so just breaking them down
* @param {*} message
* @returns
*/
const constructFullPayload = (message) => {
const context = constructPayload(message, mappingConfig[ConfigCategories.CONTEXT.name]);
let payload = constructPayload(message, mappingConfig[ConfigCategories.GENERAL.name]);
let typeSpecifcPayload;
switch (message.type) {
case 'track':
typeSpecifcPayload = constructPayload(message, mappingConfig[ConfigCategories.TRACK.name]);
break;
case 'identify':
typeSpecifcPayload = constructPayload(message, mappingConfig[ConfigCategories.IDENTIFY.name]);
break;
case 'page':
typeSpecifcPayload = constructPayload(message, mappingConfig[ConfigCategories.PAGE.name]);
break;
default:
break;

Check warning on line 25 in src/cdk/v2/destinations/ninetailed/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/ninetailed/utils.js#L24-L25

Added lines #L24 - L25 were not covered by tests
}
payload = { ...payload, ...context };
return { ...payload, ...typeSpecifcPayload }; // merge base and type-specific payloads;
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
};

const getEndpoint = (Config) => {
const { organisationId, environment } = Config;
return batchEndpoint
.replace('{{organisationId}}', organisationId)
.replace('{{environment}}', environment);
};

module.exports = { constructFullPayload, getEndpoint };
3 changes: 2 additions & 1 deletion src/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"TIKTOK_AUDIENCE": true,
"REDDIT": true,
"THE_TRADE_DESK": true,
"INTERCOM": true
"INTERCOM": true,
"NINETAILED": true
},
"regulations": [
"BRAZE",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export const destination = {
ID: 'random_id',
Name: 'ninetailed',
DestinationDefinition: {
Config: {
cdkV2Enabled: true,
},
},
Config: {
organisationId: 'dummyOrganisationId',
environment: 'main',
},
};

export const metadata = {
destinationId: 'dummyDestId',
};

// export const commonOutputHeaders = {
// accept: 'application/json',
// 'content-type': 'application/json',
// };
export const commonProperties = {
segment: 'SampleSegment',
shipcountry: 'USA',
shipped: '20240129_1500',
sitename: 'SampleSiteName',
storeId: '12345',
storecat: 'Electronics',
};
export const traits = {
email: 'test@user.com',
firstname: 'John',
lastname: 'Doe',
phone: '+1(123)456-7890',
gender: 'Male',
birthday: '1980-01-02',
city: 'San Francisco',
};
export const context = {
app: {
name: 'RudderLabs JavaScript SDK',
version: '1.0.0',
},
campaign: {
name: 'campign_123',
source: 'social marketing',
medium: 'facebook',
term: '1 year',
},
library: {
name: 'RudderstackSDK',
version: 'Ruddderstack SDK version',
},
locale: 'en-US',
page: {
path: '/signup',
referrer: 'https://rudderstack.medium.com/',
search: '?type=freetrial',
url: 'https://app.rudderstack.com/signup?type=freetrial',
},
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
location: {
coordinates: {
latitude: 40.7128,
longitude: -74.006,
},
city: 'San Francisco',
postalCode: '94107',
region: 'CA',
regionCode: 'CA',
country: ' United States',
countryCode: 'United States of America',
continent: 'North America',
timezone: 'America/Los_Angeles',
},
};
5 changes: 5 additions & 0 deletions test/integrations/destinations/ninetailed/processor/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { validationFailures } from './validation';
import { track } from './track';
import { page } from './page';
import { identify } from './identify';
export const data = [...identify, ...page, ...track, ...validationFailures];