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: iterable EUDC #3828

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 21 additions & 15 deletions src/v0/destinations/iterable/config.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
const { getMappingConfig } = require('../../util');

const BASE_URL = 'https://api.iterable.com/api/';
const BASE_URL = {
USDC: 'https://api.iterable.com/api/',
EUDC: 'https://api.eu.iterable.com/api/',
};

const ConfigCategory = {
IDENTIFY_BROWSER: {
name: 'IterableRegisterBrowserTokenConfig',
action: 'identifyBrowser',
endpoint: `${BASE_URL}users/registerBrowserToken`,
endpoint: `users/registerBrowserToken`,
},
IDENTIFY_DEVICE: {
name: 'IterableRegisterDeviceTokenConfig',
action: 'identifyDevice',
endpoint: `${BASE_URL}users/registerDeviceToken`,
endpoint: `users/registerDeviceToken`,
},
IDENTIFY: {
name: 'IterableIdentifyConfig',
action: 'identify',
endpoint: `${BASE_URL}users/update`,
endpoint: `users/update`,
},
PAGE: {
name: 'IterablePageConfig',
action: 'page',
endpoint: `${BASE_URL}events/track`,
endpoint: `events/track`,
},
SCREEN: {
name: 'IterablePageConfig',
action: 'screen',
endpoint: `${BASE_URL}events/track`,
endpoint: `events/track`,
},
TRACK: {
name: 'IterableTrackConfig',
action: 'track',
endpoint: `${BASE_URL}events/track`,
endpoint: `events/track`,
},
TRACK_PURCHASE: {
name: 'IterableTrackPurchaseConfig',
action: 'trackPurchase',
endpoint: `${BASE_URL}commerce/trackPurchase`,
endpoint: `commerce/trackPurchase`,
},
PRODUCT: {
name: 'IterableProductConfig',
Expand All @@ -46,7 +49,7 @@ const ConfigCategory = {
UPDATE_CART: {
name: 'IterableProductConfig',
action: 'updateCart',
endpoint: `${BASE_URL}commerce/updateCart`,
endpoint: `commerce/updateCart`,
},
DEVICE: {
name: 'IterableDeviceConfig',
Expand All @@ -56,30 +59,33 @@ const ConfigCategory = {
ALIAS: {
name: 'IterableAliasConfig',
action: 'alias',
endpoint: `${BASE_URL}users/updateEmail`,
endpoint: `users/updateEmail`,
},
CATALOG: {
name: 'IterableCatalogConfig',
action: 'catalogs',
endpoint: `${BASE_URL}catalogs`,
endpoint: `catalogs`,
},
};

const mappingConfig = getMappingConfig(ConfigCategory, __dirname);

// Function to construct endpoint based on the selected data center
const constructEndpoint = (dataCenter, category) => {
const baseUrl = BASE_URL[dataCenter] || BASE_URL.USDC; // Default to USDC if not found
return `${baseUrl}${category.endpoint}`;
};

const IDENTIFY_MAX_BATCH_SIZE = 1000;
const IDENTIFY_MAX_BODY_SIZE_IN_BYTES = 4000000;
const IDENTIFY_BATCH_ENDPOINT = 'https://api.iterable.com/api/users/bulkUpdate';

const TRACK_MAX_BATCH_SIZE = 8000;
const TRACK_BATCH_ENDPOINT = 'https://api.iterable.com/api/events/trackBulk';

module.exports = {
mappingConfig,
ConfigCategory,
TRACK_BATCH_ENDPOINT,
constructEndpoint,
TRACK_MAX_BATCH_SIZE,
IDENTIFY_MAX_BATCH_SIZE,
IDENTIFY_BATCH_ENDPOINT,
IDENTIFY_MAX_BODY_SIZE_IN_BYTES,
};
21 changes: 10 additions & 11 deletions src/v0/destinations/iterable/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
filterEventsAndPrepareBatchRequests,
registerDeviceTokenEventPayloadBuilder,
registerBrowserTokenEventPayloadBuilder,
getCategoryWithEndpoint,
} = require('./util');
const {
constructPayload,
Expand Down Expand Up @@ -116,30 +117,29 @@ const responseBuilderForRegisterDeviceOrBrowserTokenEvents = (message, destinati

/**
* Function to find category value
* @param {*} messageType
* @param {*} message
* @returns
*/
const getCategory = (messageType, message) => {
const eventType = messageType.toLowerCase();
const getCategory = (message, dataCenter) => {
const eventType = message.type.toLowerCase();

switch (eventType) {
case EventType.IDENTIFY:
if (
get(message, MappedToDestinationKey) &&
getDestinationExternalIDInfoForRetl(message, 'ITERABLE').objectType !== 'users'
) {
return ConfigCategory.CATALOG;
return getCategoryWithEndpoint(ConfigCategory.CATALOG, dataCenter);
}
return ConfigCategory.IDENTIFY;
return getCategoryWithEndpoint(ConfigCategory.IDENTIFY, dataCenter);
case EventType.PAGE:
return ConfigCategory.PAGE;
return getCategoryWithEndpoint(ConfigCategory.PAGE, dataCenter);
case EventType.SCREEN:
return ConfigCategory.SCREEN;
return getCategoryWithEndpoint(ConfigCategory.SCREEN, dataCenter);
case EventType.TRACK:
return getCategoryUsingEventName(message);
return getCategoryUsingEventName(message, dataCenter);
case EventType.ALIAS:
return ConfigCategory.ALIAS;
return getCategoryWithEndpoint(ConfigCategory.ALIAS, dataCenter);
default:
throw new InstrumentationError(`Message type ${eventType} not supported`);
}
Expand All @@ -150,8 +150,7 @@ const process = (event) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
const messageType = message.type.toLowerCase();
const category = getCategory(messageType, message);
const category = getCategory(message, destination.Config.dataCenter);
const response = responseBuilder(message, category, destination);

if (hasMultipleResponses(message, category, destination.Config)) {
Expand Down
32 changes: 19 additions & 13 deletions src/v0/destinations/iterable/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const {
ConfigCategory,
mappingConfig,
TRACK_MAX_BATCH_SIZE,
TRACK_BATCH_ENDPOINT,
IDENTIFY_MAX_BATCH_SIZE,
IDENTIFY_BATCH_ENDPOINT,
IDENTIFY_MAX_BODY_SIZE_IN_BYTES,
constructEndpoint,
} = require('./config');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { EventType, MappedToDestinationKey } = require('../../../constants');
Expand Down Expand Up @@ -88,25 +87,30 @@ const hasMultipleResponses = (message, category, config) => {
return isIdentifyEvent && isIdentifyCategory && hasToken && hasRegisterDeviceOrBrowserKey;
};

const getCategoryWithEndpoint = (categoryConfig, dataCenter) => ({
...categoryConfig,
endpoint: constructEndpoint(dataCenter, categoryConfig),
});

/**
* Returns category value
* @param {*} message
* @returns
*/
const getCategoryUsingEventName = (message) => {
const getCategoryUsingEventName = (message, dataCenter) => {
let { event } = message;
if (typeof event === 'string') {
event = event.toLowerCase();
}

switch (event) {
case 'order completed':
return ConfigCategory.TRACK_PURCHASE;
return getCategoryWithEndpoint(ConfigCategory.TRACK_PURCHASE, dataCenter);
case 'product added':
case 'product removed':
return ConfigCategory.UPDATE_CART;
return getCategoryWithEndpoint(ConfigCategory.UPDATE_CART, dataCenter);
default:
return ConfigCategory.TRACK;
return getCategoryWithEndpoint(ConfigCategory.TRACK, dataCenter);
}
};

Expand Down Expand Up @@ -444,8 +448,8 @@ const processUpdateUserBatch = (chunk, registerDeviceOrBrowserTokenEvents) => {
batchEventResponse.batchedRequest.body.JSON = { users: batch.users };

const { destination, metadata, nonBatchedRequests } = batch;
const { apiKey } = destination.Config;

const { apiKey, dataCenter } = destination.Config;
const IDENTIFY_BATCH_ENDPOINT = constructEndpoint(dataCenter, { endpoint: 'users/bulkUpdate' });
const batchedResponse = combineBatchedAndNonBatchedEvents(
apiKey,
metadata,
Expand Down Expand Up @@ -552,8 +556,8 @@ const processTrackBatch = (chunk) => {
const metadata = [];

const { destination } = chunk[0];
const { apiKey } = destination.Config;

const { apiKey, dataCenter } = destination.Config;
const TRACK_BATCH_ENDPOINT = constructEndpoint(dataCenter, { endpoint: 'events/trackBulk' });
chunk.forEach((event) => {
metadata.push(event.metadata);
events.push(get(event, `${MESSAGE_JSON_PATH}`));
Expand Down Expand Up @@ -653,12 +657,13 @@ const mapRegisterDeviceOrBrowserTokenEventsWithJobId = (events) => {
*/
const categorizeEvent = (event) => {
const { message, metadata, destination, error } = event;
const { dataCenter } = destination.Config;

if (error) {
return { type: 'error', data: event };
}

if (message.endpoint === ConfigCategory.IDENTIFY.endpoint) {
if (message.endpoint === constructEndpoint(dataCenter, ConfigCategory.IDENTIFY)) {
return { type: 'updateUser', data: { message, metadata, destination } };
}

Expand All @@ -667,8 +672,8 @@ const categorizeEvent = (event) => {
}

if (
message.endpoint === ConfigCategory.IDENTIFY_BROWSER.endpoint ||
message.endpoint === ConfigCategory.IDENTIFY_DEVICE.endpoint
message.endpoint === constructEndpoint(dataCenter, ConfigCategory.IDENTIFY_BROWSER) ||
message.endpoint === constructEndpoint(dataCenter, ConfigCategory.IDENTIFY_DEVICE)
) {
return { type: 'registerDeviceOrBrowser', data: { message, metadata, destination } };
}
Expand Down Expand Up @@ -753,4 +758,5 @@ module.exports = {
filterEventsAndPrepareBatchRequests,
registerDeviceTokenEventPayloadBuilder,
registerBrowserTokenEventPayloadBuilder,
getCategoryWithEndpoint,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { generateMetadata, transformResultBuilder } from './../../../testUtils';
import {
generateMetadata,
overrideDestination,
transformResultBuilder,
} from './../../../testUtils';
import { Destination } from '../../../../../src/types';
import { ProcessorTestData } from '../../../testTypes';

Expand All @@ -15,6 +19,7 @@ const destination: Destination = {
Transformations: [],
Config: {
apiKey: 'testApiKey',
dataCenter: 'USDC',
preferUserId: false,
trackAllPages: true,
trackNamedPages: false,
Expand Down Expand Up @@ -94,4 +99,56 @@ export const aliasTestData: ProcessorTestData[] = [
},
},
},
{
id: 'iterable-alias-test-1',
name: 'iterable',
description: 'Alias call with dataCenter as EUDC',
scenario: 'Business',
successCriteria:
'Response should contain status code 200 and it should contain update email payload',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
destination: overrideDestination(destination, { dataCenter: 'EUDC' }),
message: {
anonymousId: 'anonId',
userId: 'new@email.com',
previousId: 'old@email.com',
name: 'ApplicationLoaded',
context: {},
properties,
type: 'alias',
sentAt,
originalTimestamp,
},
metadata: generateMetadata(1),
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: transformResultBuilder({
userId: '',
headers,
endpoint: 'https://api.eu.iterable.com/api/users/updateEmail',
JSON: {
currentEmail: 'old@email.com',
newEmail: 'new@email.com',
},
}),
statusCode: 200,
metadata: generateMetadata(1),
},
],
},
},
},
];
Loading
Loading