-
Notifications
You must be signed in to change notification settings - Fork 102
/
util.js
354 lines (316 loc) · 11.9 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const lodash = require('lodash');
const set = require('set-value');
const get = require('get-value');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
isDefined,
constructPayload,
getFullName,
extractCustomFields,
isAppleFamily,
getBrowserInfo,
toUnixTimestampInMS,
batchMultiplexedEvents,
getSuccessRespEvents,
defaultBatchRequestConfig,
IsGzipSupported,
isObject,
isDefinedAndNotNullAndNotEmpty,
isDefinedAndNotNull,
} = require('../../util');
const {
ConfigCategory,
MP_IDENTIFY_EXCLUSION_LIST,
GEO_SOURCE_ALLOWED_VALUES,
mappingConfig,
} = require('./config');
const { CommonUtils } = require('../../../util/common');
const mPIdentifyConfigJson = mappingConfig[ConfigCategory.IDENTIFY.name];
const mPProfileAndroidConfigJson = mappingConfig[ConfigCategory.PROFILE_ANDROID.name];
const mPProfileIosConfigJson = mappingConfig[ConfigCategory.PROFILE_IOS.name];
const mPSetOnceConfigJson = mappingConfig[ConfigCategory.SET_ONCE.name];
/**
* this function has been used to create
* @param {*} message rudderstack identify payload
* @param {*} mappingJson identifyConfig.json
* @param {*} useNewMapping a variable to support backward compatibility
* @returns
*/
const getTransformedJSON = (message, mappingJson, useNewMapping) => {
let rawPayload = constructPayload(message, mappingJson);
if (
isDefined(rawPayload.$geo_source) &&
!GEO_SOURCE_ALLOWED_VALUES.includes(rawPayload.$geo_source)
) {
throw new InstrumentationError("$geo_source value must be either null or 'reverse_geocoding' ");
}
const userName = get(rawPayload, '$name');
if (!userName) {
set(rawPayload, '$name', getFullName(message));
}
rawPayload = extractCustomFields(
message,
rawPayload,
['traits', 'context.traits'],
MP_IDENTIFY_EXCLUSION_LIST,
);
/*
we are adding backward compatibility using useNewMapping key.
TODO :: This portion need to be removed after we deciding to stop
support for old mapping.
*/
if (!useNewMapping) {
if (rawPayload.$first_name) {
rawPayload.$firstName = rawPayload.$first_name;
delete rawPayload.$first_name;
}
if (rawPayload.$last_name) {
rawPayload.$lastName = rawPayload.$last_name;
delete rawPayload.$last_name;
}
}
const device = get(message, 'context.device');
if (device && device.token) {
let payload;
if (isAppleFamily(device.type)) {
payload = constructPayload(message, mPProfileIosConfigJson);
rawPayload.$ios_devices = [device.token];
} else if (device.type.toLowerCase() === 'android') {
payload = constructPayload(message, mPProfileAndroidConfigJson);
rawPayload.$android_devices = [device.token];
}
rawPayload = { ...rawPayload, ...payload };
}
if (message.channel === 'web' && message.context?.userAgent) {
const browser = getBrowserInfo(message.context.userAgent);
rawPayload.$browser = browser.name;
rawPayload.$browser_version = browser.version;
}
return rawPayload;
};
/**
* This function is used to generate identify response.
* @param {*} message rudderstack identify payload
* @param {*} type EventType (identify here)
* @param {*} destination Config.destination
* @param {*} responseBuilderSimple function to generate response
* @returns
*/
const createIdentifyResponse = (message, type, destination, responseBuilderSimple) => {
// this variable is used for supporting backward compatibility
const { useNewMapping, token } = destination.Config;
// user payload created
const properties = getTransformedJSON(message, mPIdentifyConfigJson, useNewMapping);
const payload = {
$set: properties,
$token: token,
$distinct_id: message.userId || message.anonymousId,
$ip: get(message, 'context.ip') || message.request_ip,
$time: toUnixTimestampInMS(message.timestamp || message.originalTimestamp),
};
if (destination?.Config.identityMergeApi === 'simplified') {
payload.$distinct_id = message.userId || `$device:${message.anonymousId}`;
}
if (message.context?.active === false) {
payload.$ignore_time = true;
}
// Creating the response to create user
return responseBuilderSimple(payload, message, type, destination.Config);
};
/**
* This function is checking availability of service account credentials, and secret token.
* https://developer.mixpanel.com/reference/authentication
* @param {*} destination inputs from dashboard
* @returns
*/
const isImportAuthCredentialsAvailable = (destination) =>
destination.Config.apiSecret ||
(destination.Config.serviceAccountSecret &&
destination.Config.serviceAccountUserName &&
destination.Config.projectId);
/**
* Builds UTM parameters from a campaign object.
*
* @param {Object} campaign - The campaign object containing the campaign details.
* @returns {Object} - The object containing the UTM parameters extracted from the campaign object.
*
* @example
* const campaign = {
* name: 'summer_sale',
* source: 'newsletter',
* medium: 'email'
* };
* { utm_campaign: 'summer_sale', utm_source: 'newsletter', utm_medium: 'email' }
*/
const buildUtmParams = (campaign) => {
const utmParams = {};
if (isObject(campaign)) {
Object.keys(campaign).forEach((key) => {
if (key === 'name') {
utmParams.utm_campaign = campaign[key];
} else {
utmParams[`utm_${key}`] = campaign[key];
}
});
}
return utmParams;
};
/**
* Group events with the same endpoint together in batches
* @param {*} events - An array of events
* @returns
*/
const groupEventsByEndpoint = (events) => {
const eventMap = {
engage: [],
groups: [],
track: [],
import: [],
};
const batchErrorRespList = [];
events.forEach((result) => {
if (result.message) {
const { destination, metadata } = result;
const message = CommonUtils.toArray(result.message);
message.forEach((msg) => {
const endpoint = Object.keys(eventMap).find((key) => msg.endpoint.includes(key));
if (endpoint) {
eventMap[endpoint].push({ message: msg, destination, metadata });
}
});
} else if (result.error) {
batchErrorRespList.push(result);
}
});
return {
engageEvents: eventMap.engage,
groupsEvents: eventMap.groups,
trackEvents: eventMap.track,
importEvents: eventMap.import,
batchErrorRespList,
};
};
const generateBatchedPayloadForArray = (events, reqMetadata) => {
const { batchedRequest } = defaultBatchRequestConfig();
const firstEvent = events[0];
batchedRequest.endpoint = firstEvent.endpoint;
batchedRequest.headers = firstEvent.headers;
batchedRequest.params = firstEvent.params;
const batchResponseList = events.flatMap((event) => JSON.parse(event.body.JSON_ARRAY.batch));
if (IsGzipSupported(reqMetadata) && firstEvent.endpoint.includes('import')) {
// Gzipping the payload for /import endpoint
batchedRequest.body.GZIP = { payload: JSON.stringify(batchResponseList) };
} else {
batchedRequest.body.JSON_ARRAY = { batch: JSON.stringify(batchResponseList) };
}
return batchedRequest;
};
const batchEvents = (successRespList, maxBatchSize, reqMetadata) => {
const batchedEvents = batchMultiplexedEvents(successRespList, maxBatchSize);
return batchedEvents.map((batch) => {
const batchedRequest = generateBatchedPayloadForArray(batch.events, reqMetadata);
return getSuccessRespEvents(batchedRequest, batch.metadata, batch.destination, true);
});
};
/**
* Trims the traits and contextTraits objects based on the setOnceProperties array and returns an object containing the modified traits, contextTraits, and setOnce properties.
*
* @param {object} traits - An object representing the traits.
* @param {object} contextTraits - An object representing the context traits.
* @param {string[]} setOnceProperties - An array of property paths to be considered for the setOnce transformation.
* @returns {object} - An object containing the modified traits, contextTraits, and setOnce properties.
*
* @example
* const traits = { name: 'John', age: 30 };
* const contextTraits = { country: 'USA', language: 'English', address: { city: 'New York', state: 'NY' }}};
* const setOnceProperties = ['name', 'country', 'address.city'];
*
* const result = trimTraits(traits, contextTraits, setOnceProperties);
* // Output: { traits: { age: 30 }, contextTraits: { language: 'English' }, setOnce: { $name: 'John', $country_code: 'USA', city: 'New York'} }
*/
function trimTraits(traits, contextTraits, setOnceProperties) {
let sentOnceTransformedPayload;
// Create a copy of the original traits object
const traitsCopy = { ...traits };
const contextTraitsCopy = { ...contextTraits };
// Initialize setOnce object
const setOnceEligible = {};
// Step 1: find the k-v pairs of setOnceProperties in traits and contextTraits
setOnceProperties.forEach((propertyPath) => {
const propName = lodash.last(propertyPath.split('.'));
const traitsValue = get(traitsCopy, propertyPath);
const contextTraitsValue = get(contextTraitsCopy, propertyPath);
if (isDefinedAndNotNullAndNotEmpty(traitsValue)) {
setOnceEligible[propName] = traitsValue;
lodash.unset(traitsCopy, propertyPath);
}
if (isDefinedAndNotNullAndNotEmpty(contextTraitsValue)) {
if (!setOnceEligible.hasOwnProperty(propName)) {
setOnceEligible[propName] = contextTraitsValue;
}
lodash.unset(contextTraitsCopy, propertyPath);
}
});
if (setOnceEligible && Object.keys(setOnceEligible).length > 0) {
// Step 2: transform properties eligible as per rudderstack declared identify event mapping
// setOnce should have all traits from message.traits and message.context.traits by now
sentOnceTransformedPayload = constructPayload(setOnceEligible, mPSetOnceConfigJson);
// Step 3: combine the transformed and custom setOnce traits
sentOnceTransformedPayload = extractCustomFields(
setOnceEligible,
sentOnceTransformedPayload,
'root',
MP_IDENTIFY_EXCLUSION_LIST,
);
}
return {
traits: traitsCopy,
contextTraits: contextTraitsCopy,
setOnce: sentOnceTransformedPayload || {},
};
}
/**
* Generates a custom event name for a page or screen.
*
* @param {Object} message - The message object
* @param {string} userDefinedEventTemplate - The user-defined event template to be used for generating the event name.
* @throws {ConfigurationError} If the event template is missing.
* @returns {string} The generated custom event name.
* @example
* const userDefinedEventTemplate = "Viewed {{ category }} {{ name }} Page";
* const message = {name: 'Home', properties: {category: 'Index'}};
* output: "Viewed Index Home Page"
*/
const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) => {
if (!userDefinedEventTemplate) {
throw new ConfigurationError(
'Event name template is not configured. Please provide a valid value for the `Page/Screen Event Name Template` in the destination dashboard.',
);
}
let eventName = userDefinedEventTemplate;
if (isDefinedAndNotNull(message.properties?.category)) {
// Replace {{ category }} with actual values
eventName = eventName.replace(/{{\s*category\s*}}/g, message.properties.category);
} else {
// find {{ category }} surrounded by whitespace characters and replace it with a single whitespace character
eventName = eventName.replace(/\s{{\s*category\s*}}\s/g, ' ');
}
if (isDefinedAndNotNull(message.name)) {
// Replace {{ name }} with actual values
eventName = eventName.replace(/{{\s*name\s*}}/g, message.name);
} else {
// find {{ name }} surrounded by whitespace characters and replace it with a single whitespace character
eventName = eventName.replace(/\s{{\s*name\s*}}\s/g, ' ');
}
return eventName;
};
module.exports = {
createIdentifyResponse,
isImportAuthCredentialsAvailable,
buildUtmParams,
groupEventsByEndpoint,
generateBatchedPayloadForArray,
batchEvents,
trimTraits,
generatePageOrScreenCustomEventName,
};