diff --git a/src/v0/destinations/factorsai/data/FactorsAIGroupConfig.json b/src/v0/destinations/factorsai/data/FactorsAIGroupConfig.json index f5f78e71e4..d581f56070 100644 --- a/src/v0/destinations/factorsai/data/FactorsAIGroupConfig.json +++ b/src/v0/destinations/factorsai/data/FactorsAIGroupConfig.json @@ -34,5 +34,10 @@ "destKey": "email", "sourceKeys": "email", "required": false + }, + { + "sourceKeys": "context", + "destKey": "context", + "required": false } ] diff --git a/src/v0/destinations/factorsai/data/FactorsAIIdentifyConfig.json b/src/v0/destinations/factorsai/data/FactorsAIIdentifyConfig.json index 9a08c382d4..b34cd2147e 100644 --- a/src/v0/destinations/factorsai/data/FactorsAIIdentifyConfig.json +++ b/src/v0/destinations/factorsai/data/FactorsAIIdentifyConfig.json @@ -34,5 +34,10 @@ "destKey": "channel", "sourceKeys": "channel", "required": false + }, + { + "sourceKeys": "context", + "destKey": "context", + "required": false } ] diff --git a/src/v0/destinations/factorsai/data/FactorsAITrackConfig.json b/src/v0/destinations/factorsai/data/FactorsAITrackConfig.json index ccb9686c29..67df67fc76 100644 --- a/src/v0/destinations/factorsai/data/FactorsAITrackConfig.json +++ b/src/v0/destinations/factorsai/data/FactorsAITrackConfig.json @@ -44,5 +44,10 @@ "destKey": "channel", "sourceKeys": "channel", "required": false + }, + { + "sourceKeys": "context", + "destKey": "context", + "required": false } ] diff --git a/src/v0/destinations/factorsai/transform.js b/src/v0/destinations/factorsai/transform.js index 9824b73bbf..9fbfc926f7 100644 --- a/src/v0/destinations/factorsai/transform.js +++ b/src/v0/destinations/factorsai/transform.js @@ -13,6 +13,14 @@ const { JSON_MIME_TYPE } = require('../../util/constant'); const { ConfigCategories, mappingConfig, BASE_URL } = require('./config'); +function populateIpDetails(requestJson, message) { + const payload = requestJson; + if (message.context || message.request_ip) { + payload.context = { ...(payload.context || {}), ip: message.context?.ip || message.request_ip }; + } + return payload; +} + // build final response function buildResponse(payload, factorsAIApiKey) { const response = defaultRequestConfig(); @@ -29,13 +37,15 @@ function buildResponse(payload, factorsAIApiKey) { // process identify call function processIdentify(message, factorsAIApiKey) { - const requestJson = constructPayload(message, mappingConfig[ConfigCategories.IDENTIFY.name]); + let requestJson = constructPayload(message, mappingConfig[ConfigCategories.IDENTIFY.name]); + requestJson = populateIpDetails(requestJson, message); return buildResponse(requestJson, factorsAIApiKey); } // process track call function processTrack(message, factorsAIApiKey) { - const requestJson = constructPayload(message, mappingConfig[ConfigCategories.TRACK.name]); + let requestJson = constructPayload(message, mappingConfig[ConfigCategories.TRACK.name]); + requestJson = populateIpDetails(requestJson, message); // flatten json as factorsAi do not support nested properties requestJson.properties = flattenJson(requestJson.properties); return buildResponse(requestJson, factorsAIApiKey); @@ -43,7 +53,8 @@ function processTrack(message, factorsAIApiKey) { // process Page Call function processPageAndGroup(message, factorsAIApiKey, category) { - const requestJson = constructPayload(message, mappingConfig[category]); + let requestJson = constructPayload(message, mappingConfig[category]); + requestJson = populateIpDetails(requestJson, message); requestJson.type = message.type; return buildResponse(requestJson, factorsAIApiKey); } diff --git a/test/integrations/destinations/factorsai/data.ts b/test/integrations/destinations/factorsai/data.ts index fb0a76bf34..be13c291f4 100644 --- a/test/integrations/destinations/factorsai/data.ts +++ b/test/integrations/destinations/factorsai/data.ts @@ -308,6 +308,9 @@ export const data = [ XML: {}, FORM: {}, JSON: { + context: { + ip: '8.8.8.8', + }, type: 'identify', traits: { age: 27, @@ -540,4 +543,271 @@ export const data = [ }, }, }, + { + name: 'factorsai', + description: 'IP fetched from request_ip', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + factorsAIApiKey: 'sdgerghsdfhsdhsdh432141dfgdfsg', + }, + }, + message: { + context: { + page: { + url: 'myurl', + path: '/workspace/index.html', + title: 'test track', + search: '?s=ek8reb577tu65kfc2fv41fbm3j', + referrer: '$direct', + initial_referrer: '$direct', + referring_domain: '', + initial_referring_domain: '', + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36', + }, + event: 'finalPageTest', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + type: 'track', + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + originalTimestamp: '2022-10-17T15:32:44.202+05:30', + request_ip: '104.205.211.60', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + body: { + XML: {}, + FORM: {}, + JSON: { + type: 'track', + event: 'finalPageTest', + context: { + ip: '104.205.211.60', + page: { + url: 'myurl', + path: '/workspace/index.html', + title: 'test track', + search: '?s=ek8reb577tu65kfc2fv41fbm3j', + referrer: '$direct', + initial_referrer: '$direct', + referring_domain: '', + initial_referring_domain: '', + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36', + }, + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + timestamp: '2022-10-17T15:32:44.202+05:30', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + }, + JSON_ARRAY: {}, + }, + type: 'REST', + files: {}, + userId: '', + method: 'POST', + params: {}, + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic c2RnZXJnaHNkZmhzZGhzZGg0MzIxNDFkZmdkZnNnOg==', + }, + version: '1', + endpoint: 'https://api.factors.ai/integrations/rudderstack_platform', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: 'factorsai', + description: 'IP fetched from request_ip', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + factorsAIApiKey: 'sdgerghsdfhsdhsdh432141dfgdfsg', + }, + }, + message: { + context: { + locale: 'en-US', + screen: { + density: 2, + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.0.0', + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + }, + event: 'finalPageTest', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + type: 'track', + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + originalTimestamp: '2022-10-17T15:32:44.202+05:30', + request_ip: '0.0.0.0', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + body: { + XML: {}, + FORM: {}, + JSON: { + type: 'track', + event: 'finalPageTest', + context: { + ip: '0.0.0.0', + locale: 'en-US', + screen: { + density: 2, + }, + library: { + name: 'RudderLabs JavaScript SDK', + version: '1.0.0', + }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + }, + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + timestamp: '2022-10-17T15:32:44.202+05:30', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + }, + JSON_ARRAY: {}, + }, + type: 'REST', + files: {}, + userId: '', + method: 'POST', + params: {}, + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic c2RnZXJnaHNkZmhzZGhzZGg0MzIxNDFkZmdkZnNnOg==', + }, + version: '1', + endpoint: 'https://api.factors.ai/integrations/rudderstack_platform', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: 'factorsai', + description: 'IP fetched from request_ip withput context in payload', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination: { + Config: { + factorsAIApiKey: 'sdgerghsdfhsdhsdh432141dfgdfsg', + }, + }, + message: { + event: 'finalPageTest', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + type: 'track', + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + originalTimestamp: '2022-10-17T15:32:44.202+05:30', + request_ip: '1.1.1.1', + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + body: { + XML: {}, + FORM: {}, + JSON: { + type: 'track', + event: 'finalPageTest', + context: { + ip: '1.1.1.1', + }, + messageId: '3c0abc14-96a2-4aed-9dfc-ee463832cc24', + timestamp: '2022-10-17T15:32:44.202+05:30', + properties: { + title: 'Intro to Test final segemt context page added context last', + }, + userId: 'PageTestAnonymousUser', + }, + JSON_ARRAY: {}, + }, + type: 'REST', + files: {}, + userId: '', + method: 'POST', + params: {}, + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic c2RnZXJnaHNkZmhzZGhzZGg0MzIxNDFkZmdkZnNnOg==', + }, + version: '1', + endpoint: 'https://api.factors.ai/integrations/rudderstack_platform', + }, + statusCode: 200, + }, + ], + }, + }, + }, ];