Skip to content

Commit

Permalink
feat: pass ip details for factorsAI (#2925)
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik authored Jan 2, 2024
1 parent 66a5390 commit 970d37d
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/v0/destinations/factorsai/data/FactorsAIGroupConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
"destKey": "email",
"sourceKeys": "email",
"required": false
},
{
"sourceKeys": "context",
"destKey": "context",
"required": false
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
"destKey": "channel",
"sourceKeys": "channel",
"required": false
},
{
"sourceKeys": "context",
"destKey": "context",
"required": false
}
]
5 changes: 5 additions & 0 deletions src/v0/destinations/factorsai/data/FactorsAITrackConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
"destKey": "channel",
"sourceKeys": "channel",
"required": false
},
{
"sourceKeys": "context",
"destKey": "context",
"required": false
}
]
17 changes: 14 additions & 3 deletions src/v0/destinations/factorsai/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -29,21 +37,24 @@ 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);
}

// 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);
}
Expand Down
270 changes: 270 additions & 0 deletions test/integrations/destinations/factorsai/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ export const data = [
XML: {},
FORM: {},
JSON: {
context: {
ip: '8.8.8.8',
},
type: 'identify',
traits: {
age: 27,
Expand Down Expand Up @@ -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,
},
],
},
},
},
];

0 comments on commit 970d37d

Please sign in to comment.