diff --git a/.github/workflows/component-test-report.yml b/.github/workflows/component-test-report.yml index d0d75db660..3d457df9ff 100644 --- a/.github/workflows/component-test-report.yml +++ b/.github/workflows/component-test-report.yml @@ -28,7 +28,7 @@ jobs: fetch-depth: 1 - name: Setup Node - uses: actions/setup-node@v4.0.1 + uses: actions/setup-node@v4.0.2 with: node-version-file: '.nvmrc' cache: 'npm' @@ -44,7 +44,7 @@ jobs: run: | aws s3 cp ./test_reports/ s3://test-integrations-dev/integrations-test-reports/rudder-transformer/${{ github.event.number }}/ --recursive - - name: Comment on PR with S3 Object URL + - name: Add Test Report Link as Comment on PR uses: actions/github-script@v7 with: github-token: ${{ secrets.PAT }} @@ -54,9 +54,22 @@ jobs: const prNumber = context.payload.pull_request.number; const commentBody = `Test report for this run is available at: https://test-integrations-dev.s3.amazonaws.com/integrations-test-reports/rudder-transformer/${prNumber}/test-report.html`; + // find all the comments of the PR + const issueComments = await github.paginate(github.rest.issues.listComments, { + owner, + repo, + issue_number: prNumber, + }); + + for (const comment of issueComments) { + if (comment.body === commentBody) { + console.log('Comment already exists'); + return; + } + } // Comment on the pull request - github.rest.issues.createComment({ + await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, diff --git a/.github/workflows/create-hotfix-branch.yml b/.github/workflows/create-hotfix-branch.yml index a164c25bee..ec89f8d342 100644 --- a/.github/workflows/create-hotfix-branch.yml +++ b/.github/workflows/create-hotfix-branch.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest # Only allow these users to create new hotfix branch from 'main' - if: github.ref == 'refs/heads/main' && (github.actor == 'ItsSudip' || github.actor == 'krishna2020' || github.actor == 'koladilip' || github.actor == 'saikumarrs' || github.actor == 'sandeepdsvs' || github.actor == 'shrouti1507' || github.actor == 'anantjain45823' || github.actor == 'chandumlg' || github.actor == 'mihir-4116' || github.actor == 'ujjwal-ab') && (github.triggering_actor == 'ItsSudip' || github.triggering_actor == 'krishna2020' || github.triggering_actor == 'saikumarrs' || github.triggering_actor == 'sandeepdsvs' || github.triggering_actor == 'koladilip' || github.triggering_actor == 'shrouti1507' || github.triggering_actor == 'anantjain45823' || github.triggering_actor == 'chandumlg' || github.triggering_actor == 'mihir-4116' || github.triggering_actor == 'ujjwal-ab') + if: github.ref == 'refs/heads/main' && (github.actor == 'ItsSudip' || github.actor == 'krishna2020' || github.actor == 'koladilip' || github.actor == 'saikumarrs' || github.actor == 'sandeepdsvs' || github.actor == 'shrouti1507' || github.actor == 'anantjain45823' || github.actor == 'chandumlg' || github.actor == 'mihir-4116' || github.actor == 'ujjwal-ab') && (github.triggering_actor == 'ItsSudip' || github.triggering_actor == 'krishna2020' || github.triggering_actor == 'saikumarrs' || github.triggering_actor == 'sandeepdsvs' || github.triggering_actor == 'koladilip' || github.triggering_actor == 'shrouti1507' || github.triggering_actor == 'anantjain45823' || github.triggering_actor == 'chandumlg' || github.triggering_actor == 'mihir-4116' || github.triggering_actor == 'sanpj2292') steps: - name: Create Branch uses: peterjgrainger/action-create-branch@v2.4.0 diff --git a/.github/workflows/draft-new-release.yml b/.github/workflows/draft-new-release.yml index 33b0396705..c69a481545 100644 --- a/.github/workflows/draft-new-release.yml +++ b/.github/workflows/draft-new-release.yml @@ -16,7 +16,7 @@ jobs: fetch-depth: 0 - name: Setup Node - uses: actions/setup-node@v4.0.1 + uses: actions/setup-node@v4.0.2 with: node-version-file: '.nvmrc' cache: 'npm' diff --git a/.github/workflows/dt-test-and-report-code-coverage.yml b/.github/workflows/dt-test-and-report-code-coverage.yml index 4096227400..51a9f8c9ee 100644 --- a/.github/workflows/dt-test-and-report-code-coverage.yml +++ b/.github/workflows/dt-test-and-report-code-coverage.yml @@ -20,7 +20,7 @@ jobs: fetch-depth: 1 - name: Setup Node - uses: actions/setup-node@v4.0.1 + uses: actions/setup-node@v4.0.2 with: node-version-file: '.nvmrc' cache: 'npm' @@ -40,12 +40,14 @@ jobs: npm run lint:fix - name: Upload Coverage Reports to Codecov - uses: codecov/codecov-action@v3.1.4 + uses: codecov/codecov-action@v4.0.1 with: directory: ./reports/coverage - name: Upload TS Coverage Reports to Codecov - uses: codecov/codecov-action@v3.1.4 + uses: codecov/codecov-action@v4.0.1 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: directory: ./reports/ts-coverage diff --git a/.github/workflows/publish-new-release.yml b/.github/workflows/publish-new-release.yml index 9d1558d826..233e99577d 100644 --- a/.github/workflows/publish-new-release.yml +++ b/.github/workflows/publish-new-release.yml @@ -30,7 +30,7 @@ jobs: fetch-depth: 0 - name: Setup Node - uses: actions/setup-node@v4.0.1 + uses: actions/setup-node@v4.0.2 with: node-version-file: '.nvmrc' cache: 'npm' @@ -89,7 +89,7 @@ jobs: - name: Notify Slack Channel id: slack - uses: slackapi/slack-github-action@v1.24.0 + uses: slackapi/slack-github-action@v1.25.0 continue-on-error: true env: SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} diff --git a/.github/workflows/ut-tests.yml b/.github/workflows/ut-tests.yml index 30c29ceaee..0a2ef8a390 100644 --- a/.github/workflows/ut-tests.yml +++ b/.github/workflows/ut-tests.yml @@ -26,7 +26,7 @@ jobs: fetch-depth: 1 - name: Setup Node - uses: actions/setup-node@v4.0.1 + uses: actions/setup-node@v4.0.2 with: node-version-file: '.nvmrc' cache: 'npm' diff --git a/src/cdk/v2/destinations/bluecore/config.js b/src/cdk/v2/destinations/bluecore/config.js new file mode 100644 index 0000000000..9b9cde9c66 --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/config.js @@ -0,0 +1,56 @@ +const { getMappingConfig } = require('../../../../v0/util'); + +const BASE_URL = 'https://api.bluecore.com/api/track/mobile/v1'; + +const CONFIG_CATEGORIES = { + IDENTIFY: { + name: 'bluecoreIdentifyConfig', + type: 'identify', + }, + TRACK: { + name: 'bluecoreTrackConfig', + type: 'track', + }, + COMMON: { + name: 'bluecoreCommonConfig', + type: 'common', + }, +}; + +const EVENT_NAME_MAPPING = [ + { + src: ['product viewed'], + dest: 'viewed_product', + }, + { + src: ['products searched'], + dest: 'search', + }, + { + src: ['product added'], + dest: 'add_to_cart', + }, + { + src: ['product removed'], + dest: 'remove_from_cart', + }, + { + src: ['product added to wishlist'], + dest: 'wishlist', + }, + { + src: ['order completed'], + dest: 'purchase', + }, +]; + +const BLUECORE_EXCLUSION_FIELDS = ['query', 'order_id', 'total']; + +const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname); +module.exports = { + CONFIG_CATEGORIES, + MAPPING_CONFIG, + EVENT_NAME_MAPPING, + BASE_URL, + BLUECORE_EXCLUSION_FIELDS, +}; diff --git a/src/cdk/v2/destinations/bluecore/data/bluecoreCommonConfig.json b/src/cdk/v2/destinations/bluecore/data/bluecoreCommonConfig.json new file mode 100644 index 0000000000..be74c7c4b3 --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/data/bluecoreCommonConfig.json @@ -0,0 +1,52 @@ +[ + { + "destKey": "properties.customer.name", + "sourceKeys": "name", + "required": false, + "sourceFromGenericMap": true + }, + { + "destKey": "properties.customer.first_name", + "sourceKeys": "firstName", + "required": false, + "sourceFromGenericMap": true + }, + { + "destKey": "properties.customer.last_name", + "sourceKeys": "lastName", + "required": false, + "sourceFromGenericMap": true + }, + { + "destKey": "properties.customer.age", + "sourceKeys": ["context.traits.age", "traits.age"], + "required": false + }, + { + "destKey": "properties.customer.sex", + "sourceKeys": ["traits.gender", "context.traits.gender", "traits.sex", "context.traits.sex"], + "required": false + }, + { + "destKey": "properties.customer.address", + "sourceKeys": "address", + "required": false, + "sourceFromGenericMap": true + }, + { + "destKey": "properties.customer.email", + "sourceKeys": "email", + "required": false, + "sourceFromGenericMap": true + }, + { + "destKey": "properties.client", + "sourceKeys": "context.app.version", + "required": false + }, + { + "destKey": "properties.device", + "sourceKeys": "context.device.model", + "required": false + } +] diff --git a/src/cdk/v2/destinations/bluecore/data/bluecoreIdentifyConfig.json b/src/cdk/v2/destinations/bluecore/data/bluecoreIdentifyConfig.json new file mode 100644 index 0000000000..5c3686f0ab --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/data/bluecoreIdentifyConfig.json @@ -0,0 +1,7 @@ +[ + { + "destKey": "event", + "sourceKeys": ["traits.action", "context.traits.action"], + "required": false + } +] diff --git a/src/cdk/v2/destinations/bluecore/data/bluecoreTrackConfig.json b/src/cdk/v2/destinations/bluecore/data/bluecoreTrackConfig.json new file mode 100644 index 0000000000..8f6d59ec54 --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/data/bluecoreTrackConfig.json @@ -0,0 +1,22 @@ +[ + { + "destKey": "properties.search_term", + "sourceKeys": "properties.query", + "required": false + }, + { + "destKey": "properties.order_id", + "sourceKeys": "properties.order_id", + "required": false + }, + { + "destKey": "properties.total", + "sourceKeys": "properties.total", + "required": false + }, + { + "destKey": "properties.products", + "sourceKeys": ["properties.products"], + "required": false + } +] diff --git a/src/cdk/v2/destinations/bluecore/procWorkflow.yaml b/src/cdk/v2/destinations/bluecore/procWorkflow.yaml new file mode 100644 index 0000000000..378659fa2a --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/procWorkflow.yaml @@ -0,0 +1,69 @@ +bindings: + - name: EventType + path: ../../../../constants + - path: ../../bindings/jsontemplate + - name: defaultRequestConfig + path: ../../../../v0/util + - name: removeUndefinedNullValuesAndEmptyObjectArray + path: ../../../../v0/util + - name: removeUndefinedAndNullValues + path: ../../../../v0/util + - path: ./utils + - path: lodash + name: cloneDeep + +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])}}, "message type " + messageType + " is not supported"); + $.assertConfig(.destination.Config.bluecoreNamespace, "[BLUECORE] account namespace required for Authentication."); + - name: prepareIdentifyPayload + condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} + template: | + const payload = $.constructProperties(.message); + payload.token = .destination.Config.bluecoreNamespace; + $.verifyPayload(payload, .message); + payload.event = payload.event ?? 'customer_patch'; + payload.properties.distinct_id = $.populateAccurateDistinctId(payload, .message); + $.context.payloads = [$.removeUndefinedAndNullValues(payload)]; + - name: handleTrackEvent + condition: $.outputs.messageType === {{$.EventType.TRACK}} + steps: + - name: validateInput + description: Additional validation for Track events + template: | + $.assert(.message.event, "event_name could not be mapped. Aborting.") + - name: deduceEventNames + template: | + $.context.deducedEventNameArray = $.deduceTrackEventName(.message.event,.destination.Config) + - name: preparePayload + template: | + const payload = $.constructProperties(.message); + $.context.payloads = $.context.deducedEventNameArray@eventName.( + const newPayload = $.cloneDeep(payload); + newPayload.properties.distinct_id = $.populateAccurateDistinctId(newPayload, ^.message); + const temporaryProductArray = newPayload.properties.products ?? $.createProductForStandardEcommEvent(^.message, eventName); + newPayload.properties.products = $.normalizeProductArray(temporaryProductArray); + newPayload.event = eventName; + newPayload.token = ^.destination.Config.bluecoreNamespace; + $.verifyPayload(newPayload, ^.message); + $.removeUndefinedNullValuesAndEmptyObjectArray(newPayload) + )[]; + + - name: buildResponse + template: | + $.context.payloads.( + const response = $.defaultRequestConfig(); + response.body.JSON = .; + response.method = "POST"; + response.endpoint = "https://api.bluecore.com/api/track/mobile/v1"; + response.headers = { + "Content-Type": "application/json" + }; + response + ) diff --git a/src/cdk/v2/destinations/bluecore/utils.js b/src/cdk/v2/destinations/bluecore/utils.js new file mode 100644 index 0000000000..22ec254fe2 --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/utils.js @@ -0,0 +1,250 @@ +const lodash = require('lodash'); + +const { + InstrumentationError, + isDefinedAndNotNullAndNotEmpty, + getHashFromArrayWithDuplicate, + isDefinedAndNotNull, + isDefinedNotNullNotEmpty, +} = require('@rudderstack/integrations-lib'); +const { + getFieldValueFromMessage, + validateEventName, + constructPayload, + getDestinationExternalID, +} = require('../../../../v0/util'); +const { CommonUtils } = require('../../../../util/common'); +const { EVENT_NAME_MAPPING } = require('./config'); +const { EventType } = require('../../../../constants'); +const { MAPPING_CONFIG, CONFIG_CATEGORIES } = require('./config'); + +/** + * Verifies the correctness of payload for different events. + * + * @param {Object} payload - The payload object containing event information. + * @param {Object} message - The message object containing additional information. + * @throws {InstrumentationError} - Throws an error if required properties are missing. + * @returns {void} + */ +const verifyPayload = (payload, message) => { + if ( + message.type === EventType.IDENTIFY && + isDefinedNotNullNotEmpty(message.traits?.action) && + message.traits?.action !== 'identify' + ) { + throw new InstrumentationError( + "[Bluecore] traits.action must be 'identify' for identify action", + ); + } + switch (payload.event) { + case 'search': + if (!payload?.properties?.search_term) { + throw new InstrumentationError( + '[Bluecore] property:: search_query is required for search event', + ); + } + break; + case 'purchase': + if (!payload?.properties?.order_id) { + throw new InstrumentationError( + '[Bluecore] property:: order_id is required for purchase event', + ); + } + if (!payload?.properties?.total) { + throw new InstrumentationError( + '[Bluecore] property:: total is required for purchase event', + ); + } + if ( + !isDefinedAndNotNull(payload?.properties?.customer) || + Object.keys(payload.properties.customer).length === 0 + ) { + throw new InstrumentationError( + `[Bluecore] property:: No relevant trait to populate customer information, which is required for ${payload.event} event`, + ); + } + break; + case 'identify': + case 'optin': + case 'unsubscribe': + if (!isDefinedAndNotNullAndNotEmpty(getFieldValueFromMessage(message, 'email'))) { + throw new InstrumentationError( + `[Bluecore] property:: email is required for ${payload.event} action`, + ); + } + if ( + !isDefinedAndNotNull(payload?.properties?.customer) || + Object.keys(payload.properties.customer).length === 0 + ) { + throw new InstrumentationError( + `[Bluecore] property:: No relevant trait to populate customer information, which is required for ${payload.event} action`, + ); + } + break; + default: + break; + } +}; + +/** + * Deduces the track event name based on the provided track event name and configuration. + * + * @param {string} trackEventName - The track event name to deduce. + * @param {object} Config - The configuration object. + * @returns {string|array} - The deduced track event name. + */ +const deduceTrackEventName = (trackEventName, destConfig) => { + let eventName; + const { eventsMapping } = destConfig; + validateEventName(trackEventName); + /* + Step 1: Will look for the event name in the eventsMapping array if mapped to a standard bluecore event. + and return the corresponding event name if found. + */ + if (eventsMapping.length > 0) { + const keyMap = getHashFromArrayWithDuplicate(eventsMapping, 'from', 'to', false); + eventName = keyMap[trackEventName]; + } + if (isDefinedAndNotNullAndNotEmpty(eventName)) { + const finalEvent = typeof eventName === 'string' ? [eventName] : [...eventName]; + return finalEvent; + } + + /* + Step 2: To find if the particular event is amongst the list of standard + Rudderstack ecommerce events, used specifically for Bluecore API + mappings. + */ + + const eventMapInfo = EVENT_NAME_MAPPING.find((eventMap) => + eventMap.src.includes(trackEventName.toLowerCase()), + ); + if (isDefinedAndNotNull(eventMapInfo)) { + return [eventMapInfo.dest]; + } + + // Step 3: if nothing matches this is to be considered as a custom event + return [trackEventName]; +}; + +/** + * Determines if the given event name is a standard Bluecore event. + * + * @param {string} eventName - The name of the event to check. + * @returns {boolean} - True if the event is a standard Bluecore event, false otherwise. + */ +const isStandardBluecoreEvent = (eventName) => { + // Return false immediately if eventName is an empty string or falsy + if (!eventName) { + return false; + } + // Proceed with the original check if eventName is not empty + return !!EVENT_NAME_MAPPING.some((item) => item.dest.includes(eventName)); +}; + +/** + * Adds an array of products to a message. + * + * @param {object} message - The message object to add the products to. + * @param {array|object} products - The array or object of products to add. + * @param {string} eventName - The name of the event. + * @throws {InstrumentationError} - If the products array is not defined or null. + * @returns {array} - The updated product array. + */ +const normalizeProductArray = (products) => { + let finalProductArray = null; + if (isDefinedAndNotNull(products)) { + const productArray = CommonUtils.toArray(products); + const mappedProductArray = productArray.map( + ({ product_id, sku, id, query, order_id, total, ...rest }) => ({ + id: product_id || sku || id, + ...rest, + }), + ); + finalProductArray = mappedProductArray; + } + // if any custom event is not sent with product array, then it should be null + return finalProductArray; +}; + +/** + * Constructs properties based on the given message. + * + * @param {object} message - The message object. + * @returns {object} - The constructed properties object. + */ +const constructProperties = (message) => { + const commonCategory = CONFIG_CATEGORIES.COMMON; + const commonPayload = constructPayload(message, MAPPING_CONFIG[commonCategory.name]); + const category = CONFIG_CATEGORIES[message.type.toUpperCase()]; + const typeSpecificPayload = constructPayload(message, MAPPING_CONFIG[category.name]); + const finalPayload = lodash.merge(commonPayload, typeSpecificPayload); + return finalPayload; +}; + +/** + * Creates a product for a standard e-commerce event. + * + * @param {Object} properties - The properties of the product. + * @param {string} eventName - The name of the event. + * @returns {Array|null} - An array containing the properties if the event is a standard Bluecore event and not 'search', otherwise null. + */ +const createProductForStandardEcommEvent = (message, eventName) => { + const { event, properties } = message; + if (event.toLowerCase() === 'order completed' && eventName === 'purchase') { + throw new InstrumentationError('[Bluecore]:: products array is required for purchase event'); + } + if (eventName !== 'search' && isStandardBluecoreEvent(eventName)) { + return [properties]; + } + return null; +}; +/** + * Function: populateAccurateDistinctId + * + * Description: + * This function is used to populate the accurate distinct ID based on the given payload and message. + * + * Parameters: + * - payload (object): The payload object containing the event and other data. + * - message (object): The message object containing the user data. + * + * Returns: + * - distinctId (string): The accurate distinct ID based on the given payload and message. + * + * Throws: + * - InstrumentationError: If the distinct ID could not be set. + * + */ +const populateAccurateDistinctId = (payload, message) => { + const bluecoreExternalId = getDestinationExternalID(message, 'bluecoreExternalId'); + if (isDefinedAndNotNullAndNotEmpty(bluecoreExternalId)) { + return bluecoreExternalId; + } + let distinctId; + if (payload.event === 'identify') { + distinctId = getFieldValueFromMessage(message, 'userId'); + } else { + // email is always a more preferred distinct_id + distinctId = + getFieldValueFromMessage(message, 'email') || getFieldValueFromMessage(message, 'userId'); + } + + if (!isDefinedAndNotNullAndNotEmpty(distinctId)) { + // dev safe. AnonymouId should be always present + throw new InstrumentationError( + '[Bluecore] property:: distinct_id could not be set. Please provide either email or userId or anonymousId or externalId as distinct_id.', + ); + } + return distinctId; +}; + +module.exports = { + verifyPayload, + deduceTrackEventName, + normalizeProductArray, + isStandardBluecoreEvent, + constructProperties, + createProductForStandardEcommEvent, + populateAccurateDistinctId, +}; diff --git a/src/cdk/v2/destinations/bluecore/utils.test.js b/src/cdk/v2/destinations/bluecore/utils.test.js new file mode 100644 index 0000000000..829073bbcc --- /dev/null +++ b/src/cdk/v2/destinations/bluecore/utils.test.js @@ -0,0 +1,403 @@ +const { + normalizeProductArray, + verifyPayload, + isStandardBluecoreEvent, + deduceTrackEventName, + populateAccurateDistinctId, + createProductForStandardEcommEvent, +} = require('./utils'); +const { InstrumentationError } = require('@rudderstack/integrations-lib'); + +describe('normalizeProductArray', () => { + // Adds an array of products to a message when products array is defined and not null. + it('should add an array of products to a message when products array is defined and not null', () => { + const products = [ + { product_id: 1, name: 'Product 1' }, + { product_id: 2, name: 'Product 2' }, + ]; + const eventName = 'purchase'; + + const result = normalizeProductArray(products, eventName); + + expect(result).toEqual([ + { id: 1, name: 'Product 1' }, + { id: 2, name: 'Product 2' }, + ]); + }); + + // Adds a single product object to a message when a single product object is passed. + it('should add a single product object to a message when a single product object is passed', () => { + const product = { product_id: 1, name: 'Product 1' }; + const eventName = 'add_to_cart'; + + const result = normalizeProductArray(product, eventName); + expect(result).toEqual([{ id: 1, name: 'Product 1' }]); + }); + + it('should not throw an InstrumentationError for a custom event when products array is null', () => { + const message = {}; + const products = null; + const eventName = 'custom'; + + expect(() => { + normalizeProductArray(message, products, eventName); + }).toBeNull; + }); +}); + +describe('verifyPayload', () => { + // Verify payload for search event with search_term property. + it('should verify payload for search event with search_term property', () => { + const payload = { + event: 'search', + properties: { + search_term: 'example', + }, + }; + expect(() => verifyPayload(payload, {})).not.toThrow(); + }); + + // Verify payload for purchase event with order_id and total properties. + it('should verify payload for purchase event with order_id and total and customer properties', () => { + const payload = { + event: 'purchase', + properties: { + order_id: '123', + total: 100, + }, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); + + // Verify payload for identify event with email property. + it('should verify payload for identify event with email property', () => { + const payload = { + event: 'identify', + properties: { + customer: { + first_name: 'John', + }, + }, + }; + const message = { + traits: { + email: 'test@example.com', + }, + }; + expect(() => verifyPayload(payload, message)).not.toThrow(); + }); + + // Verify payload for search event without search_term property, should throw an InstrumentationError. + it('should throw an InstrumentationError when verifying payload for search event without search_term property', () => { + const payload = { + event: 'search', + properties: {}, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); + + // Verify payload for purchase event without order_id property, should throw an InstrumentationError. + it('should throw an InstrumentationError when verifying payload for purchase event without order_id property', () => { + const payload = { + event: 'purchase', + properties: { + total: 100, + }, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); + + // Verify payload for purchase event without total property, should throw an InstrumentationError. + it('should throw an InstrumentationError when verifying payload for purchase event without total property', () => { + const payload = { + event: 'purchase', + properties: { + order_id: '123', + }, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); + + // Verify payload for purchase event without total property, should throw an InstrumentationError. + it('should throw an InstrumentationError when verifying payload for identify event with action field other than identify', () => { + const payload = { + event: 'random', + properties: { + email: 'abc@gmail.com', + }, + }; + expect(() => + verifyPayload(payload, { type: 'identify', traits: { action: 'random' } }), + ).toThrow(InstrumentationError); + }); + + it('should throw an InstrumentationError when verifying payload for optin event without email property', () => { + const payload = { + event: 'optin', + properties: { + order_id: '123', + }, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); + + it('should throw an InstrumentationError when verifying payload for unsubscribe event without email property', () => { + const payload = { + event: 'unsubscribe', + properties: { + order_id: '123', + }, + }; + expect(() => verifyPayload(payload, {})).toThrow(InstrumentationError); + }); +}); + +describe('isStandardBluecoreEvent', () => { + // Returns true if the given event name is in the list of standard Bluecore events. + it('should return true when the given event name is in the list of standard Bluecore events', () => { + const eventName = 'search'; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(true); + }); + + // Returns false if the given event name is not in the list of standard Bluecore events. + it('should return false when the given event name is not in the list of standard Bluecore events', () => { + const eventName = 'someEvent'; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(false); + }); + + // Returns false if the given event name is null. + it('should return false when the given event name is null', () => { + const eventName = null; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(false); + }); + + // Returns false if the given event name is undefined. + it('should return false when the given event name is undefined', () => { + const eventName = undefined; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(false); + }); + + // Returns false if the given event name is not a string. + it('should return false when the given event name is not a string', () => { + const eventName = 123; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(false); + }); + + // Returns false if the given event name is an empty string. + it('should return false when the given event name is an empty string', () => { + const eventName = ''; + const result = isStandardBluecoreEvent(eventName); + expect(result).toBe(false); + }); +}); + +describe('deduceTrackEventName', () => { + // The function returns the trackEventName if no eventsMapping is provided and the trackEventName is not a standard Rudderstack ecommerce event. + it('should return the trackEventName when no eventsMapping is provided and the trackEventName is not a standard Rudderstack ecommerce event', () => { + const trackEventName = 'customEvent'; + const Config = { + eventsMapping: [], + }; + const result = deduceTrackEventName(trackEventName, Config); + expect(result).toEqual([trackEventName]); + }); + + // The function returns the corresponding event name from eventsMapping if the trackEventName is mapped to a standard bluecore event. + it('should return the corresponding event name from eventsMapping if the trackEventName is mapped to a standard bluecore event', () => { + const trackEventName = 'customEvent'; + const Config = { + eventsMapping: [{ from: 'customEvent', to: 'search' }], + }; + const result = deduceTrackEventName(trackEventName, Config); + expect(result).toEqual(['search']); + }); + + // The function returns the corresponding event name from eventsMapping if the trackEventName is mapped to a standard bluecore event. + it('should return the corresponding event name array from eventsMapping if the trackEventName is mapped to more than one standard bluecore events', () => { + const trackEventName = 'customEvent'; + const Config = { + eventsMapping: [ + { from: 'customEvent', to: 'search' }, + { from: 'customEvent', to: 'purchase' }, + ], + }; + const result = deduceTrackEventName(trackEventName, Config); + expect(result).toEqual(['search', 'purchase']); + }); + + // The function returns the corresponding standard Rudderstack ecommerce event name if the trackEventName is a standard bluecore event. + it('should return the corresponding standard Rudderstack ecommerce event name if the trackEventName is a standard bluecore event', () => { + const trackEventName = 'Product Added to Wishlist'; + const Config = { + eventsMapping: [], + }; + const result = deduceTrackEventName(trackEventName, Config); + expect(result).toEqual(['wishlist']); + }); + + // The function throws an error if the trackEventName is not a string. + it('should throw an error if the trackEventName is not a string', () => { + const trackEventName = 123; + const Config = { + eventsMapping: [], + }; + expect(() => deduceTrackEventName(trackEventName, Config)).toThrow(); + }); + + // The function throws an error if the trackEventName is an empty string. + it('should throw an error if the trackEventName is an empty string', () => { + const trackEventName = ''; + const Config = { + eventsMapping: [], + }; + expect(() => deduceTrackEventName(trackEventName, Config)).toThrow(); + }); +}); + +describe('populateAccurateDistinctId', () => { + // Returns the distinctId based on the email field when it exists in the message object and the event is not an identify event. + it('should return the distinctId based on the email field when it exists in the message object and the event is not an identify event', () => { + const payload = { event: 'event' }; + const message = { userId: '123', context: { traits: { email: 'test@example.com' } } }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('test@example.com'); + }); + + // Returns the distinctId based on the userId field when it exists in the message object and the event is an identify event. + it('should return the distinctId based on the userId field when it exists in the message object and the event is an identify event', () => { + const payload = { event: 'identify' }; + const message = { userId: '123', context: { traits: { email: 'test@example.com' } } }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('123'); + }); + + // Returns the distinctId based on the userId field when it exists in the message object and the email field does not exist and the event is not an identify event. + it('should return the distinctId based on the userId field when it exists in the message object and the email field does not exist and the event is not an identify event', () => { + const payload = { event: 'event' }; + const message = { userId: '123' }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('123'); + }); + + // Returns the distinctId based on the email field when it exists in the message object and the userId field is empty and the event is not an identify event. + it('should throw instrumenatation error as the message is malformed where email is at the root level', () => { + const payload = { event: 'event' }; + const message = { email: 'test@example.com', userId: '' }; + const testFn = () => populateAccurateDistinctId(payload, message); + expect(testFn).toThrow(InstrumentationError); + }); + + // Returns the distinctId based on the userId field when it exists in the message object and the email field is empty and the event is not an identify event. + it('should return the distinctId based on the userId field when it exists in the message object and the email field is empty and the event is not an identify event', () => { + const payload = { event: 'event' }; + const message = { email: '', userId: '123' }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('123'); + }); + + // Returns the distinctId based on the anonymousId field when it exists in the message object and the email and userId fields are empty and the event is not an identify event. + it('should return the distinctId based on the anonymousId field when it exists in the message object and the email and userId fields are empty and the event is not an identify event', () => { + const payload = { event: 'event' }; + const message = { anonymousId: 'abc' }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('abc'); + }); + + it('should return the distinctId based on the externalId field when it exists in the context object and the event is not an identify event', () => { + const payload = { event: 'event' }; + const message = { + userId: '123', + context: { + traits: { email: 'test@example.com' }, + externalId: [{ type: 'bluecoreExternalId', id: '54321' }], + }, + }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('54321'); + }); + + it('should return the distinctId based on the externalId field when it exists in the context object and the event is an identify event', () => { + const payload = { event: 'identify' }; + const message = { + userId: '123', + context: { + traits: { email: 'test@example.com' }, + externalId: [{ type: 'bluecoreExternalId', id: '54321' }], + }, + }; + const distinctId = populateAccurateDistinctId(payload, message); + expect(distinctId).toBe('54321'); + }); +}); + +describe('createProductForStandardEcommEvent', () => { + // Returns an array containing the properties if the event is a standard Bluecore event and not 'search'. + it("should return an array containing the properties when the event is a standard Bluecore event and not 'search'", () => { + const message = { + event: 'some event', + properties: { name: 'product 1' }, + }; + const eventName = 'some event'; + const result = createProductForStandardEcommEvent(message, eventName); + expect(result).toEqual(null); + }); + + // Returns null if the event is 'search'. + it("should return null when the event is 'search'", () => { + const message = { + event: 'search', + properties: { name: 'product 1' }, + }; + const eventName = 'search'; + const result = createProductForStandardEcommEvent(message, eventName); + expect(result).toBeNull(); + }); + + // Throws an InstrumentationError if the event is 'order completed' and the eventName is 'purchase'. + it("should throw an InstrumentationError when the event is 'order completed' and the eventName is 'purchase'", () => { + const message = { + event: 'order completed', + properties: { name: 'product 1' }, + }; + const eventName = 'purchase'; + expect(() => { + createProductForStandardEcommEvent(message, eventName); + }).toThrow(InstrumentationError); + }); + + // Returns null if the eventName is not a standard Bluecore event. + it('should return null when the eventName is not a standard Bluecore event', () => { + const message = { + event: 'some event', + properties: { name: 'product 1', products: [{ product_id: 1, name: 'prod1' }] }, + }; + const eventName = 'non-standard'; + const result = createProductForStandardEcommEvent(message, eventName); + expect(result).toBeNull(); + }); + + // Returns null if the eventName is not provided. + it('should return null when the eventName is not provided', () => { + const message = { + event: 'some event', + properties: { name: 'product 1' }, + }; + const result = createProductForStandardEcommEvent(message); + expect(result).toBeNull(); + }); + + // Returns null if the properties are not provided. + it('should return null when the properties are not provided', () => { + const message = { + event: 'some event', + }; + const eventName = 'some event'; + const result = createProductForStandardEcommEvent(message, eventName); + expect(result).toBeNull(); + }); +}); diff --git a/src/cdk/v2/destinations/the_trade_desk/config.js b/src/cdk/v2/destinations/the_trade_desk/config.js index 828bab3714..300325223b 100644 --- a/src/cdk/v2/destinations/the_trade_desk/config.js +++ b/src/cdk/v2/destinations/the_trade_desk/config.js @@ -1,6 +1,4 @@ -const { getMappingConfig } = require('../../../../v0/util'); - -const SUPPORTED_EVENT_TYPE = ['record', 'track']; +const SUPPORTED_EVENT_TYPE = ['record']; const ACTION_TYPES = ['insert', 'delete']; const DATA_PROVIDER_ID = 'rudderstack'; @@ -15,66 +13,10 @@ const DATA_SERVERS_BASE_ENDPOINTS_MAP = { china: 'https://data-cn2.adsrvr.cn', }; -// ref:- https://partner.thetradedesk.com/v3/portal/data/doc/DataConversionEventsApi -const REAL_TIME_CONVERSION_ENDPOINT = 'https://insight.adsrvr.org/track/realtimeconversion'; - -const CONVERSION_SUPPORTED_ID_TYPES = [ - 'TDID', - 'IDFA', - 'AAID', - 'DAID', - 'NAID', - 'IDL', - 'EUID', - 'UID2', -]; - -const ECOMM_EVENT_MAP = { - 'product added': { - event: 'addtocart', - rootLevelPriceSupported: true, - }, - 'order completed': { - event: 'purchase', - itemsArray: true, - revenueFieldSupported: true, - }, - 'product viewed': { - event: 'viewitem', - rootLevelPriceSupported: true, - }, - 'checkout started': { - event: 'startcheckout', - itemsArray: true, - revenueFieldSupported: true, - }, - 'cart viewed': { - event: 'viewcart', - itemsArray: true, - }, - 'product added to wishlist': { - event: 'wishlistitem', - rootLevelPriceSupported: true, - }, -}; - -const CONFIG_CATEGORIES = { - COMMON_CONFIGS: { name: 'TTDCommonConfig' }, - ITEM_CONFIGS: { name: 'TTDItemConfig' }, -}; - -const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname); - module.exports = { SUPPORTED_EVENT_TYPE, ACTION_TYPES, DATA_PROVIDER_ID, MAX_REQUEST_SIZE_IN_BYTES: 2500000, DATA_SERVERS_BASE_ENDPOINTS_MAP, - CONVERSION_SUPPORTED_ID_TYPES, - CONFIG_CATEGORIES, - COMMON_CONFIGS: MAPPING_CONFIG[CONFIG_CATEGORIES.COMMON_CONFIGS.name], - ITEM_CONFIGS: MAPPING_CONFIG[CONFIG_CATEGORIES.ITEM_CONFIGS.name], - ECOMM_EVENT_MAP, - REAL_TIME_CONVERSION_ENDPOINT, }; diff --git a/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml b/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml index ee05ecd967..5f0476dd62 100644 --- a/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml +++ b/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml @@ -3,20 +3,19 @@ bindings: path: ../../../../constants - name: processRecordInputs path: ./transformRecord - - name: processConversionInputs - path: ./transformConversion - name: handleRtTfSingleEventError path: ../../../../v0/util/index - name: InstrumentationError path: '@rudderstack/integrations-lib' steps: - - name: validateCommonConfig - description: | - validate common config for first party data and realtime conversion flow + - name: validateConfig template: | const config = ^[0].destination.Config + $.assertConfig(config.audienceId, "Segment name/Audience ID is not present. Aborting") $.assertConfig(config.advertiserId, "Advertiser ID is not present. Aborting") + $.assertConfig(config.advertiserSecretKey, "Advertiser Secret Key is not present. Aborting") + config.ttlInDays ? $.assertConfig(config.ttlInDays >=0 && config.ttlInDays <= 180, "TTL is out of range. Allowed values are 0 to 180 days") - name: validateInput template: | @@ -26,22 +25,17 @@ steps: template: | $.processRecordInputs(^.{.message.type === $.EventType.RECORD}[], ^[0].destination) - - name: processConversionEvents - template: | - $.processConversionInputs(^.{.message.type === $.EventType.TRACK}[]) - - name: failOtherEvents template: | - const otherEvents = ^.{.message.type !== $.EventType.TRACK && .message.type !== $.EventType.RECORD}[] + const otherEvents = ^.{.message.type !== $.EventType.RECORD}[] let failedEvents = otherEvents.map( function(event) { const error = new $.InstrumentationError("Event type " + event.message.type + " is not supported"); $.handleRtTfSingleEventError(event, error, {}) } ) - failedEvents ?? [] - name: finalPayload template: | - [...$.outputs.processRecordEvents, ...$.outputs.processConversionEvents, ...$.outputs.failOtherEvents] + [...$.outputs.processRecordEvents, ...$.outputs.failOtherEvents] diff --git a/src/cdk/v2/destinations/the_trade_desk/transformConversion.js b/src/cdk/v2/destinations/the_trade_desk/transformConversion.js deleted file mode 100644 index b282c43151..0000000000 --- a/src/cdk/v2/destinations/the_trade_desk/transformConversion.js +++ /dev/null @@ -1,98 +0,0 @@ -const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib'); -const { - defaultRequestConfig, - simpleProcessRouterDest, - defaultPostRequestConfig, - removeUndefinedAndNullValues, -} = require('../../../../v0/util'); -const { EventType } = require('../../../../constants'); -const { REAL_TIME_CONVERSION_ENDPOINT } = require('./config'); -const { - prepareFromConfig, - prepareCommonPayload, - getRevenue, - prepareItemsPayload, - getAdvertisingId, - prepareCustomProperties, - populateEventName, - getDataProcessingOptions, - getPrivacySetting, - enrichTrackPayload, -} = require('./utils'); - -const responseBuilder = (payload) => { - const response = defaultRequestConfig(); - response.endpoint = REAL_TIME_CONVERSION_ENDPOINT; - response.method = defaultPostRequestConfig.requestMethod; - response.body.JSON = payload; - return response; -}; - -const validateInputAndConfig = (message, destination) => { - const { Config } = destination; - if (!Config.trackerId) { - throw new ConfigurationError('Tracking Tag ID is not present. Aborting'); - } - - if (!message.type) { - throw new InstrumentationError('Event type is required'); - } - - const messageType = message.type.toLowerCase(); - if (messageType !== EventType.TRACK) { - throw new InstrumentationError(`Event type "${messageType}" is not supported`); - } - - if (!message.event) { - throw new InstrumentationError('Event name is not present. Aborting.'); - } -}; - -const prepareTrackPayload = (message, destination) => { - const configPayload = prepareFromConfig(destination); - const commonPayload = prepareCommonPayload(message); - // prepare items array - const items = prepareItemsPayload(message); - const { id, type } = getAdvertisingId(message); - // get td1-td10 custom properties - const customProperties = prepareCustomProperties(message, destination); - const eventName = populateEventName(message, destination); - const value = getRevenue(message); - let payload = { - ...configPayload, - ...commonPayload, - event_name: eventName, - value, - items, - adid: id, - adid_type: type, - ...customProperties, - data_processing_option: getDataProcessingOptions(message), - privacy_settings: getPrivacySetting(message), - }; - - payload = enrichTrackPayload(message, payload); - return { data: [removeUndefinedAndNullValues(payload)] }; -}; - -const trackResponseBuilder = (message, destination) => { - const payload = prepareTrackPayload(message, destination); - return responseBuilder(payload); -}; - -const processEvent = (message, destination) => { - validateInputAndConfig(message, destination); - return trackResponseBuilder(message, destination); -}; - -const process = (event) => processEvent(event.message, event.destination); - -const processConversionInputs = async (inputs, reqMetadata) => { - if (!inputs || inputs.length === 0) { - return []; - } - const respList = await simpleProcessRouterDest(inputs, process, reqMetadata); - return respList; -}; - -module.exports = { processConversionInputs }; diff --git a/src/cdk/v2/destinations/the_trade_desk/transformRecord.js b/src/cdk/v2/destinations/the_trade_desk/transformRecord.js index d571e11b7a..b452f8d7bc 100644 --- a/src/cdk/v2/destinations/the_trade_desk/transformRecord.js +++ b/src/cdk/v2/destinations/the_trade_desk/transformRecord.js @@ -1,4 +1,4 @@ -const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib'); +const { InstrumentationError } = require('@rudderstack/integrations-lib'); const { BatchUtils } = require('@rudderstack/workflow-engine'); const { defaultPostRequestConfig, @@ -13,20 +13,6 @@ const tradeDeskConfig = require('./config'); const { DATA_PROVIDER_ID } = tradeDeskConfig; -const validateConfig = (config) => { - if (!config.advertiserSecretKey) { - throw new ConfigurationError('Advertiser Secret Key is not present. Aborting'); - } - - if (config.ttlInDays && !(config.ttlInDays >= 0 && config.ttlInDays <= 180)) { - throw new ConfigurationError('TTL is out of range. Allowed values are 0 to 180 days'); - } - - if (!config.audienceId) { - throw new ConfigurationError('Segment name/Audience ID is not present. Aborting'); - } -}; - const responseBuilder = (items, config) => { const { advertiserId, dataServer } = config; @@ -64,8 +50,6 @@ const processRecordInputs = (inputs, destination) => { return []; } - validateConfig(Config); - const invalidActionTypeError = new InstrumentationError( 'Invalid action type. You can only add or remove IDs from the audience/segment', ); diff --git a/src/cdk/v2/destinations/the_trade_desk/utils.js b/src/cdk/v2/destinations/the_trade_desk/utils.js index f51d8dc3ff..71b479438c 100644 --- a/src/cdk/v2/destinations/the_trade_desk/utils.js +++ b/src/cdk/v2/destinations/the_trade_desk/utils.js @@ -1,28 +1,10 @@ -const lodash = require('lodash'); -const get = require('get-value'); const CryptoJS = require('crypto-js'); -const { InstrumentationError, AbortedError } = require('@rudderstack/integrations-lib'); -const { - constructPayload, - getHashFromArray, - isDefinedAndNotNull, - isAppleFamily, - getIntegrationsObj, - extractCustomFields, - generateExclusionList, -} = require('../../../../v0/util'); -const { - DATA_SERVERS_BASE_ENDPOINTS_MAP, - CONVERSION_SUPPORTED_ID_TYPES, - COMMON_CONFIGS, - ITEM_CONFIGS, - ECOMM_EVENT_MAP, -} = require('./config'); +const { AbortedError } = require('@rudderstack/integrations-lib'); +const { DATA_SERVERS_BASE_ENDPOINTS_MAP } = require('./config'); const getTTLInMin = (ttl) => parseInt(ttl, 10) * 1440; const getBaseEndpoint = (dataServer) => DATA_SERVERS_BASE_ENDPOINTS_MAP[dataServer]; const getFirstPartyEndpoint = (dataServer) => `${getBaseEndpoint(dataServer)}/data/advertiser`; -const prepareCommonPayload = (message) => constructPayload(message, COMMON_CONFIGS); /** * Generates a signature header for a given request using a secret key. @@ -41,300 +23,8 @@ const getSignatureHeader = (request, secretKey) => { return base; }; -const prepareFromConfig = (destination) => ({ - tracker_id: destination.Config?.trackerId, - adv: destination.Config?.advertiserId, -}); - -/** - * Calculates the revenue based on the given message. - * - * @param {Object} message - The message object containing the event and properties. - * @returns {number} - The calculated revenue. - * @throws {InstrumentationError} - If the event is 'Order Completed' and revenue is not provided. - */ -const getRevenue = (message) => { - const { event, properties } = message; - let revenue = properties?.value; - const eventsMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; - if (eventsMapInfo?.rootLevelPriceSupported) { - const { price, quantity = 1 } = properties; - if (price && !Number.isNaN(parseFloat(price)) && !Number.isNaN(parseInt(quantity, 10))) { - revenue = parseFloat(price) * parseInt(quantity, 10); - } - } else if (eventsMapInfo?.revenueFieldSupported) { - revenue = properties?.revenue || revenue; - if (event.toLowerCase() === 'order completed' && !revenue) { - throw new InstrumentationError('value is required for `Order Completed` event'); - } - } - - return revenue; -}; - -/** - * Generates items from properties of a given message. - * - * @param {Object} message - The message object containing properties. - * @returns {Array} - An array of items generated from the properties. - */ -const prepareItemsFromProperties = (message) => { - const { properties } = message; - const items = []; - const item = constructPayload(properties, ITEM_CONFIGS); - items.push(item); - return items; -}; - -/** - * Generates items payload from products. - * - * @param {Object} message - The message object. - * @returns {Array} - The items payload. - */ -const prepareItemsFromProducts = (message) => { - const products = get(message, 'properties.products'); - const items = []; - products.forEach((product) => { - const item = constructPayload(product, ITEM_CONFIGS); - const itemExclusionList = generateExclusionList(ITEM_CONFIGS); - extractCustomFields(product, item, 'root', itemExclusionList); - items.push(item); - }); - return items; -}; - -/** - * Generates items payload from root properties or products. - * - * @param {Object} message - The message object containing event and properties. - * @returns {Array} - The array of items payload. - */ -const prepareItemsPayload = (message) => { - const { event } = message; - let items; - const eventMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; - if (eventMapInfo?.itemsArray) { - // if event is one of the supported ecommerce events and products array is present - items = prepareItemsFromProducts(message); - } else if (eventMapInfo) { - // if event is one of the supported ecommerce events and products array is not present - items = prepareItemsFromProperties(message); - } - return items; -}; - -/** - * Retrieves the device advertising ID and type based on the provided message. - * - * @param {Object} message - The message object containing the context. - * @returns {Object} - An object containing the device advertising ID and type. - */ -const getDeviceAdvertisingId = (message) => { - const { context } = message; - const deviceId = context?.device?.advertisingId; - const osName = context?.os?.name?.toLowerCase(); - - let type; - switch (osName) { - case 'android': - type = 'AAID'; - break; - case 'windows': - type = 'NAID'; - break; - default: - type = isAppleFamily(osName) ? 'IDFA' : undefined; - break; - } - - return { deviceId, type }; -}; - -/** - * Retrieves the external ID object from the given message context. - * - * @param {Object} message - The message object containing the context. - * @returns {Object|undefined} - The external ID object, or undefined if not found. - */ -const getDestinationExternalIDObject = (message) => { - const { context } = message; - const externalIdArray = context?.externalId || []; - - let externalIdObj; - - if (Array.isArray(externalIdArray)) { - externalIdObj = externalIdArray.find( - (extIdObj) => - CONVERSION_SUPPORTED_ID_TYPES.includes(extIdObj?.type?.toUpperCase()) && extIdObj?.id, - ); - } - return externalIdObj; -}; - -/** - * Retrieves the advertising ID and type from the given message. - * - * @param {Object} message - The message object containing the context. - * @returns {Object} - An object containing the advertising ID and type. - * If the advertising ID and type are found in the device context, they are returned. - * If not, the external ID object is checked and if found, its ID and type are returned. - * If neither the device context nor the external ID object contain the required information, - * an object with null values for ID and type is returned. - */ -const getAdvertisingId = (message) => { - const { deviceId, type } = getDeviceAdvertisingId(message); - if (deviceId && type) { - return { id: deviceId, type }; - } - const externalIdObj = getDestinationExternalIDObject(message); - if (externalIdObj?.id && externalIdObj?.type) { - return { id: externalIdObj.id, type: externalIdObj.type.toUpperCase() }; - } - - return { id: null, type: null }; -}; - -/** - * Prepares custom properties (td1-td10) for a given message and destination. - * - * @param {object} message - The message object. - * @param {object} destination - The destination object. - * @returns {object} - The prepared payload object. - */ -const prepareCustomProperties = (message, destination) => { - const { customProperties } = destination.Config; - const payload = {}; - if (customProperties) { - customProperties.forEach((customProperty) => { - const { rudderProperty, tradeDeskProperty } = customProperty; - const value = get(message, rudderProperty); - if (value) { - payload[tradeDeskProperty] = value; - // unset the rudder property from the message, since it is already mapped to a trade desk property - lodash.unset(message, rudderProperty); - } - }); - } - return payload; -}; - -/** - * Retrieves the event name based on the provided message and destination. - * - * @param {object} message - The message object containing the event. - * @param {object} destination - The destination object containing the events mapping configuration. - * @returns {string} - The event name. - */ -const populateEventName = (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'); - eventName = keyMap[event.toLowerCase()]; - } - - if (eventName) { - return eventName; - } - - // if event is one of the supported ecommerce events, use the mapped trade desk event name - const eventMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; - if (isDefinedAndNotNull(eventMapInfo)) { - return eventMapInfo.event; - } - - // else return the event name as it is - return event; -}; - -/** - * Retrieves the data processing options based on the provided message. - * - * @param {string} message - The message to process. - * @throws {InstrumentationError} - Throws an error if the region is not supported, if no policies are provided, if multiple policies are provided, or if the policy is not supported. - * @returns {Object} - The data processing options, including the policies and region. - */ -const getDataProcessingOptions = (message) => { - const integrationObj = getIntegrationsObj(message, 'THE_TRADE_DESK') || {}; - let { policies } = integrationObj; - const { region } = integrationObj; - let dataProcessingOptions; - - if (region && !region.toLowerCase().startsWith('us')) { - throw new InstrumentationError('Only US states are supported'); - } - - if (!policies || (Array.isArray(policies) && policies.length === 0)) { - policies = ['LDU']; - } - - if (policies.length > 1) { - throw new InstrumentationError('Only one policy is allowed'); - } - - if (policies[0] !== 'LDU') { - throw new InstrumentationError('Only LDU policy is supported'); - } - - if (policies && region) { - dataProcessingOptions = { policies, region }; - } - - return dataProcessingOptions; -}; - -const getPrivacySetting = (message) => { - const integrationObj = getIntegrationsObj(message, 'THE_TRADE_DESK'); - return integrationObj?.privacy_settings; -}; - -/** - * Enriches the track payload with extra properties present in 'properties' other than the ones defined in TTDCommonConfig.json and TTDItemConfig.json - * - * @param {Object} message - The message object containing the event information. - * @param {Object} payload - The payload object to be enriched. - * @returns {Object} - The enriched payload object. - */ -const enrichTrackPayload = (message, payload) => { - let rawPayload = { ...payload }; - const eventsMapInfo = ECOMM_EVENT_MAP[message.event.toLowerCase()]; - // checking if event is an ecomm one and itemsArray/products support is not present. e.g Product Added event - if (eventsMapInfo && !eventsMapInfo.itemsArray) { - const itemExclusionList = generateExclusionList(ITEM_CONFIGS); - rawPayload = extractCustomFields(message, rawPayload, ['properties'], itemExclusionList); - } else if (eventsMapInfo) { - // for ecomm events with products array supports. e.g Order Completed event - rawPayload = extractCustomFields( - message, - rawPayload, - ['properties'], - ['products', 'revenue', 'value'], - ); - } else { - // for custom events - rawPayload = extractCustomFields(message, rawPayload, ['properties'], ['value']); - } - return rawPayload; -}; - module.exports = { getTTLInMin, getFirstPartyEndpoint, getSignatureHeader, - prepareFromConfig, - getRevenue, - prepareCommonPayload, - prepareItemsPayload, - getDeviceAdvertisingId, - getDestinationExternalIDObject, - getAdvertisingId, - prepareCustomProperties, - populateEventName, - getDataProcessingOptions, - getPrivacySetting, - enrichTrackPayload, }; diff --git a/src/cdk/v2/destinations/the_trade_desk/utils.test.js b/src/cdk/v2/destinations/the_trade_desk/utils.test.js index 029c3004ae..81fd7cf17d 100644 --- a/src/cdk/v2/destinations/the_trade_desk/utils.test.js +++ b/src/cdk/v2/destinations/the_trade_desk/utils.test.js @@ -1,16 +1,5 @@ -const { AbortedError, InstrumentationError } = require('@rudderstack/integrations-lib'); -const { - getSignatureHeader, - getRevenue, - getDeviceAdvertisingId, - getDestinationExternalIDObject, - getAdvertisingId, - prepareCustomProperties, - populateEventName, - getDataProcessingOptions, - getPrivacySetting, - enrichTrackPayload, -} = require('./utils'); +const { AbortedError } = require('@rudderstack/integrations-lib'); +const { getSignatureHeader } = require('./utils'); describe('getSignatureHeader', () => { it('should calculate the signature header for a valid request and secret key', () => { @@ -58,612 +47,3 @@ describe('getSignatureHeader', () => { }).toThrow(AbortedError); }); }); - -describe('getRevenue', () => { - it('should return revenue value from message properties for custom events', () => { - const message = { - event: 'customEvent', - properties: { - value: 100, - }, - }; - const result = getRevenue(message); - expect(result).toBe(100); - }); - - it('should calculate revenue based on price and quantity from message properties if ecomm event is supported for price calculation', () => { - const message = { - event: 'Product Added', - properties: { - price: 10, - quantity: 5, - }, - }; - const result = getRevenue(message); - expect(result).toBe(50); - }); - - it('should return revenue value from message properties if ecomm event is supported for revenue calculation', () => { - const message = { - event: 'Order Completed', - properties: { - revenue: 200, - }, - }; - const result = getRevenue(message); - expect(result).toBe(200); - }); - - it('should return default revenue value from properties.value for ecomm events', () => { - let message = { - event: 'Product Added', - properties: { - price: '', - value: 200, - }, - }; - let result = getRevenue(message); - expect(result).toBe(200); - - message = { - event: 'Order Completed', - properties: { - value: 200, - }, - }; - result = getRevenue(message); - expect(result).toBe(200); - }); - - it('should throw an Instrumentation error if revenue is missing for `Order Completed` event', () => { - const message = { - event: 'Order Completed', - properties: {}, - }; - expect(() => { - getRevenue(message); - }).toThrow(InstrumentationError); - }); -}); - -describe('getDeviceAdvertisingId', () => { - it('should return an object with deviceId and type properties when context.device.advertisingId and context.os.name are present', () => { - let message = { - context: { - device: { - advertisingId: '123456789', - }, - os: { - name: 'android', - }, - }, - }; - let result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: '123456789', type: 'AAID' }); - - message = { - context: { - device: { - advertisingId: '123456789', - }, - os: { - name: 'ios', - }, - }, - }; - result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: '123456789', type: 'IDFA' }); - - message = { - context: { - device: { - advertisingId: '123456789', - }, - os: { - name: 'windows', - }, - }, - }; - result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: '123456789', type: 'NAID' }); - }); - - it('should return an object with undefined type property when osName is not "android", "windows", or an Apple OS', () => { - const message = { - context: { - device: { - advertisingId: '123456789', - }, - os: { - name: 'linux', - }, - }, - }; - const result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: '123456789', type: undefined }); - }); - - it('should return an object with undefined deviceId and type properties when context is undefined', () => { - let message = {}; - let result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: undefined, type: undefined }); - - message = { - context: {}, - }; - result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: undefined, type: undefined }); - - message = { - context: { - device: {}, - }, - }; - result = getDeviceAdvertisingId(message); - expect(result).toEqual({ deviceId: undefined, type: undefined }); - }); -}); - -describe('getDestinationExternalIDObject', () => { - it('should return the external ID object when it exists in the message context', () => { - const message = { - context: { - externalId: [ - { id: '123', type: 'daid' }, - { id: '456', type: 'type123' }, - ], - }, - }; - const result = getDestinationExternalIDObject(message); - expect(result).toEqual({ id: '123', type: 'daid' }); - }); - - it('should return undefined when no external ID object exists in the message context', () => { - let message = { - context: { - externalId: [], - }, - }; - let result = getDestinationExternalIDObject(message); - expect(result).toBeUndefined(); - - message = { - context: {}, - }; - result = getDestinationExternalIDObject(message); - expect(result).toBeUndefined(); - }); - - it('should return the first matching external ID object in the array', () => { - const message = { - context: { - externalId: [ - { id: '', type: 'daid' }, - { id: '456', type: 'tdid' }, - { id: '789', type: 'UID2' }, - ], - }, - }; - const result = getDestinationExternalIDObject(message); - expect(result).toEqual({ id: '456', type: 'tdid' }); - }); -}); - -describe('getAdvertisingId', () => { - it('should return an object with the ID and type when the message contains a valid device advertising ID and OS type', () => { - const message = { - context: { - device: { - advertisingId: '1234567890', - }, - os: { - name: 'android', - }, - }, - }; - - const result = getAdvertisingId(message); - expect(result).toEqual({ id: '1234567890', type: 'AAID' }); - }); - - it('should return an object with the ID and type when the message contains a valid external ID object with a supported type', () => { - const message = { - context: { - externalId: [ - { - type: 'IDFA', - id: 'abcdefg', - }, - ], - }, - }; - - const result = getAdvertisingId(message); - expect(result).toEqual({ id: 'abcdefg', type: 'IDFA' }); - }); - - it('should return an object with undefined ID and type when the message contains a valid external ID object with an unsupported type', () => { - let message = { - context: { - externalId: [ - { - type: 'unsupported', - id: '1234567890', - }, - ], - }, - }; - - let result = getAdvertisingId(message); - expect(result).toEqual({ id: null, type: null }); - - message = { - context: { - device: { - advertisingId: '1234567890', - }, - }, - }; - - result = getAdvertisingId(message); - expect(result).toEqual({ id: null, type: null }); - }); - - it('should return an object with undefined ID and type when the message contains an external ID object with a supported type but no ID or missing externalId', () => { - let message = { - context: { - externalId: [ - { - type: 'IDFA', - }, - ], - }, - }; - let result = getAdvertisingId(message); - expect(result).toEqual({ id: null, type: null }); - - message = { - context: {}, - }; - result = getAdvertisingId(message); - expect(result).toEqual({ id: null, type: null }); - }); -}); - -describe('prepareCustomProperties', () => { - it('should return an empty object when customProperties is an empty array', () => { - const message = {}; - let destination = { Config: { customProperties: [] } }; - let result = prepareCustomProperties(message, destination); - expect(result).toEqual({}); - - destination = { Config: { customProperties: [{ rudderProperty: '', tradeDeskProperty: '' }] } }; - result = prepareCustomProperties(message, destination); - expect(result).toEqual({}); - - destination = { Config: { customProperties: undefined } }; - result = prepareCustomProperties(message, destination); - expect(result).toEqual({}); - }); - - it('should return an object with `tradeDeskProperty` as key and `rudderProperty` value as value when `rudderProperty` exists in message', () => { - const message = { - rudderProperty1: 'value1', - rudderProperty2: 'value2', - }; - const destination = { - Config: { - customProperties: [ - { - rudderProperty: 'rudderProperty1', - tradeDeskProperty: 'tradeDeskProperty1', - }, - { - rudderProperty: 'rudderProperty2', - tradeDeskProperty: 'tradeDeskProperty2', - }, - { - rudderProperty: 'rudderProperty3', - tradeDeskProperty: 'tradeDeskProperty3', - }, - ], - }, - }; - const result = prepareCustomProperties(message, destination); - expect(result).toEqual({ - tradeDeskProperty1: 'value1', - tradeDeskProperty2: 'value2', - }); - }); -}); - -describe('populateEventName', () => { - it('should return the eventName if it exists in the eventsMapping of destination.Config', () => { - const message = { event: 'someEvent' }; - const destination = { Config: { eventsMapping: [{ from: 'someEvent', to: 'mappedEvent' }] } }; - const result = populateEventName(message, destination); - expect(result).toBe('mappedEvent'); - }); - - it('should return the eventName if it exists in the ECOMM_EVENT_MAP', () => { - const message = { event: 'product added' }; - let destination = { Config: { eventsMapping: [{ from: 'someEvent', to: 'mappedEvent' }] } }; - let result = populateEventName(message, destination); - expect(result).toBe('addtocart'); - - destination = { Config: { eventsMapping: [] } }; - result = populateEventName(message, destination); - expect(result).toBe('addtocart'); - }); - - it('should return undefined if eventsMapping is an empty array', () => { - const message = { event: 'someEvent' }; - const destination = { Config: { eventsMapping: [] } }; - const result = populateEventName(message, destination); - expect(result).toBe('someEvent'); - }); -}); - -describe('getDataProcessingOptions', () => { - it('should return an object with policies and region when provided a integrationObj in message', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - policies: ['LDU'], - region: 'US-CO', - }, - }, - }; - const expected = { - policies: ['LDU'], - region: 'US-CO', - }; - const result = getDataProcessingOptions(message); - expect(result).toEqual(expected); - }); - - it('should throw an InstrumentationError if the region is not a US state', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - policies: ['LDU'], - region: 'EU-abc', - }, - }, - }; - expect(() => { - getDataProcessingOptions(message); - }).toThrow(InstrumentationError); - }); - - it('should throw an InstrumentationError if multiple policies are provided', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - policies: ['LDU', 'Policy2'], - region: 'US-CO', - }, - }, - }; - - expect(() => { - getDataProcessingOptions(message); - }).toThrow(InstrumentationError); - }); - - it('should throw an InstrumentationError if a policy other than `LDU` is provided', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - policies: ['Policy1'], - region: 'US-CO', - }, - }, - }; - - expect(() => { - getDataProcessingOptions(message); - }).toThrow(InstrumentationError); - }); - - it('should return an object with default policy `LDU` when policies are not provided', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - policies: [], - region: 'US-CO', - }, - }, - }; - - const expected = { - policies: ['LDU'], - region: 'US-CO', - }; - - expect(getDataProcessingOptions(message)).toEqual(expected); - }); - - it('should handle empty cases', () => { - let message = { - integrations: { - All: true, - THE_TRADE_DESK: {}, - }, - }; - - expect(getDataProcessingOptions(message)).toBeUndefined(); - - message = { - integrations: { - All: true, - }, - }; - - expect(getDataProcessingOptions(message)).toBeUndefined(); - - message = { - integrations: { - All: true, - THE_TRADE_DESK: { region: 'US-CO' }, - }, - }; - - expect(getDataProcessingOptions(message)).toEqual({ policies: ['LDU'], region: 'US-CO' }); - }); -}); - -describe('getPrivacySetting', () => { - it('should return the privacy settings object when it exists in the integration object', () => { - const message = { - integrations: { - All: true, - THE_TRADE_DESK: { - privacy_settings: [ - { - privacy_type: 'GDPR', - is_applicable: 1, - consent_string: 'ok', - }, - ], - }, - }, - }; - const expected = [ - { - privacy_type: 'GDPR', - is_applicable: 1, - consent_string: 'ok', - }, - ]; - const result = getPrivacySetting(message); - expect(result).toEqual(expected); - }); - - it('should return null when the privacy settings object does not exist in the integration object', () => { - let message = { integrations: {} }; - expect(getPrivacySetting(message)).toBeUndefined(); - - message = { integrations: { THE_TRADE_DESK: {} } }; - expect(getPrivacySetting(message)).toBeUndefined(); - - message = { integrations: { THE_TRADE_DESK: { privacy_settings: null } } }; - expect(getPrivacySetting(message)).toBeNull(); - }); -}); - -describe('enrichTrackPayload', () => { - it('should correctly enrich the payload with custom fields for ecomm events where product array is not supported', () => { - const message = { - event: 'Product Added', - properties: { - product_id: 'prd123', - sku: 'sku123', - brand: 'brand123', - property1: 'value1', - property2: 'value2', - }, - }; - const payload = { - items: [ - { - item_code: 'prd123', - }, - ], - property1: 'value1', - property2: 'value2', - }; - const expectedPayload = { - items: [ - { - item_code: 'prd123', - }, - ], - brand: 'brand123', - property1: 'value1', - property2: 'value2', - }; - - const result = enrichTrackPayload(message, payload); - expect(result).toEqual(expectedPayload); - }); - - it('should correctly enrich the payload with custom fields when the for ecomm events with products array support', () => { - const message = { - event: 'order completed', - properties: { - order_id: 'ord123', - total: 52.0, - subtotal: 45.0, - revenue: 50.0, - products: [{ product_id: 'prd123', sku: 'sku123', brand: 'brand123' }], - property1: 'value1', - property2: 'value2', - }, - }; - const payload = { - order_id: 'ord123', - value: 50.0, - items: [{ item_code: 'prd123', brand: 'brand123' }], - property1: 'value1', - property2: 'value2', - }; - const expectedPayload = { - order_id: 'ord123', - total: 52.0, - subtotal: 45.0, - value: 50.0, - items: [{ item_code: 'prd123', brand: 'brand123' }], - property1: 'value1', - property2: 'value2', - }; - - const result = enrichTrackPayload(message, payload); - - expect(result).toEqual(expectedPayload); - }); - - it('should return the enriched payload for custom event', () => { - const message = { - event: 'someEvent', - properties: { - order_id: 'ord123', - property1: 'value1', - property2: 'value2', - revenue: 10, - value: 11, - products: [ - { - product_id: 'prd123', - test: 'test', - }, - ], - }, - }; - const payload = { - order_id: 'ord123', - value: 11, - }; - const expectedPayload = { - order_id: 'ord123', - property1: 'value1', - property2: 'value2', - revenue: 10, - value: 11, - products: [ - { - product_id: 'prd123', - test: 'test', - }, - ], - }; - - const result = enrichTrackPayload(message, payload); - expect(result).toEqual(expectedPayload); - }); -}); diff --git a/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/config.js b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/config.js new file mode 100644 index 0000000000..7af732185f --- /dev/null +++ b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/config.js @@ -0,0 +1,63 @@ +const { getMappingConfig } = require('../../../../v0/util'); + +const SUPPORTED_EVENT_TYPE = ['track']; + +// ref:- https://partner.thetradedesk.com/v3/portal/data/doc/DataConversionEventsApi +const REAL_TIME_CONVERSION_ENDPOINT = 'https://insight.adsrvr.org/track/realtimeconversion'; + +const CONVERSION_SUPPORTED_ID_TYPES = [ + 'TDID', + 'IDFA', + 'AAID', + 'DAID', + 'NAID', + 'IDL', + 'EUID', + 'UID2', +]; + +const ECOMM_EVENT_MAP = { + 'product added': { + event: 'addtocart', + rootLevelPriceSupported: true, + }, + 'order completed': { + event: 'purchase', + itemsArray: true, + revenueFieldSupported: true, + }, + 'product viewed': { + event: 'viewitem', + rootLevelPriceSupported: true, + }, + 'checkout started': { + event: 'startcheckout', + itemsArray: true, + revenueFieldSupported: true, + }, + 'cart viewed': { + event: 'viewcart', + itemsArray: true, + }, + 'product added to wishlist': { + event: 'wishlistitem', + rootLevelPriceSupported: true, + }, +}; + +const CONFIG_CATEGORIES = { + COMMON_CONFIGS: { name: 'TTDCommonConfig' }, + ITEM_CONFIGS: { name: 'TTDItemConfig' }, +}; + +const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname); + +module.exports = { + SUPPORTED_EVENT_TYPE, + CONFIG_CATEGORIES, + CONVERSION_SUPPORTED_ID_TYPES, + COMMON_CONFIGS: MAPPING_CONFIG[CONFIG_CATEGORIES.COMMON_CONFIGS.name], + ITEM_CONFIGS: MAPPING_CONFIG[CONFIG_CATEGORIES.ITEM_CONFIGS.name], + ECOMM_EVENT_MAP, + REAL_TIME_CONVERSION_ENDPOINT, +}; diff --git a/src/cdk/v2/destinations/the_trade_desk/data/TTDCommonConfig.json b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/data/TTDCommonConfig.json similarity index 100% rename from src/cdk/v2/destinations/the_trade_desk/data/TTDCommonConfig.json rename to src/cdk/v2/destinations/the_trade_desk_real_time_conversions/data/TTDCommonConfig.json diff --git a/src/cdk/v2/destinations/the_trade_desk/data/TTDItemConfig.json b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/data/TTDItemConfig.json similarity index 100% rename from src/cdk/v2/destinations/the_trade_desk/data/TTDItemConfig.json rename to src/cdk/v2/destinations/the_trade_desk_real_time_conversions/data/TTDItemConfig.json diff --git a/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/procWorkflow.yaml b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/procWorkflow.yaml new file mode 100644 index 0000000000..5191320cdc --- /dev/null +++ b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/procWorkflow.yaml @@ -0,0 +1,59 @@ +bindings: + - name: EventType + path: ../../../../constants + - path: ../../bindings/jsontemplate + - name: defaultRequestConfig + path: ../../../../v0/util + - name: removeUndefinedAndNullValues + path: ../../../../v0/util + - path: ./config + exportAll: true + - path: ./utils + exportAll: true + +steps: + - name: validateConfig + template: | + $.assertConfig(.destination.Config.advertiserId, "Advertiser ID is not present. Aborting") + $.assertConfig(.destination.Config.trackerId, "Tracking Tag ID is not present. Aborting") + + - name: validateInput + template: | + let messageType = .message.type; + $.assert(messageType, "message Type is not present. Aborting."); + $.assert(messageType.toLowerCase() === $.EventType.TRACK, "Event type " + messageType + " is not supported"); + $.assert(.message.event, "Event is not present. Aborting."); + + - name: prepareTrackPayload + template: | + const configPayload = $.prepareFromConfig(.destination); + const commonPayload = $.prepareCommonPayload(.message); + const { id, type } = $.getAdvertisingId(.message); + const items = $.prepareItemsPayload(.message); + const customProperties = $.prepareCustomProperties(.message, .destination); + const eventName = $.populateEventName(.message, .destination); + const value = $.getRevenue(.message); + let payload = { + ...configPayload, + ...commonPayload, + event_name: eventName, + value, + items, + adid: id, + adid_type: type, + ...customProperties, + data_processing_option: $.getDataProcessingOptions(.message), + privacy_settings: $.getPrivacySetting(.message), + }; + payload = $.enrichTrackPayload(.message, payload); + payload; + + - name: buildResponseForProcessTransformation + template: | + const response = $.defaultRequestConfig(); + response.body.JSON = {data: [$.removeUndefinedAndNullValues($.outputs.prepareTrackPayload)]}; + response.endpoint = $.REAL_TIME_CONVERSION_ENDPOINT; + response.headers = { + "Content-Type": "application/json" + }; + response; diff --git a/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.js b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.js new file mode 100644 index 0000000000..2232be61f0 --- /dev/null +++ b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.js @@ -0,0 +1,315 @@ +const lodash = require('lodash'); +const get = require('get-value'); +const { InstrumentationError } = require('@rudderstack/integrations-lib'); +const { + constructPayload, + getHashFromArray, + isDefinedAndNotNull, + isAppleFamily, + getIntegrationsObj, + extractCustomFields, + generateExclusionList, +} = require('../../../../v0/util'); +const { + CONVERSION_SUPPORTED_ID_TYPES, + COMMON_CONFIGS, + ITEM_CONFIGS, + ECOMM_EVENT_MAP, +} = require('./config'); + +const prepareCommonPayload = (message) => constructPayload(message, COMMON_CONFIGS); + +const prepareFromConfig = (destination) => ({ + tracker_id: destination.Config?.trackerId, + adv: destination.Config?.advertiserId, +}); + +/** + * Calculates the revenue based on the given message. + * + * @param {Object} message - The message object containing the event and properties. + * @returns {number} - The calculated revenue. + * @throws {InstrumentationError} - If the event is 'Order Completed' and revenue is not provided. + */ +const getRevenue = (message) => { + const { event, properties } = message; + let revenue = properties?.value; + const eventsMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; + if (eventsMapInfo?.rootLevelPriceSupported) { + const { price, quantity = 1 } = properties; + if (price && !Number.isNaN(parseFloat(price)) && !Number.isNaN(parseInt(quantity, 10))) { + revenue = parseFloat(price) * parseInt(quantity, 10); + } + } else if (eventsMapInfo?.revenueFieldSupported) { + revenue = properties?.revenue || revenue; + if (event.toLowerCase() === 'order completed' && !revenue) { + throw new InstrumentationError('value is required for `Order Completed` event'); + } + } + + return revenue; +}; + +/** + * Generates items from properties of a given message. + * + * @param {Object} message - The message object containing properties. + * @returns {Array} - An array of items generated from the properties. + */ +const prepareItemsFromProperties = (message) => { + const { properties } = message; + const items = []; + const item = constructPayload(properties, ITEM_CONFIGS); + items.push(item); + return items; +}; + +/** + * Generates items payload from products. + * + * @param {Object} message - The message object. + * @returns {Array} - The items payload. + */ +const prepareItemsFromProducts = (message) => { + const products = get(message, 'properties.products'); + const items = []; + products.forEach((product) => { + const item = constructPayload(product, ITEM_CONFIGS); + const itemExclusionList = generateExclusionList(ITEM_CONFIGS); + extractCustomFields(product, item, 'root', itemExclusionList); + items.push(item); + }); + return items; +}; + +/** + * Generates items payload from root properties or products. + * + * @param {Object} message - The message object containing event and properties. + * @returns {Array} - The array of items payload. + */ +const prepareItemsPayload = (message) => { + const { event } = message; + let items; + const eventMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; + if (eventMapInfo?.itemsArray) { + // if event is one of the supported ecommerce events and products array is present + items = prepareItemsFromProducts(message); + } else if (eventMapInfo) { + // if event is one of the supported ecommerce events and products array is not present + items = prepareItemsFromProperties(message); + } + return items; +}; + +/** + * Retrieves the device advertising ID and type based on the provided message. + * + * @param {Object} message - The message object containing the context. + * @returns {Object} - An object containing the device advertising ID and type. + */ +const getDeviceAdvertisingId = (message) => { + const { context } = message; + const deviceId = context?.device?.advertisingId; + const osName = context?.os?.name?.toLowerCase(); + + let type; + switch (osName) { + case 'android': + type = 'AAID'; + break; + case 'windows': + type = 'NAID'; + break; + default: + type = isAppleFamily(osName) ? 'IDFA' : undefined; + break; + } + + return { deviceId, type }; +}; + +/** + * Retrieves the external ID object from the given message context. + * + * @param {Object} message - The message object containing the context. + * @returns {Object|undefined} - The external ID object, or undefined if not found. + */ +const getDestinationExternalIDObject = (message) => { + const { context } = message; + const externalIdArray = context?.externalId || []; + + let externalIdObj; + + if (Array.isArray(externalIdArray)) { + externalIdObj = externalIdArray.find( + (extIdObj) => + CONVERSION_SUPPORTED_ID_TYPES.includes(extIdObj?.type?.toUpperCase()) && extIdObj?.id, + ); + } + return externalIdObj; +}; + +/** + * Retrieves the advertising ID and type from the given message. + * + * @param {Object} message - The message object containing the context. + * @returns {Object} - An object containing the advertising ID and type. + * If the advertising ID and type are found in the device context, they are returned. + * If not, the external ID object is checked and if found, its ID and type are returned. + * If neither the device context nor the external ID object contain the required information, + * an object with null values for ID and type is returned. + */ +const getAdvertisingId = (message) => { + const { deviceId, type } = getDeviceAdvertisingId(message); + if (deviceId && type) { + return { id: deviceId, type }; + } + const externalIdObj = getDestinationExternalIDObject(message); + if (externalIdObj?.id && externalIdObj?.type) { + return { id: externalIdObj.id, type: externalIdObj.type.toUpperCase() }; + } + + return { id: null, type: null }; +}; + +/** + * Prepares custom properties (td1-td10) for a given message and destination. + * + * @param {object} message - The message object. + * @param {object} destination - The destination object. + * @returns {object} - The prepared payload object. + */ +const prepareCustomProperties = (message, destination) => { + const { customProperties } = destination.Config; + const payload = {}; + if (customProperties) { + customProperties.forEach((customProperty) => { + const { rudderProperty, tradeDeskProperty } = customProperty; + const value = get(message, rudderProperty); + if (value) { + payload[tradeDeskProperty] = value; + // unset the rudder property from the message, since it is already mapped to a trade desk property + lodash.unset(message, rudderProperty); + } + }); + } + return payload; +}; + +/** + * Retrieves the event name based on the provided message and destination. + * + * @param {object} message - The message object containing the event. + * @param {object} destination - The destination object containing the events mapping configuration. + * @returns {string} - The event name. + */ +const populateEventName = (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'); + eventName = keyMap[event.toLowerCase()]; + } + + if (eventName) { + return eventName; + } + + // if event is one of the supported ecommerce events, use the mapped trade desk event name + const eventMapInfo = ECOMM_EVENT_MAP[event.toLowerCase()]; + if (isDefinedAndNotNull(eventMapInfo)) { + return eventMapInfo.event; + } + + // else return the event name as it is + return event; +}; + +/** + * Retrieves the data processing options based on the provided message. + * + * @param {string} message - The message to process. + * @throws {InstrumentationError} - Throws an error if the region is not supported, if no policies are provided, if multiple policies are provided, or if the policy is not supported. + * @returns {Object} - The data processing options, including the policies and region. + */ +const getDataProcessingOptions = (message) => { + const integrationObj = getIntegrationsObj(message, 'THE_TRADE_DESK') || {}; + let { policies } = integrationObj; + const { region } = integrationObj; + let dataProcessingOptions; + + if (region && !region.toLowerCase().startsWith('us')) { + throw new InstrumentationError('Only US states are supported'); + } + + if (!policies || (Array.isArray(policies) && policies.length === 0)) { + policies = ['LDU']; + } + + if (policies.length > 1) { + throw new InstrumentationError('Only one policy is allowed'); + } + + if (policies[0] !== 'LDU') { + throw new InstrumentationError('Only LDU policy is supported'); + } + + if (policies && region) { + dataProcessingOptions = { policies, region }; + } + + return dataProcessingOptions; +}; + +const getPrivacySetting = (message) => { + const integrationObj = getIntegrationsObj(message, 'THE_TRADE_DESK'); + return integrationObj?.privacy_settings; +}; + +/** + * Enriches the track payload with extra properties present in 'properties' other than the ones defined in TTDCommonConfig.json and TTDItemConfig.json + * + * @param {Object} message - The message object containing the event information. + * @param {Object} payload - The payload object to be enriched. + * @returns {Object} - The enriched payload object. + */ +const enrichTrackPayload = (message, payload) => { + let rawPayload = { ...payload }; + const eventsMapInfo = ECOMM_EVENT_MAP[message.event.toLowerCase()]; + // checking if event is an ecomm one and itemsArray/products support is not present. e.g Product Added event + if (eventsMapInfo && !eventsMapInfo.itemsArray) { + const itemExclusionList = generateExclusionList(ITEM_CONFIGS); + rawPayload = extractCustomFields(message, rawPayload, ['properties'], itemExclusionList); + } else if (eventsMapInfo) { + // for ecomm events with products array supports. e.g Order Completed event + rawPayload = extractCustomFields( + message, + rawPayload, + ['properties'], + ['products', 'revenue', 'value'], + ); + } else { + // for custom events + rawPayload = extractCustomFields(message, rawPayload, ['properties'], ['value']); + } + return rawPayload; +}; + +module.exports = { + prepareFromConfig, + getRevenue, + prepareCommonPayload, + prepareItemsPayload, + getDeviceAdvertisingId, + getDestinationExternalIDObject, + getAdvertisingId, + prepareCustomProperties, + populateEventName, + getDataProcessingOptions, + getPrivacySetting, + enrichTrackPayload, +}; diff --git a/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.test.js b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.test.js new file mode 100644 index 0000000000..20b39fab93 --- /dev/null +++ b/src/cdk/v2/destinations/the_trade_desk_real_time_conversions/utils.test.js @@ -0,0 +1,621 @@ +const { InstrumentationError } = require('@rudderstack/integrations-lib'); +const { + getRevenue, + getDeviceAdvertisingId, + getDestinationExternalIDObject, + getAdvertisingId, + prepareCustomProperties, + populateEventName, + getDataProcessingOptions, + getPrivacySetting, + enrichTrackPayload, +} = require('./utils'); + +describe('getRevenue', () => { + it('should return revenue value from message properties for custom events', () => { + const message = { + event: 'customEvent', + properties: { + value: 100, + }, + }; + const result = getRevenue(message); + expect(result).toBe(100); + }); + + it('should calculate revenue based on price and quantity from message properties if ecomm event is supported for price calculation', () => { + const message = { + event: 'Product Added', + properties: { + price: 10, + quantity: 5, + }, + }; + const result = getRevenue(message); + expect(result).toBe(50); + }); + + it('should return revenue value from message properties if ecomm event is supported for revenue calculation', () => { + const message = { + event: 'Order Completed', + properties: { + revenue: 200, + }, + }; + const result = getRevenue(message); + expect(result).toBe(200); + }); + + it('should return default revenue value from properties.value for ecomm events', () => { + let message = { + event: 'Product Added', + properties: { + price: '', + value: 200, + }, + }; + let result = getRevenue(message); + expect(result).toBe(200); + + message = { + event: 'Order Completed', + properties: { + value: 200, + }, + }; + result = getRevenue(message); + expect(result).toBe(200); + }); + + it('should throw an Instrumentation error if revenue is missing for `Order Completed` event', () => { + const message = { + event: 'Order Completed', + properties: {}, + }; + expect(() => { + getRevenue(message); + }).toThrow(InstrumentationError); + }); +}); + +describe('getDeviceAdvertisingId', () => { + it('should return an object with deviceId and type properties when context.device.advertisingId and context.os.name are present', () => { + let message = { + context: { + device: { + advertisingId: '123456789', + }, + os: { + name: 'android', + }, + }, + }; + let result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: '123456789', type: 'AAID' }); + + message = { + context: { + device: { + advertisingId: '123456789', + }, + os: { + name: 'ios', + }, + }, + }; + result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: '123456789', type: 'IDFA' }); + + message = { + context: { + device: { + advertisingId: '123456789', + }, + os: { + name: 'windows', + }, + }, + }; + result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: '123456789', type: 'NAID' }); + }); + + it('should return an object with undefined type property when osName is not "android", "windows", or an Apple OS', () => { + const message = { + context: { + device: { + advertisingId: '123456789', + }, + os: { + name: 'linux', + }, + }, + }; + const result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: '123456789', type: undefined }); + }); + + it('should return an object with undefined deviceId and type properties when context is undefined', () => { + let message = {}; + let result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: undefined, type: undefined }); + + message = { + context: {}, + }; + result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: undefined, type: undefined }); + + message = { + context: { + device: {}, + }, + }; + result = getDeviceAdvertisingId(message); + expect(result).toEqual({ deviceId: undefined, type: undefined }); + }); +}); + +describe('getDestinationExternalIDObject', () => { + it('should return the external ID object when it exists in the message context', () => { + const message = { + context: { + externalId: [ + { id: '123', type: 'daid' }, + { id: '456', type: 'type123' }, + ], + }, + }; + const result = getDestinationExternalIDObject(message); + expect(result).toEqual({ id: '123', type: 'daid' }); + }); + + it('should return undefined when no external ID object exists in the message context', () => { + let message = { + context: { + externalId: [], + }, + }; + let result = getDestinationExternalIDObject(message); + expect(result).toBeUndefined(); + + message = { + context: {}, + }; + result = getDestinationExternalIDObject(message); + expect(result).toBeUndefined(); + }); + + it('should return the first matching external ID object in the array', () => { + const message = { + context: { + externalId: [ + { id: '', type: 'daid' }, + { id: '456', type: 'tdid' }, + { id: '789', type: 'UID2' }, + ], + }, + }; + const result = getDestinationExternalIDObject(message); + expect(result).toEqual({ id: '456', type: 'tdid' }); + }); +}); + +describe('getAdvertisingId', () => { + it('should return an object with the ID and type when the message contains a valid device advertising ID and OS type', () => { + const message = { + context: { + device: { + advertisingId: '1234567890', + }, + os: { + name: 'android', + }, + }, + }; + + const result = getAdvertisingId(message); + expect(result).toEqual({ id: '1234567890', type: 'AAID' }); + }); + + it('should return an object with the ID and type when the message contains a valid external ID object with a supported type', () => { + const message = { + context: { + externalId: [ + { + type: 'IDFA', + id: 'abcdefg', + }, + ], + }, + }; + + const result = getAdvertisingId(message); + expect(result).toEqual({ id: 'abcdefg', type: 'IDFA' }); + }); + + it('should return an object with undefined ID and type when the message contains a valid external ID object with an unsupported type', () => { + let message = { + context: { + externalId: [ + { + type: 'unsupported', + id: '1234567890', + }, + ], + }, + }; + + let result = getAdvertisingId(message); + expect(result).toEqual({ id: null, type: null }); + + message = { + context: { + device: { + advertisingId: '1234567890', + }, + }, + }; + + result = getAdvertisingId(message); + expect(result).toEqual({ id: null, type: null }); + }); + + it('should return an object with undefined ID and type when the message contains an external ID object with a supported type but no ID or missing externalId', () => { + let message = { + context: { + externalId: [ + { + type: 'IDFA', + }, + ], + }, + }; + let result = getAdvertisingId(message); + expect(result).toEqual({ id: null, type: null }); + + message = { + context: {}, + }; + result = getAdvertisingId(message); + expect(result).toEqual({ id: null, type: null }); + }); +}); + +describe('prepareCustomProperties', () => { + it('should return an empty object when customProperties is an empty array', () => { + const message = {}; + let destination = { Config: { customProperties: [] } }; + let result = prepareCustomProperties(message, destination); + expect(result).toEqual({}); + + destination = { Config: { customProperties: [{ rudderProperty: '', tradeDeskProperty: '' }] } }; + result = prepareCustomProperties(message, destination); + expect(result).toEqual({}); + + destination = { Config: { customProperties: undefined } }; + result = prepareCustomProperties(message, destination); + expect(result).toEqual({}); + }); + + it('should return an object with `tradeDeskProperty` as key and `rudderProperty` value as value when `rudderProperty` exists in message', () => { + const message = { + rudderProperty1: 'value1', + rudderProperty2: 'value2', + }; + const destination = { + Config: { + customProperties: [ + { + rudderProperty: 'rudderProperty1', + tradeDeskProperty: 'tradeDeskProperty1', + }, + { + rudderProperty: 'rudderProperty2', + tradeDeskProperty: 'tradeDeskProperty2', + }, + { + rudderProperty: 'rudderProperty3', + tradeDeskProperty: 'tradeDeskProperty3', + }, + ], + }, + }; + const result = prepareCustomProperties(message, destination); + expect(result).toEqual({ + tradeDeskProperty1: 'value1', + tradeDeskProperty2: 'value2', + }); + }); +}); + +describe('populateEventName', () => { + it('should return the eventName if it exists in the eventsMapping of destination.Config', () => { + const message = { event: 'someEvent' }; + const destination = { Config: { eventsMapping: [{ from: 'someEvent', to: 'mappedEvent' }] } }; + const result = populateEventName(message, destination); + expect(result).toBe('mappedEvent'); + }); + + it('should return the eventName if it exists in the ECOMM_EVENT_MAP', () => { + const message = { event: 'product added' }; + let destination = { Config: { eventsMapping: [{ from: 'someEvent', to: 'mappedEvent' }] } }; + let result = populateEventName(message, destination); + expect(result).toBe('addtocart'); + + destination = { Config: { eventsMapping: [] } }; + result = populateEventName(message, destination); + expect(result).toBe('addtocart'); + }); + + it('should return undefined if eventsMapping is an empty array', () => { + const message = { event: 'someEvent' }; + const destination = { Config: { eventsMapping: [] } }; + const result = populateEventName(message, destination); + expect(result).toBe('someEvent'); + }); +}); + +describe('getDataProcessingOptions', () => { + it('should return an object with policies and region when provided a integrationObj in message', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + policies: ['LDU'], + region: 'US-CO', + }, + }, + }; + const expected = { + policies: ['LDU'], + region: 'US-CO', + }; + const result = getDataProcessingOptions(message); + expect(result).toEqual(expected); + }); + + it('should throw an InstrumentationError if the region is not a US state', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + policies: ['LDU'], + region: 'EU-abc', + }, + }, + }; + expect(() => { + getDataProcessingOptions(message); + }).toThrow(InstrumentationError); + }); + + it('should throw an InstrumentationError if multiple policies are provided', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + policies: ['LDU', 'Policy2'], + region: 'US-CO', + }, + }, + }; + + expect(() => { + getDataProcessingOptions(message); + }).toThrow(InstrumentationError); + }); + + it('should throw an InstrumentationError if a policy other than `LDU` is provided', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + policies: ['Policy1'], + region: 'US-CO', + }, + }, + }; + + expect(() => { + getDataProcessingOptions(message); + }).toThrow(InstrumentationError); + }); + + it('should return an object with default policy `LDU` when policies are not provided', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + policies: [], + region: 'US-CO', + }, + }, + }; + + const expected = { + policies: ['LDU'], + region: 'US-CO', + }; + + expect(getDataProcessingOptions(message)).toEqual(expected); + }); + + it('should handle empty cases', () => { + let message = { + integrations: { + All: true, + THE_TRADE_DESK: {}, + }, + }; + + expect(getDataProcessingOptions(message)).toBeUndefined(); + + message = { + integrations: { + All: true, + }, + }; + + expect(getDataProcessingOptions(message)).toBeUndefined(); + + message = { + integrations: { + All: true, + THE_TRADE_DESK: { region: 'US-CO' }, + }, + }; + + expect(getDataProcessingOptions(message)).toEqual({ policies: ['LDU'], region: 'US-CO' }); + }); +}); + +describe('getPrivacySetting', () => { + it('should return the privacy settings object when it exists in the integration object', () => { + const message = { + integrations: { + All: true, + THE_TRADE_DESK: { + privacy_settings: [ + { + privacy_type: 'GDPR', + is_applicable: 1, + consent_string: 'ok', + }, + ], + }, + }, + }; + const expected = [ + { + privacy_type: 'GDPR', + is_applicable: 1, + consent_string: 'ok', + }, + ]; + const result = getPrivacySetting(message); + expect(result).toEqual(expected); + }); + + it('should return null when the privacy settings object does not exist in the integration object', () => { + let message = { integrations: {} }; + expect(getPrivacySetting(message)).toBeUndefined(); + + message = { integrations: { THE_TRADE_DESK: {} } }; + expect(getPrivacySetting(message)).toBeUndefined(); + + message = { integrations: { THE_TRADE_DESK: { privacy_settings: null } } }; + expect(getPrivacySetting(message)).toBeNull(); + }); +}); + +describe('enrichTrackPayload', () => { + it('should correctly enrich the payload with custom fields for ecomm events where product array is not supported', () => { + const message = { + event: 'Product Added', + properties: { + product_id: 'prd123', + sku: 'sku123', + brand: 'brand123', + property1: 'value1', + property2: 'value2', + }, + }; + const payload = { + items: [ + { + item_code: 'prd123', + }, + ], + property1: 'value1', + property2: 'value2', + }; + const expectedPayload = { + items: [ + { + item_code: 'prd123', + }, + ], + brand: 'brand123', + property1: 'value1', + property2: 'value2', + }; + + const result = enrichTrackPayload(message, payload); + expect(result).toEqual(expectedPayload); + }); + + it('should correctly enrich the payload with custom fields when the for ecomm events with products array support', () => { + const message = { + event: 'order completed', + properties: { + order_id: 'ord123', + total: 52.0, + subtotal: 45.0, + revenue: 50.0, + products: [{ product_id: 'prd123', sku: 'sku123', brand: 'brand123' }], + property1: 'value1', + property2: 'value2', + }, + }; + const payload = { + order_id: 'ord123', + value: 50.0, + items: [{ item_code: 'prd123', brand: 'brand123' }], + property1: 'value1', + property2: 'value2', + }; + const expectedPayload = { + order_id: 'ord123', + total: 52.0, + subtotal: 45.0, + value: 50.0, + items: [{ item_code: 'prd123', brand: 'brand123' }], + property1: 'value1', + property2: 'value2', + }; + + const result = enrichTrackPayload(message, payload); + + expect(result).toEqual(expectedPayload); + }); + + it('should return the enriched payload for custom event', () => { + const message = { + event: 'someEvent', + properties: { + order_id: 'ord123', + property1: 'value1', + property2: 'value2', + revenue: 10, + value: 11, + products: [ + { + product_id: 'prd123', + test: 'test', + }, + ], + }, + }; + const payload = { + order_id: 'ord123', + value: 11, + }; + const expectedPayload = { + order_id: 'ord123', + property1: 'value1', + property2: 'value2', + revenue: 10, + value: 11, + products: [ + { + product_id: 'prd123', + test: 'test', + }, + ], + }; + + const result = enrichTrackPayload(message, payload); + expect(result).toEqual(expectedPayload); + }); +}); diff --git a/src/v0/destinations/the_trade_desk/networkHandler.js b/src/v0/destinations/the_trade_desk/networkHandler.js index e9693e8132..30378e5ace 100644 --- a/src/v0/destinations/the_trade_desk/networkHandler.js +++ b/src/v0/destinations/the_trade_desk/networkHandler.js @@ -8,37 +8,28 @@ const { getSignatureHeader } = require('../../../cdk/v2/destinations/the_trade_d const { isHttpStatusSuccess } = require('../../util/index'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); -const { - REAL_TIME_CONVERSION_ENDPOINT, -} = require('../../../cdk/v2/destinations/the_trade_desk/config'); const proxyRequest = async (request) => { const { endpoint, data, method, params, headers, config } = prepareProxyRequest(request); - let ProxyHeaders = { - ...headers, - 'Content-Type': JSON_MIME_TYPE, - }; - - // For first party data flow - if (endpoint !== REAL_TIME_CONVERSION_ENDPOINT) { - if (!config?.advertiserSecretKey) { - throw new PlatformError('Advertiser secret key is missing in destination config. Aborting'); - } - if (!process.env.THE_TRADE_DESK_DATA_PROVIDER_SECRET_KEY) { - throw new PlatformError('Data provider secret key is missing. Aborting'); - } + if (!config?.advertiserSecretKey) { + throw new PlatformError('Advertiser secret key is missing in destination config. Aborting'); + } - ProxyHeaders = { - ...ProxyHeaders, - TtdSignature: getSignatureHeader(data, config.advertiserSecretKey), - 'TtdSignature-dp': getSignatureHeader( - data, - process.env.THE_TRADE_DESK_DATA_PROVIDER_SECRET_KEY, - ), - }; + if (!process.env.THE_TRADE_DESK_DATA_PROVIDER_SECRET_KEY) { + throw new PlatformError('Data provider secret key is missing. Aborting'); } + const ProxyHeaders = { + ...headers, + 'Content-Type': JSON_MIME_TYPE, + TtdSignature: getSignatureHeader(data, config.advertiserSecretKey), + 'TtdSignature-dp': getSignatureHeader( + data, + process.env.THE_TRADE_DESK_DATA_PROVIDER_SECRET_KEY, + ), + }; + const requestOptions = { url: endpoint, data, @@ -69,7 +60,6 @@ const responseHandler = (destinationResponse) => { // Trade desk first party data api returns 200 with an error in case of "Failed to parse TDID, DAID, UID2, IDL, EUID, or failed to decrypt UID2Token or EUIDToken" // https://partner.thetradedesk.com/v3/portal/data/doc/post-data-advertiser-external // {"FailedLines":[{"ErrorCode":"MissingUserId","Message":"Invalid DAID, item #1"}]} - // For real time conversion api we don't have separate response handling, trade desk always return 400 for bad events. if ('FailedLines' in response && response.FailedLines.length > 0) { throw new AbortedError( `Request failed with status: ${status} due to ${JSON.stringify(response)}`, diff --git a/test/integrations/destinations/bluecore/data.ts b/test/integrations/destinations/bluecore/data.ts new file mode 100644 index 0000000000..c2205c25a1 --- /dev/null +++ b/test/integrations/destinations/bluecore/data.ts @@ -0,0 +1,6 @@ +import { ecomTestData } from './ecommTestData'; +import { identifyData } from './identifyTestData'; +import { trackTestData } from './trackTestData'; +import { validationTestData } from './validationTestData'; + +export const data = [...identifyData, ...trackTestData, ...ecomTestData, ...validationTestData]; diff --git a/test/integrations/destinations/bluecore/ecommTestData.ts b/test/integrations/destinations/bluecore/ecommTestData.ts new file mode 100644 index 0000000000..de7584df78 --- /dev/null +++ b/test/integrations/destinations/bluecore/ecommTestData.ts @@ -0,0 +1,489 @@ +import { generateSimplifiedTrackPayload, transformResultBuilder } from '../../testUtils'; + +const metadata = { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', +}; + +const destination = { + ID: '1pYpzzvcn7AQ2W9GGIAZSsN6Mfq', + Name: 'BLUECORE', + Config: { + bluecoreNamespace: 'dummy_sandbox', + eventsMapping: [ + { + from: 'ABC Searched', + to: 'search', + }, + { + from: 'testPurchase', + to: 'purchase', + }, + { + from: 'testboth', + to: 'wishlist', + }, + { + from: 'testboth', + to: 'add_to_cart', + }, + ], + }, + Enabled: true, + Transformations: [], + DestinationDefinition: { Config: { cdkV2Enabled: true } }, +}; + +const commonTraits = { + id: 'user@1', + age: '22', + anonymousId: '9c6bd77ea9da3e68', +}; + +const commonPropsWithProducts = { + property1: 'value1', + property2: 'value2', + products: [ + { + product_id: '123', + sku: 'sku123', + name: 'Product 1', + price: 100, + quantity: 2, + }, + { + product_id: '124', + sku: 'sku124', + name: 'Product 2', + price: 200, + quantity: 3, + }, + ], +}; + +const commonPropsWithoutProducts = { + property1: 'value1', + property2: 'value2', + product_id: '123', +}; + +const commonOutputHeaders = { + 'Content-Type': 'application/json', +}; + +const eventEndPoint = 'https://api.bluecore.com/api/track/mobile/v1'; + +export const ecomTestData = [ + { + id: 'bluecore-track-test-1', + name: 'bluecore', + description: + 'Track event call with custom event mapped in destination config to purchase event. This will fail as order_id is not present in the payload', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'testPurchase', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + '[Bluecore] property:: order_id is required for purchase event: Workflow: procWorkflow, Step: handleTrackEvent, ChildStep: preparePayload, OriginalError: [Bluecore] property:: order_id is required for purchase event', + metadata, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-2', + name: 'bluecore', + description: + 'Track event call with custom event mapped in destination config to purchase event. This will fail as total is not present in the payload', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'testPurchase', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: { ...commonPropsWithProducts, order_id: '123' }, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + '[Bluecore] property:: total is required for purchase event: Workflow: procWorkflow, Step: handleTrackEvent, ChildStep: preparePayload, OriginalError: [Bluecore] property:: total is required for purchase event', + metadata, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-3', + name: 'bluecore', + description: + 'Track event call with products searched event not mapped in destination config. This will fail as search_query is not present in the payload', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'Products Searched', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: { ...commonPropsWithoutProducts }, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + '[Bluecore] property:: search_query is required for search event: Workflow: procWorkflow, Step: handleTrackEvent, ChildStep: preparePayload, OriginalError: [Bluecore] property:: search_query is required for search event', + metadata, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-4', + name: 'bluecore', + description: + 'Track event call with Product Viewed event not mapped in destination config. This will be sent with viewed_product name. This event without properties.products will add entire property object as products as this event type is recommended to sent with products', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'product viewed', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithoutProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'test@rudderstack.com', + customer: { + age: '22', + email: 'test@rudderstack.com', + }, + products: [ + { + id: '123', + property1: 'value1', + property2: 'value2', + }, + ], + }, + event: 'viewed_product', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-5', + name: 'bluecore', + description: + 'Track event call with custom event mapped with two standard ecomm events in destination config. Both of the two corresponding standard events will be sent ', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'testboth', + sentAt: '2020-08-14T05:30:30.118Z', + channel: 'web', + context: { + source: 'test', + userAgent: 'chrome', + traits: { + id: 'user@1', + age: '22', + anonymousId: '9c6bd77ea9da3e68', + }, + device: { + advertisingId: 'abc123', + }, + library: { + name: 'rudder-sdk-ruby-sync', + version: '1.0.6', + }, + }, + properties: { + property1: 'value1', + property2: 'value2', + product_id: '123', + }, + anonymousId: 'new-id', + integrations: { + All: true, + }, + }, + metadata, + destination, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'user@1', + customer: { + age: '22', + }, + products: [ + { + id: '123', + property1: 'value1', + property2: 'value2', + }, + ], + }, + event: 'wishlist', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'user@1', + customer: { + age: '22', + }, + products: [ + { + id: '123', + property1: 'value1', + property2: 'value2', + }, + ], + }, + event: 'add_to_cart', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-6', + name: 'bluecore', + description: + 'Track event call with Order Completed event without product array and not mapped in destination config. This will be sent with purchase name. This event without properties.products will generate error as products array is required for purchase event and ordered completed is a standard ecomm event', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'Order Completed', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithoutProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + '[Bluecore]:: products array is required for purchase event: Workflow: procWorkflow, Step: handleTrackEvent, ChildStep: preparePayload, OriginalError: [Bluecore]:: products array is required for purchase event', + metadata, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, +]; diff --git a/test/integrations/destinations/bluecore/identifyTestData.ts b/test/integrations/destinations/bluecore/identifyTestData.ts new file mode 100644 index 0000000000..660e335bc6 --- /dev/null +++ b/test/integrations/destinations/bluecore/identifyTestData.ts @@ -0,0 +1,381 @@ +import { + overrideDestination, + transformResultBuilder, + generateSimplifiedIdentifyPayload, +} from '../../testUtils'; + +const destination = { + ID: '1pYpzzvcn7AQ2W9GGIAZSsN6Mfq', + Name: 'BLUECORE', + Config: { + bluecoreNamespace: 'dummy_sandbox', + eventsMapping: [ + { + from: 'ABC Searched', + to: 'search', + }, + ], + }, + Enabled: true, + Transformations: [], + DestinationDefinition: { Config: { cdkV2Enabled: true } }, +}; + +const metadata = { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', +}; + +const commonTraits = { + anonymousId: '50be5c78-6c3f-4b60-be84-97805a316fb1', + phone: '+1234589947', + gender: 'non-binary', + db: '19950715', + lastname: 'Rudderlabs', + firstName: 'Test', + address: { + city: 'Kolkata', + state: 'WB', + zip: '700114', + country: 'IN', + }, +}; + +const contextWithExternalId = { + traits: { + ...commonTraits, + email: 'abc@gmail.com', + }, + externalId: [{ type: 'bluecoreExternalId', id: '54321' }], +}; + +const commonOutputCustomerProperties = { + first_name: 'Test', + last_name: 'Rudderlabs', + sex: 'non-binary', + address: { + city: 'Kolkata', + state: 'WB', + zip: '700114', + country: 'IN', + }, +}; + +const commonOutputHeaders = { + 'Content-Type': 'application/json', +}; + +const anonymousId = '97c46c81-3140-456d-b2a9-690d70aaca35'; +const userId = 'user@1'; +const sentAt = '2021-01-03T17:02:53.195Z'; +const originalTimestamp = '2021-01-03T17:02:53.193Z'; +const commonEndpoint = 'https://api.bluecore.com/api/track/mobile/v1'; + +export const identifyData = [ + { + id: 'bluecore-identify-test-1', + name: 'bluecore', + description: + '[Success]: Identify call with all properties, that creates a customer in bluecore by default', + scenario: 'Business', + successCriteria: + 'Response should containt one payload with event name as customer_patch and status code should be 200', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + metadata, + message: generateSimplifiedIdentifyPayload({ + context: { + traits: { ...commonTraits, email: 'abc@gmail.com' }, + }, + anonymousId, + userId, + sentAt, + originalTimestamp, + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + userId: '', + method: 'POST', + endpoint: commonEndpoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'abc@gmail.com', + customer: { ...commonOutputCustomerProperties, email: 'abc@gmail.com' }, + }, + token: 'dummy_sandbox', + event: 'customer_patch', + }, + }), + metadata: { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-identify-test-2', + name: 'bluecore', + description: + '[Success]: Identify call with all properties,along with action as identify that mandatorily needs email to link distict_id with customer in bluecore', + scenario: 'Business', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + metadata, + message: generateSimplifiedIdentifyPayload({ + context: { + traits: commonTraits, + }, + traits: { + action: 'identify', + }, + anonymousId, + userId, + sentAt, + originalTimestamp, + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + '[Bluecore] property:: email is required for identify action: Workflow: procWorkflow, Step: prepareIdentifyPayload, ChildStep: undefined, OriginalError: [Bluecore] property:: email is required for identify action', + metadata: { + destinationId: '', + destinationType: '', + namespace: '', + sourceType: '', + }, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-identify-test-3', + name: 'bluecore', + description: + '[Success]: Identify call with all properties,along with action as random which is not supported by bluecore for identify action', + scenario: 'Business', + successCriteria: + 'Response should containt one payload with event name as identify and status code should be 200', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + metadata, + message: generateSimplifiedIdentifyPayload({ + context: { + traits: commonTraits, + }, + traits: { + action: 'random', + }, + anonymousId, + userId, + sentAt, + originalTimestamp, + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + "[Bluecore] traits.action must be 'identify' for identify action: Workflow: procWorkflow, Step: prepareIdentifyPayload, ChildStep: undefined, OriginalError: [Bluecore] traits.action must be 'identify' for identify action", + metadata: { + destinationId: '', + destinationType: '', + namespace: '', + sourceType: '', + }, + statTags: { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-identify-test-4', + name: 'bluecore', + description: + '[Success]: Identify call with all properties, that stitches a customer email with distinct_id in bluecore if action is identify and email is present in traits', + scenario: 'Business', + successCriteria: + 'Response should containt one payload with event name as identify and status code should be 200', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + metadata, + message: generateSimplifiedIdentifyPayload({ + context: { + traits: { ...commonTraits, email: 'abc@gmail.com' }, + }, + traits: { + action: 'identify', + }, + anonymousId, + userId, + sentAt, + originalTimestamp, + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + userId: '', + method: 'POST', + endpoint: commonEndpoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'user@1', + customer: { ...commonOutputCustomerProperties, email: 'abc@gmail.com' }, + }, + token: 'dummy_sandbox', + event: 'identify', + }, + }), + metadata: { + destinationId: '', + destinationType: '', + namespace: '', + sourceType: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-identify-test-5', + name: 'bluecore', + description: + '[Success]: Identify call with all properties and externalId, that creates a customer in bluecore by default, distinct_id is set to externalId value', + scenario: 'Business', + successCriteria: + 'Response should containt one payload with event name as customer_patch and status code should be 200', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + metadata, + message: generateSimplifiedIdentifyPayload({ + context: contextWithExternalId, + anonymousId, + userId, + sentAt, + originalTimestamp, + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + userId: '', + method: 'POST', + endpoint: commonEndpoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: '54321', + customer: { ...commonOutputCustomerProperties, email: 'abc@gmail.com' }, + }, + token: 'dummy_sandbox', + event: 'customer_patch', + }, + }), + metadata: { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, +]; diff --git a/test/integrations/destinations/bluecore/trackTestData.ts b/test/integrations/destinations/bluecore/trackTestData.ts new file mode 100644 index 0000000000..72d48bf93d --- /dev/null +++ b/test/integrations/destinations/bluecore/trackTestData.ts @@ -0,0 +1,439 @@ +import { + generateSimplifiedTrackPayload, + generateTrackPayload, + overrideDestination, + transformResultBuilder, +} from '../../testUtils'; + +const destination = { + ID: '1pYpzzvcn7AQ2W9GGIAZSsN6Mfq', + Name: 'BLUECORE', + Config: { + bluecoreNamespace: 'dummy_sandbox', + eventsMapping: [ + { + from: 'ABC Searched', + to: 'search', + }, + { + from: 'testPurchase', + to: 'purchase', + }, + { + from: 'testboth', + to: 'wishlist', + }, + { + from: 'testboth', + to: 'add_to_cart', + }, + ], + }, + Enabled: true, + Transformations: [], + DestinationDefinition: { Config: { cdkV2Enabled: true } }, +}; + +const metadata = { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', +}; + +const commonTraits = { + id: 'user@1', + age: '22', + anonymousId: '9c6bd77ea9da3e68', +}; + +const contextWithExternalId = { + traits: { + ...commonTraits, + email: 'abc@gmail.com', + }, + externalId: [{ type: 'bluecoreExternalId', id: '54321' }], +}; + +const commonPropsWithProducts = { + property1: 'value1', + property2: 'value2', + products: [ + { + product_id: '123', + sku: 'sku123', + name: 'Product 1', + price: 100, + quantity: 2, + }, + { + product_id: '124', + sku: 'sku124', + name: 'Product 2', + price: 200, + quantity: 3, + }, + ], +}; + +const commonPropsWithoutProducts = { + property1: 'value1', + property2: 'value2', + product_id: '123', +}; + +const commonOutputHeaders = { + 'Content-Type': 'application/json', +}; + +const eventEndPoint = 'https://api.bluecore.com/api/track/mobile/v1'; + +export const trackTestData = [ + { + id: 'bluecore-track-test-1', + name: 'bluecore', + description: + 'Track event call with custom event with properties not mapped in destination config. This will be sent with its original name', + scenario: 'Business', + successCriteria: + 'Response should contain only event payload and status code should be 200, for the event payload should contain flattened properties in the payload', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'TestEven001', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'test@rudderstack.com', + customer: { + age: '22', + email: 'test@rudderstack.com', + }, + products: [ + { + name: 'Product 1', + price: 100, + id: '123', + quantity: 2, + }, + { + name: 'Product 2', + price: 200, + id: '124', + quantity: 3, + }, + ], + }, + event: 'TestEven001', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-2', + name: 'bluecore', + description: + 'Track event call with custom event without properties not mapped in destination config. This will be sent with its original name', + scenario: 'Business', + successCriteria: + 'Response should contain only event payload and status code should be 200. As the event paylaod does not contains products, product array will not be sent', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'TestEven001', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithoutProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'test@rudderstack.com', + customer: { + age: '22', + email: 'test@rudderstack.com', + }, + }, + event: 'TestEven001', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-3', + name: 'bluecore', + description: + 'optin event is also considered as a track event, user need to not map it from the UI , it will be sent with the same event name to bluecore', + scenario: 'Business', + successCriteria: + 'Response should contain only event payload and status code should be 200, for the event payload should contain flattened properties in the payload', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'optin', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithoutProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'test@rudderstack.com', + customer: { + age: '22', + email: 'test@rudderstack.com', + }, + }, + event: 'optin', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-4', + name: 'bluecore', + description: + 'unsubscribe event is also considered as a track event, user need to not map it from the UI , it will be sent with the same event name to bluecore', + scenario: 'Business', + successCriteria: + 'Response should contain only event payload and status code should be 200, for the event payload should contain flattened properties in the payload', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'unsubscribe', + userId: 'sajal12', + context: { + traits: { + ...commonTraits, + email: 'test@rudderstack.com', + phone: '9112340375', + }, + }, + properties: commonPropsWithoutProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: 'test@rudderstack.com', + customer: { + age: '22', + email: 'test@rudderstack.com', + }, + }, + event: 'unsubscribe', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, + { + id: 'bluecore-track-test-5', + name: 'bluecore', + description: + 'Track event call with with externalId. This will map externalId to distinct_id in the payload', + scenario: 'Business', + successCriteria: + 'Response should contain only event payload and status code should be 200, for the event payload should contain flattened properties in the payload', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: destination, + metadata, + message: generateSimplifiedTrackPayload({ + type: 'track', + event: 'TestEven001', + userId: 'sajal12', + context: contextWithExternalId, + properties: commonPropsWithProducts, + anonymousId: '9c6bd77ea9da3e68', + originalTimestamp: '2021-01-25T15:32:56.409Z', + }), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: transformResultBuilder({ + method: 'POST', + endpoint: eventEndPoint, + headers: commonOutputHeaders, + JSON: { + properties: { + distinct_id: '54321', + customer: { + age: '22', + email: 'abc@gmail.com', + }, + products: [ + { + name: 'Product 1', + price: 100, + id: '123', + quantity: 2, + }, + { + name: 'Product 2', + price: 200, + id: '124', + quantity: 3, + }, + ], + }, + event: 'TestEven001', + token: 'dummy_sandbox', + }, + userId: '', + }), + metadata, + statusCode: 200, + }, + ], + }, + }, + }, +]; diff --git a/test/integrations/destinations/bluecore/validationTestData.ts b/test/integrations/destinations/bluecore/validationTestData.ts new file mode 100644 index 0000000000..5b81f8c95a --- /dev/null +++ b/test/integrations/destinations/bluecore/validationTestData.ts @@ -0,0 +1,150 @@ +const metadata = { + sourceType: '', + destinationType: '', + namespace: '', + destinationId: '', +}; + +const outputStatTags = { + destType: 'BLUECORE', + destinationId: '', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + feature: 'processor', + implementation: 'cdkV2', + module: 'destination', +}; + +export const validationTestData = [ + { + id: 'bluecore-validation-test-1', + name: 'bluecore', + description: '[Error]: Check for unsupported message type', + scenario: 'Framework', + successCriteria: + 'Response should contain error message and status code should be 400, as we are sending a message type which is not supported by bluecore destination and the error message should be Event type random is not supported', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + ID: '1pYpzzvcn7AQ2W9GGIAZSsN6Mfq', + Name: 'BLUECORE', + Config: { + bluecoreNamespace: 'dummy_sandbox', + eventsMapping: [ + { + from: 'ABC Searched', + to: 'search', + }, + ], + }, + Enabled: true, + Transformations: [], + DestinationDefinition: { Config: { cdkV2Enabled: true } }, + }, + metadata, + message: { + userId: 'user123', + type: 'random', + groupId: 'XUepkK', + traits: { + subscribe: true, + }, + context: { + traits: { + email: 'test@rudderstack.com', + phone: '+12 345 678 900', + consent: 'email', + }, + }, + timestamp: '2020-01-21T00:21:34.208Z', + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + 'message type random is not supported: Workflow: procWorkflow, Step: validateInput, ChildStep: undefined, OriginalError: message type random is not supported', + metadata, + statTags: outputStatTags, + statusCode: 400, + }, + ], + }, + }, + }, + { + id: 'bluecore-validation-test-1', + name: 'bluecore', + description: '[Error]: Check for not finding bluecoreNamespace', + scenario: 'Framework', + successCriteria: + 'Response should contain error message and status code should be 400, as bluecoreNamespace is not found in the destination config', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + ID: '1pYpzzvcn7AQ2W9GGIAZSsN6Mfq', + Name: 'BLUECORE', + Config: { + eventsMapping: [ + { + from: 'ABC Searched', + to: 'search', + }, + ], + }, + Enabled: true, + Transformations: [], + DestinationDefinition: { Config: { cdkV2Enabled: true } }, + }, + metadata: metadata, + message: { + userId: 'user123', + type: 'random', + groupId: 'XUepkK', + traits: { + subscribe: true, + }, + context: { + traits: { + email: 'test@rudderstack.com', + phone: '+12 345 678 900', + consent: 'email', + }, + }, + timestamp: '2020-01-21T00:21:34.208Z', + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + 'message type random is not supported: Workflow: procWorkflow, Step: validateInput, ChildStep: undefined, OriginalError: message type random is not supported', + metadata, + statTags: outputStatTags, + statusCode: 400, + }, + ], + }, + }, + }, +]; diff --git a/test/integrations/destinations/the_trade_desk/common.ts b/test/integrations/destinations/the_trade_desk/common.ts index 8deaf60034..d792c7faae 100644 --- a/test/integrations/destinations/the_trade_desk/common.ts +++ b/test/integrations/destinations/the_trade_desk/common.ts @@ -3,7 +3,6 @@ const destTypeInUpperCase = 'THE_TRADE_DESK'; const advertiserId = 'test-advertiser-id'; const dataProviderId = 'rudderstack'; const segmentName = 'test-segment'; -const trackerId = 'test-trackerId'; const sampleDestination = { Config: { advertiserId, @@ -11,7 +10,6 @@ const sampleDestination = { dataServer: 'apac', ttlInDays: 30, audienceId: segmentName, - trackerId, }, DestinationDefinition: { Config: { cdkV2Enabled: true } }, }; @@ -35,73 +33,12 @@ const sampleContext = { sources: sampleSource, }; -const sampleContextForConversion = { - app: { - build: '1.0.0', - name: 'RudderLabs Android SDK', - namespace: 'com.rudderlabs.javascript', - version: '1.0.5', - }, - 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, - }, - externalId: [ - { - type: 'daid', - id: 'test-daid', - }, - ], - ip: '0.0.0.0', - page: { - referrer: 'https://docs.rudderstack.com/destinations/trade_desk', - }, - library: { - name: 'RudderLabs JavaScript SDK', - version: '1.0.5', - }, - locale: 'en-GB', - os: { - name: '', - version: '', - }, - screen: { - density: 2, - }, - userAgent: - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36', -}; - -const integrationObject = { - All: true, - THE_TRADE_DESK: { - policies: ['LDU'], - region: 'US-CA', - privacy_settings: [ - { - privacy_type: 'GDPR', - is_applicable: 1, - consent_string: 'ok', - }, - ], - }, -}; - export { destType, destTypeInUpperCase, advertiserId, dataProviderId, segmentName, - trackerId, sampleDestination, sampleContext, - sampleContextForConversion, - integrationObject, }; diff --git a/test/integrations/destinations/the_trade_desk/delivery/data.ts b/test/integrations/destinations/the_trade_desk/delivery/data.ts index da8f60972e..320eb6dcfe 100644 --- a/test/integrations/destinations/the_trade_desk/delivery/data.ts +++ b/test/integrations/destinations/the_trade_desk/delivery/data.ts @@ -5,7 +5,6 @@ import { dataProviderId, segmentName, sampleDestination, - trackerId, } from '../common'; beforeAll(() => { @@ -246,169 +245,4 @@ export const data = [ }, }, }, - { - name: destType, - description: 'Successful delivery of realtime conversion event to Trade Desk', - feature: 'dataDelivery', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - event_name: 'viewitem', - value: 249.95000000000002, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - method: 'POST', - }, - }, - output: { - response: { - status: 200, - body: { - output: { - destinationResponse: { - response: { - Message: null, - EventResponses: [], - }, - status: 200, - }, - message: 'Request Processed Successfully', - status: 200, - }, - }, - }, - }, - }, - { - name: destType, - description: - 'Error response from the Trade Desk due to invalid real time conversion event payload', - feature: 'dataDelivery', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - event_name: 'viewitem', - value: 249.95000000000002, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - privacy_settings: [{}], - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - method: 'POST', - }, - }, - output: { - response: { - status: 400, - body: { - output: { - destinationResponse: { - response: { - Message: null, - EventResponses: [ - { - EventIndex: 0, - EventErrors: [ - { - Error: 'InvalidPrivacySetting', - ErrorMessage: 'The request has an invalid privacy setting.', - }, - ], - EventWarnings: [], - Successful: false, - }, - ], - }, - status: 400, - }, - message: - 'Request failed with status: 400 due to {"Message":null,"EventResponses":[{"EventIndex":0,"EventErrors":[{"Error":"InvalidPrivacySetting","ErrorMessage":"The request has an invalid privacy setting."}],"EventWarnings":[],"Successful":false}]}', - statTags: { - destType: destTypeInUpperCase, - destinationId: 'Non-determininable', - errorCategory: 'network', - errorType: 'aborted', - feature: 'dataDelivery', - implementation: 'native', - module: 'destination', - workspaceId: 'Non-determininable', - }, - status: 400, - }, - }, - }, - }, - }, ]; diff --git a/test/integrations/destinations/the_trade_desk/network.ts b/test/integrations/destinations/the_trade_desk/network.ts index 5908cbf8f5..ed6bdf4c7d 100644 --- a/test/integrations/destinations/the_trade_desk/network.ts +++ b/test/integrations/destinations/the_trade_desk/network.ts @@ -1,4 +1,4 @@ -import { destType, advertiserId, dataProviderId, segmentName, trackerId } from './common'; +import { destType, advertiserId, dataProviderId, segmentName } from './common'; export const networkCallsData = [ { @@ -103,107 +103,4 @@ export const networkCallsData = [ statusText: 'Ok', }, }, - { - httpReq: { - url: 'https://insight.adsrvr.org/track/realtimeconversion', - data: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - event_name: 'viewitem', - value: 249.95000000000002, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - }, - ], - }, - params: { destination: destType }, - headers: { - 'Content-Type': 'application/json', - 'User-Agent': 'RudderLabs', - }, - method: 'POST', - }, - httpRes: { - data: { - Message: null, - EventResponses: [], - }, - status: 200, - statusText: 'OK', - }, - }, - { - httpReq: { - url: 'https://insight.adsrvr.org/track/realtimeconversion', - data: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - event_name: 'viewitem', - value: 249.95000000000002, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - privacy_settings: [{}], - }, - ], - }, - params: { destination: destType }, - headers: { - 'Content-Type': 'application/json', - 'User-Agent': 'RudderLabs', - }, - method: 'POST', - }, - httpRes: { - data: { - Message: null, - EventResponses: [ - { - EventIndex: 0, - EventErrors: [ - { - Error: 'InvalidPrivacySetting', - ErrorMessage: 'The request has an invalid privacy setting.', - }, - ], - EventWarnings: [], - Successful: false, - }, - ], - }, - status: 400, - statusText: 'Bad Request', - }, - }, ]; diff --git a/test/integrations/destinations/the_trade_desk/router/data.ts b/test/integrations/destinations/the_trade_desk/router/data.ts index 691ec703b9..f095f561db 100644 --- a/test/integrations/destinations/the_trade_desk/router/data.ts +++ b/test/integrations/destinations/the_trade_desk/router/data.ts @@ -6,11 +6,8 @@ import { advertiserId, dataProviderId, segmentName, - trackerId, sampleDestination, sampleContext, - sampleContextForConversion, - integrationObject, } from '../common'; export const data = [ @@ -868,786 +865,6 @@ export const data = [ }, mockFns: defaultMockFns, }, - { - name: destType, - description: - 'Mapped Ecommerce events (product added, product viewed, product added to wishlist, cart viewed, checkout started, order completed)', - feature: 'router', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - input: [ - { - message: { - type: 'track', - event: 'Product Added', - messageId: 'messageId123', - context: sampleContextForConversion, - properties: { - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - category: 'Games', - name: 'Cones of Dunshire', - brand: 'Wyatt Games', - variant: 'exapansion pack', - price: 49.99, - quantity: 5, - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - key1: 'value1', - }, - integrations: integrationObject, - }, - destination: overrideDestination(sampleDestination, { - customProperties: [ - { - rudderProperty: 'properties.key1', - tradeDeskProperty: 'td1', - }, - { - rudderProperty: 'properties.key2', - tradeDeskProperty: 'td2', - }, - ], - }), - metadata: { - jobId: 1, - }, - }, - { - message: { - type: 'track', - event: 'Product Viewed', - properties: { - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - category: 'Games', - name: 'Cones of Dunshire', - brand: 'Wyatt Games', - variant: 'exapansion pack', - price: 49.99, - quantity: 5, - coupon: 'PREORDER15', - currency: 'USD', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - }, - }, - destination: sampleDestination, - metadata: { - jobId: 2, - }, - }, - { - message: { - type: 'track', - event: 'Product Added to Wishlist', - properties: { - wishlist_id: '74fkdjfl0jfdkdj29j030', - wishlist_name: 'New Games', - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - category: 'Games', - name: 'Cones of Dunshire', - brand: 'Wyatt Games', - variant: 'exapansion pack', - price: 49.99, - quantity: 1, - coupon: 'PREORDER15', - position: 1, - url: 'https://www.site.com/product/path', - image_url: 'https://www.site.com/product/path.jpg', - }, - }, - destination: sampleDestination, - metadata: { - jobId: 3, - }, - }, - { - message: { - type: 'track', - event: 'Cart Viewed', - properties: { - cart_id: '6b2c6f5aecf86a4ae77358ae3', - products: [ - { - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - name: 'Cones of Dunshire', - price: 49.99, - position: 5, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - product_id: '577c6f5d5cf86a4c7735ba03', - sku: '3309-483-2201', - name: 'Five Crowns', - price: 5.99, - position: 2, - category: 'Games', - }, - ], - }, - }, - destination: sampleDestination, - metadata: { - jobId: 4, - }, - }, - { - message: { - type: 'track', - event: 'Checkout Started', - properties: { - order_id: '40684e8f0eaf000000000000', - affiliation: 'Vandelay Games', - value: 52, - revenue: 50.0, - shipping: 4, - tax: 3, - discount: 5, - coupon: 'NEWCUST5', - currency: 'USD', - products: [ - { - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - name: 'Cones of Dunshire', - price: 40, - position: 1, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - product_id: '577c6f5d5cf86a4c7735ba03', - sku: '3309-483-2201', - name: 'Five Crowns', - price: 5, - position: 2, - category: 'Games', - }, - ], - }, - }, - destination: sampleDestination, - metadata: { - jobId: 5, - }, - }, - { - message: { - type: 'track', - event: 'Order Completed', - properties: { - checkout_id: '70324a1f0eaf000000000000', - order_id: '40684e8f0eaf000000000000', - affiliation: 'Vandelay Games', - total: 52.0, - subtotal: 45.0, - revenue: 50.0, - shipping: 4.0, - tax: 3.0, - discount: 5.0, - coupon: 'NEWCUST5', - currency: 'USD', - products: [ - { - product_id: '622c6f5d5cf86a4c77358033', - sku: '8472-998-0112', - name: 'Cones of Dunshire', - price: 40, - position: 1, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - product_id: '577c6f5d5cf86a4c7735ba03', - sku: '3309-483-2201', - name: 'Five Crowns', - price: 5, - position: 2, - category: 'Games', - }, - ], - }, - }, - destination: sampleDestination, - metadata: { - jobId: 6, - }, - }, - ], - destType, - }, - }, - }, - output: { - response: { - status: 200, - body: { - output: [ - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'addtocart', - value: 249.95000000000002, - adid: 'test-daid', - adid_type: 'DAID', - client_ip: '0.0.0.0', - referrer_url: 'https://docs.rudderstack.com/destinations/trade_desk', - imp: 'messageId123', - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - td1: 'value1', - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - data_processing_option: { - policies: ['LDU'], - region: 'US-CA', - }, - privacy_settings: [ - { - privacy_type: 'GDPR', - is_applicable: 1, - consent_string: 'ok', - }, - ], - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 1, - }, - ], - batched: false, - statusCode: 200, - destination: overrideDestination(sampleDestination, { - customProperties: [ - { - rudderProperty: 'properties.key1', - tradeDeskProperty: 'td1', - }, - { - rudderProperty: 'properties.key2', - tradeDeskProperty: 'td2', - }, - ], - }), - }, - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - event_name: 'viewitem', - value: 249.95000000000002, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 5, - price: 49.99, - }, - ], - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.webp', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 2, - }, - ], - batched: false, - statusCode: 200, - destination: sampleDestination, - }, - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'wishlistitem', - value: 49.99, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - qty: 1, - price: 49.99, - }, - ], - wishlist_id: '74fkdjfl0jfdkdj29j030', - wishlist_name: 'New Games', - category: 'Games', - brand: 'Wyatt Games', - variant: 'exapansion pack', - coupon: 'PREORDER15', - position: 1, - url: 'https://www.site.com/product/path', - image_url: 'https://www.site.com/product/path.jpg', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 3, - }, - ], - batched: false, - statusCode: 200, - destination: sampleDestination, - }, - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'viewcart', - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - price: 49.99, - position: 5, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - item_code: '577c6f5d5cf86a4c7735ba03', - name: 'Five Crowns', - price: 5.99, - position: 2, - category: 'Games', - }, - ], - cart_id: '6b2c6f5aecf86a4ae77358ae3', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 4, - }, - ], - batched: false, - statusCode: 200, - destination: sampleDestination, - }, - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - order_id: '40684e8f0eaf000000000000', - event_name: 'startcheckout', - value: 50, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - price: 40, - position: 1, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - item_code: '577c6f5d5cf86a4c7735ba03', - name: 'Five Crowns', - price: 5, - position: 2, - category: 'Games', - }, - ], - affiliation: 'Vandelay Games', - shipping: 4, - tax: 3, - discount: 5, - coupon: 'NEWCUST5', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 5, - }, - ], - batched: false, - statusCode: 200, - destination: sampleDestination, - }, - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - currency: 'USD', - order_id: '40684e8f0eaf000000000000', - event_name: 'purchase', - value: 50, - items: [ - { - item_code: '622c6f5d5cf86a4c77358033', - name: 'Cones of Dunshire', - price: 40, - position: 1, - category: 'Games', - url: 'https://www.website.com/product/path', - image_url: 'https://www.website.com/product/path.jpg', - }, - { - item_code: '577c6f5d5cf86a4c7735ba03', - name: 'Five Crowns', - price: 5, - position: 2, - category: 'Games', - }, - ], - checkout_id: '70324a1f0eaf000000000000', - affiliation: 'Vandelay Games', - total: 52.0, - subtotal: 45.0, - shipping: 4.0, - tax: 3.0, - discount: 5.0, - coupon: 'NEWCUST5', - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 6, - }, - ], - batched: false, - statusCode: 200, - destination: sampleDestination, - }, - ], - }, - }, - }, - }, - { - name: destType, - description: 'Custom event', - feature: 'router', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - input: [ - { - message: { - type: 'track', - event: 'custom event abc', - properties: { - key1: 'value1', - value: 25, - product_id: 'prd123', - key2: true, - test: 'test123', - }, - }, - destination: overrideDestination(sampleDestination, { - customProperties: [ - { - rudderProperty: 'properties.key1', - tradeDeskProperty: 'td1', - }, - { - rudderProperty: 'properties.key2', - tradeDeskProperty: 'td2', - }, - ], - }), - metadata: { - jobId: 1, - }, - }, - ], - destType, - }, - }, - }, - output: { - response: { - status: 200, - body: { - output: [ - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'custom event abc', - value: 25, - product_id: 'prd123', - test: 'test123', - td1: 'value1', - td2: true, - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 1, - }, - ], - batched: false, - statusCode: 200, - destination: overrideDestination(sampleDestination, { - customProperties: [ - { - rudderProperty: 'properties.key1', - tradeDeskProperty: 'td1', - }, - { - rudderProperty: 'properties.key2', - tradeDeskProperty: 'td2', - }, - ], - }), - }, - ], - }, - }, - }, - }, - { - name: destType, - description: 'Mapped standard trade desk event', - feature: 'router', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - input: [ - { - message: { - type: 'track', - event: 'custom event abc', - properties: { - key1: 'value1', - value: 25, - product_id: 'prd123', - key2: true, - test: 'test123', - }, - }, - destination: overrideDestination(sampleDestination, { - eventsMapping: [ - { - from: 'custom event abc', - to: 'direction', - }, - ], - }), - metadata: { - jobId: 1, - }, - }, - ], - destType, - }, - }, - }, - output: { - response: { - status: 200, - body: { - output: [ - { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'direction', - value: 25, - product_id: 'prd123', - test: 'test123', - key1: 'value1', - key2: true, - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 1, - }, - ], - batched: false, - statusCode: 200, - destination: overrideDestination(sampleDestination, { - eventsMapping: [ - { - from: 'custom event abc', - to: 'direction', - }, - ], - }), - }, - ], - }, - }, - }, - }, { name: destType, description: 'Batch call with different event types', @@ -1674,30 +891,6 @@ export const data = [ jobId: 1, }, }, - { - message: { - type: 'track', - event: 'custom event abc', - properties: { - key1: 'value1', - value: 25, - revenue: 10, - product_id: 'prd123', - key2: true, - test: 'test123', - products: [ - { - product_id: 'prd123', - test: 'test', - }, - ], - }, - }, - destination: sampleDestination, - metadata: { - jobId: 2, - }, - }, { message: { type: 'identify', @@ -1711,7 +904,7 @@ export const data = [ }, destination: sampleDestination, metadata: { - jobId: 3, + jobId: 2, }, }, ], @@ -1767,53 +960,8 @@ export const data = [ destination: sampleDestination, }, { - batchedRequest: { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', - headers: {}, - params: {}, - body: { - JSON: { - data: [ - { - tracker_id: trackerId, - adv: advertiserId, - event_name: 'custom event abc', - value: 25, - product_id: 'prd123', - test: 'test123', - key1: 'value1', - key2: true, - revenue: 10, - products: [ - { - product_id: 'prd123', - test: 'test', - }, - ], - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - metadata: [ - { - jobId: 2, - }, - ], batched: false, - statusCode: 200, - destination: sampleDestination, - }, - { - batched: false, - metadata: [{ jobId: 3 }], + metadata: [{ jobId: 2 }], statusCode: 400, error: 'Event type identify is not supported', statTags: { @@ -1831,119 +979,4 @@ export const data = [ }, }, }, - { - name: destType, - description: 'Tracker id is not present', - feature: 'router', - module: 'destination', - version: 'v0', - input: { - request: { - body: { - input: [ - { - message: { - type: 'record', - action: 'insert', - fields: { - DAID: 'test-daid-1', - }, - channel: 'sources', - context: sampleContext, - recordId: '1', - }, - destination: overrideDestination(sampleDestination, { trackerId: '' }), - metadata: { - jobId: 1, - }, - }, - { - message: { - type: 'track', - event: 'custom event abc', - properties: { - key1: 'value1', - value: 25, - product_id: 'prd123', - key2: true, - test: 'test123', - }, - }, - destination: overrideDestination(sampleDestination, { trackerId: '' }), - metadata: { - jobId: 2, - }, - }, - ], - destType, - }, - method: 'POST', - }, - }, - output: { - response: { - status: 200, - body: { - output: [ - { - batchedRequest: [ - { - version: '1', - type: 'REST', - method: 'POST', - endpoint: 'https://sin-data.adsrvr.org/data/advertiser', - headers: {}, - params: {}, - body: { - JSON: { - DataProviderId: dataProviderId, - AdvertiserId: advertiserId, - Items: [ - { - DAID: 'test-daid-1', - Data: [ - { - Name: segmentName, - TTLInMinutes: 43200, - }, - ], - }, - ], - }, - JSON_ARRAY: {}, - XML: {}, - FORM: {}, - }, - files: {}, - }, - ], - metadata: [ - { - jobId: 1, - }, - ], - batched: true, - statusCode: 200, - destination: overrideDestination(sampleDestination, { trackerId: '' }), - }, - { - batched: false, - metadata: [{ jobId: 2 }], - statusCode: 400, - error: 'Tracking Tag ID is not present. Aborting', - statTags: { - errorCategory: 'dataValidation', - errorType: 'configuration', - destType: 'THE_TRADE_DESK', - module: 'destination', - implementation: 'cdkV2', - feature: 'router', - }, - destination: overrideDestination(sampleDestination, { trackerId: '' }), - }, - ], - }, - }, - }, - }, ]; diff --git a/test/integrations/destinations/the_trade_desk_real_time_conversions/common.ts b/test/integrations/destinations/the_trade_desk_real_time_conversions/common.ts new file mode 100644 index 0000000000..3af7791ec8 --- /dev/null +++ b/test/integrations/destinations/the_trade_desk_real_time_conversions/common.ts @@ -0,0 +1,79 @@ +const destType = 'the_trade_desk_real_time_conversions'; +const destTypeInUpperCase = 'THE_TRADE_DESK_REAL_TIME_CONVERSIONS'; +const advertiserId = 'test-advertiser-id'; +const trackerId = 'test-trackerId'; +const sampleDestination = { + Config: { + advertiserId, + trackerId, + }, + DestinationDefinition: { Config: { cdkV2Enabled: true } }, +}; + +const sampleContextForConversion = { + app: { + build: '1.0.0', + name: 'RudderLabs Android SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.0.5', + }, + 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, + }, + externalId: [ + { + type: 'daid', + id: 'test-daid', + }, + ], + ip: '0.0.0.0', + page: { + referrer: 'https://docs.rudderstack.com/destinations/trade_desk', + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.0.5', + }, + locale: 'en-GB', + os: { + name: '', + version: '', + }, + screen: { + density: 2, + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36', +}; + +const integrationObject = { + All: true, + THE_TRADE_DESK: { + policies: ['LDU'], + region: 'US-CA', + privacy_settings: [ + { + privacy_type: 'GDPR', + is_applicable: 1, + consent_string: 'ok', + }, + ], + }, +}; + +export { + destType, + destTypeInUpperCase, + advertiserId, + trackerId, + sampleDestination, + sampleContextForConversion, + integrationObject, +}; diff --git a/test/integrations/destinations/the_trade_desk_real_time_conversions/processor/data.ts b/test/integrations/destinations/the_trade_desk_real_time_conversions/processor/data.ts new file mode 100644 index 0000000000..264c760088 --- /dev/null +++ b/test/integrations/destinations/the_trade_desk_real_time_conversions/processor/data.ts @@ -0,0 +1,984 @@ +import { overrideDestination } from '../../../testUtils'; +import { + destType, + destTypeInUpperCase, + advertiserId, + trackerId, + sampleDestination, + sampleContextForConversion, + integrationObject, +} from '../common'; + +export const data = [ + { + name: destType, + description: 'Missing advertiser ID in the config', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'custom event abc', + properties: { + key1: 'value1', + value: 25, + product_id: 'prd123', + key2: true, + test: 'test123', + }, + }, + destination: overrideDestination(sampleDestination, { advertiserId: '' }), + metadata: { + jobId: 1, + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + 'Advertiser ID is not present. Aborting: Workflow: procWorkflow, Step: validateConfig, ChildStep: undefined, OriginalError: Advertiser ID is not present. Aborting', + statTags: { + destType: destTypeInUpperCase, + implementation: 'cdkV2', + feature: 'processor', + module: 'destination', + errorCategory: 'dataValidation', + errorType: 'configuration', + }, + metadata: { + jobId: 1, + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Tracker id is not present', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'custom event abc', + properties: { + key1: 'value1', + value: 25, + product_id: 'prd123', + key2: true, + test: 'test123', + }, + }, + destination: overrideDestination(sampleDestination, { trackerId: '' }), + metadata: { + jobId: 1, + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + 'Tracking Tag ID is not present. Aborting: Workflow: procWorkflow, Step: validateConfig, ChildStep: undefined, OriginalError: Tracking Tag ID is not present. Aborting', + statTags: { + destType: destTypeInUpperCase, + implementation: 'cdkV2', + feature: 'processor', + module: 'destination', + errorCategory: 'dataValidation', + errorType: 'configuration', + }, + metadata: { + jobId: 1, + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Unsupported event type', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'identify', + context: { + traits: { + name: 'John Doe', + email: 'johndoe@gmail.com', + age: 25, + }, + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + error: + 'Event type identify is not supported: Workflow: procWorkflow, Step: validateInput, ChildStep: undefined, OriginalError: Event type identify is not supported', + statTags: { + destType: destTypeInUpperCase, + implementation: 'cdkV2', + feature: 'processor', + module: 'destination', + errorCategory: 'dataValidation', + errorType: 'instrumentation', + }, + metadata: { + jobId: 1, + }, + statusCode: 400, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Product Added', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Product Added', + messageId: 'messageId123', + context: sampleContextForConversion, + properties: { + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + category: 'Games', + name: 'Cones of Dunshire', + brand: 'Wyatt Games', + variant: 'expansion pack', + price: 49.99, + quantity: 5, + coupon: 'PREORDER15', + position: 1, + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.webp', + key1: 'value1', + }, + integrations: integrationObject, + }, + destination: overrideDestination(sampleDestination, { + customProperties: [ + { + rudderProperty: 'properties.key1', + tradeDeskProperty: 'td1', + }, + { + rudderProperty: 'properties.key2', + tradeDeskProperty: 'td2', + }, + ], + }), + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + event_name: 'addtocart', + value: 249.95000000000002, + adid: 'test-daid', + adid_type: 'DAID', + client_ip: '0.0.0.0', + referrer_url: 'https://docs.rudderstack.com/destinations/trade_desk', + imp: 'messageId123', + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + qty: 5, + price: 49.99, + }, + ], + td1: 'value1', + category: 'Games', + brand: 'Wyatt Games', + variant: 'expansion pack', + coupon: 'PREORDER15', + position: 1, + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.webp', + data_processing_option: { + policies: ['LDU'], + region: 'US-CA', + }, + privacy_settings: [ + { + privacy_type: 'GDPR', + is_applicable: 1, + consent_string: 'ok', + }, + ], + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Product Viewed', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Product Viewed', + properties: { + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + category: 'Games', + name: 'Cones of Dunshire', + brand: 'Wyatt Games', + variant: 'expansion pack', + price: 49.99, + quantity: 5, + coupon: 'PREORDER15', + currency: 'USD', + position: 1, + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.webp', + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + currency: 'USD', + event_name: 'viewitem', + value: 249.95000000000002, + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + qty: 5, + price: 49.99, + }, + ], + category: 'Games', + brand: 'Wyatt Games', + variant: 'expansion pack', + coupon: 'PREORDER15', + position: 1, + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.webp', + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Product Added to Wishlist', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Product Added to Wishlist', + properties: { + wishlist_id: '74fkdjfl0jfdkdj29j030', + wishlist_name: 'New Games', + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + category: 'Games', + name: 'Cones of Dunshire', + brand: 'Wyatt Games', + variant: 'expansion pack', + price: 49.99, + quantity: 1, + coupon: 'PREORDER15', + position: 1, + url: 'https://www.site.com/product/path', + image_url: 'https://www.site.com/product/path.jpg', + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + event_name: 'wishlistitem', + value: 49.99, + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + qty: 1, + price: 49.99, + }, + ], + wishlist_id: '74fkdjfl0jfdkdj29j030', + wishlist_name: 'New Games', + category: 'Games', + brand: 'Wyatt Games', + variant: 'expansion pack', + coupon: 'PREORDER15', + position: 1, + url: 'https://www.site.com/product/path', + image_url: 'https://www.site.com/product/path.jpg', + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Cart Viewed', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Cart Viewed', + properties: { + cart_id: '6b2c6f5aecf86a4ae77358ae3', + products: [ + { + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + name: 'Cones of Dunshire', + price: 49.99, + position: 5, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + product_id: '577c6f5d5cf86a4c7735ba03', + sku: '3309-483-2201', + name: 'Five Crowns', + price: 5.99, + position: 2, + category: 'Games', + }, + ], + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + event_name: 'viewcart', + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + price: 49.99, + position: 5, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + item_code: '577c6f5d5cf86a4c7735ba03', + name: 'Five Crowns', + price: 5.99, + position: 2, + category: 'Games', + }, + ], + cart_id: '6b2c6f5aecf86a4ae77358ae3', + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Checkout Started', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Checkout Started', + properties: { + order_id: '40684e8f0eaf000000000000', + affiliation: 'Vandelay Games', + value: 52, + revenue: 50.0, + shipping: 4, + tax: 3, + discount: 5, + coupon: 'NEWCUST5', + currency: 'USD', + products: [ + { + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + name: 'Cones of Dunshire', + price: 40, + position: 1, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + product_id: '577c6f5d5cf86a4c7735ba03', + sku: '3309-483-2201', + name: 'Five Crowns', + price: 5, + position: 2, + category: 'Games', + }, + ], + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + currency: 'USD', + order_id: '40684e8f0eaf000000000000', + event_name: 'startcheckout', + value: 50, + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + price: 40, + position: 1, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + item_code: '577c6f5d5cf86a4c7735ba03', + name: 'Five Crowns', + price: 5, + position: 2, + category: 'Games', + }, + ], + affiliation: 'Vandelay Games', + shipping: 4, + tax: 3, + discount: 5, + coupon: 'NEWCUST5', + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Order Completed', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'Order Completed', + properties: { + checkout_id: '70324a1f0eaf000000000000', + order_id: '40684e8f0eaf000000000000', + affiliation: 'Vandelay Games', + total: 52.0, + subtotal: 45.0, + revenue: 50.0, + shipping: 4.0, + tax: 3.0, + discount: 5.0, + coupon: 'NEWCUST5', + currency: 'USD', + products: [ + { + product_id: '622c6f5d5cf86a4c77358033', + sku: '8472-998-0112', + name: 'Cones of Dunshire', + price: 40, + position: 1, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + product_id: '577c6f5d5cf86a4c7735ba03', + sku: '3309-483-2201', + name: 'Five Crowns', + price: 5, + position: 2, + category: 'Games', + }, + ], + }, + }, + destination: sampleDestination, + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + currency: 'USD', + order_id: '40684e8f0eaf000000000000', + event_name: 'purchase', + value: 50, + items: [ + { + item_code: '622c6f5d5cf86a4c77358033', + name: 'Cones of Dunshire', + price: 40, + position: 1, + category: 'Games', + url: 'https://www.website.com/product/path', + image_url: 'https://www.website.com/product/path.jpg', + }, + { + item_code: '577c6f5d5cf86a4c7735ba03', + name: 'Five Crowns', + price: 5, + position: 2, + category: 'Games', + }, + ], + checkout_id: '70324a1f0eaf000000000000', + affiliation: 'Vandelay Games', + total: 52.0, + subtotal: 45.0, + shipping: 4.0, + tax: 3.0, + discount: 5.0, + coupon: 'NEWCUST5', + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Custom event', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'custom event abc', + properties: { + key1: 'value1', + value: 25, + product_id: 'prd123', + key2: true, + test: 'test123', + }, + }, + destination: overrideDestination(sampleDestination, { + customProperties: [ + { + rudderProperty: 'properties.key1', + tradeDeskProperty: 'td1', + }, + { + rudderProperty: 'properties.key2', + tradeDeskProperty: 'td2', + }, + ], + }), + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + event_name: 'custom event abc', + value: 25, + product_id: 'prd123', + test: 'test123', + td1: 'value1', + td2: true, + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: destType, + description: 'Mapped standard trade desk event', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + type: 'track', + event: 'custom event abc', + properties: { + key1: 'value1', + value: 25, + product_id: 'prd123', + key2: true, + test: 'test123', + }, + }, + destination: overrideDestination(sampleDestination, { + eventsMapping: [ + { + from: 'custom event abc', + to: 'direction', + }, + ], + }), + metadata: { + jobId: 1, + }, + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://insight.adsrvr.org/track/realtimeconversion', + headers: { 'Content-Type': 'application/json' }, + params: {}, + body: { + JSON: { + data: [ + { + tracker_id: trackerId, + adv: advertiserId, + event_name: 'direction', + value: 25, + product_id: 'prd123', + test: 'test123', + key1: 'value1', + key2: true, + }, + ], + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + metadata: { + jobId: 1, + }, + statusCode: 200, + }, + ], + }, + }, + }, +]; diff --git a/test/integrations/testUtils.ts b/test/integrations/testUtils.ts index 3a9cd759ab..2cfbe3be8e 100644 --- a/test/integrations/testUtils.ts +++ b/test/integrations/testUtils.ts @@ -125,7 +125,7 @@ export const generateSimplifiedIdentifyPayload = (parametersOverride: any) => { rudderId: parametersOverride.rudderId || generateAlphanumericId(36), messageId: parametersOverride.messageId || generateAlphanumericId(36), context: { - externalId: parametersOverride.externalId, + externalId: parametersOverride.context.externalId, traits: parametersOverride.context.traits, }, anonymousId: parametersOverride.anonymousId || 'default-anonymousId', @@ -180,7 +180,7 @@ export const generateSimplifiedTrackPayload = (parametersOverride: any) => { rudderId: parametersOverride.rudderId || generateAlphanumericId(36), messageId: parametersOverride.messageId || generateAlphanumericId(36), context: removeUndefinedAndNullValues({ - externalId: parametersOverride.externalId, + externalId: parametersOverride.context.externalId, traits: parametersOverride.context.traits, }), anonymousId: parametersOverride.anonymousId || 'default-anonymousId',