forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
intentIqIdSystem.js
569 lines (515 loc) · 18.4 KB
/
intentIqIdSystem.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**
* This module adds IntentIqId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/intentIqIdSystem
* @requires module:modules/userId
*/
import { logError, logInfo } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { submodule } from '../src/hook.js'
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
import { gppDataHandler, uspDataHandler } from '../src/consentHandler.js';
import AES from 'crypto-js/aes.js';
import Utf8 from 'crypto-js/enc-utf8.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').SubmoduleConfig} SubmoduleConfig
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/
const PCID_EXPIRY = 365;
const MODULE_NAME = 'intentIqId';
export const FIRST_PARTY_KEY = '_iiq_fdata';
export let FIRST_PARTY_DATA_KEY = '_iiq_fdata';
export const WITH_IIQ = 'A';
export const WITHOUT_IIQ = 'B';
export const NOT_YET_DEFINED = 'U';
export const OPT_OUT = 'O';
export const BLACK_LIST = 'L';
export const CLIENT_HINTS_KEY = '_iiq_ch';
export const EMPTY = 'EMPTY'
export const VERSION = 0.1
const encoderCH = {
brands: 0,
mobile: 1,
platform: 2,
architecture: 3,
bitness: 4,
model: 5,
platformVersion: 6,
wow64: 7,
fullVersionList: 8
};
const INVALID_ID = 'INVALID_ID';
const SUPPORTED_TYPES = ['html5', 'cookie']
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
/**
* Generate standard UUID string
* @return {string}
*/
function generateGUID() {
let d = new Date().getTime();
const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return guid;
}
/**
* Encrypts plaintext.
* @param {string} plainText The plaintext to encrypt.
* @returns {string} The encrypted text as a base64 string.
*/
export function encryptData(plainText) {
return AES.encrypt(plainText, MODULE_NAME).toString();
}
/**
* Decrypts ciphertext.
* @param {string} encryptedText The encrypted text as a base64 string.
* @returns {string} The decrypted plaintext.
*/
export function decryptData(encryptedText) {
const bytes = AES.decrypt(encryptedText, MODULE_NAME);
return bytes.toString(Utf8);
}
/**
* Read Intent IQ data from local storage or cookie
* @param key
* @return {string}
*/
export function readData(key, allowedStorage) {
try {
if (storage.hasLocalStorage() && allowedStorage.includes('html5')) {
return storage.getDataFromLocalStorage(key);
}
if (storage.cookiesAreEnabled() && allowedStorage.includes('cookie')) {
return storage.getCookie(key);
}
} catch (error) {
logError(error);
}
}
/**
* Store Intent IQ data in cookie, local storage or both of them
* expiration date: 365 days
* @param key
* @param {string} value IntentIQ ID value to sintentIqIdSystem_spec.jstore
*/
export function storeData(key, value, allowedStorage) {
try {
logInfo(MODULE_NAME + ': storing data: key=' + key + ' value=' + value);
if (value) {
if (storage.hasLocalStorage() && allowedStorage.includes('html5')) {
storage.setDataInLocalStorage(key, value);
}
if (storage.cookiesAreEnabled() && allowedStorage.includes('cookie')) {
const expiresStr = (new Date(Date.now() + (PCID_EXPIRY * (60 * 60 * 24 * 1000)))).toUTCString();
storage.setCookie(key, value, expiresStr, 'LAX');
}
}
} catch (error) {
logError(error);
}
}
/**
* Remove Intent IQ data from cookie or local storage
* @param key
*/
export function removeDataByKey(key, allowedStorage) {
try {
if (storage.hasLocalStorage() && allowedStorage.includes('html5')) {
storage.removeDataFromLocalStorage(key);
}
if (storage.cookiesAreEnabled() && allowedStorage.includes('cookie')) {
const expiredDate = new Date(0).toUTCString();
storage.setCookie(key, '', expiredDate, 'LAX');
}
} catch (error) {
logError(error);
}
}
/**
* Parse json if possible, else return null
* @param data
*/
function tryParse(data) {
try {
return JSON.parse(data);
} catch (err) {
logError(err);
return null;
}
}
/**
* Convert GPP data to an object
* @param {Object} data
* @return {string} The JSON string representation of the input data.
*/
export function handleGPPData(data = {}) {
if (Array.isArray(data)) {
let obj = {};
for (const element of data) {
obj = Object.assign(obj, element);
}
return JSON.stringify(obj);
}
return JSON.stringify(data);
}
/**
* Detects the browser using either userAgent or userAgentData
* @return {string} The name of the detected browser or 'unknown' if unable to detect
*/
function detectBrowser() {
try {
if (navigator.userAgent) {
return detectBrowserFromUserAgent(navigator.userAgent);
} else if (navigator.userAgentData) {
return detectBrowserFromUserAgentData(navigator.userAgentData);
}
} catch (error) {
logError('Error detecting browser:', error);
}
return 'unknown';
}
/**
* Detects the browser from the user agent string
* @param {string} userAgent - The user agent string from the browser
* @return {string} The name of the detected browser or 'unknown' if unable to detect
*/
export function detectBrowserFromUserAgent(userAgent) {
const browserRegexPatterns = {
opera: /Opera|OPR/,
edge: /Edg/,
chrome: /Chrome|CriOS/,
safari: /Safari/,
firefox: /Firefox/,
ie: /MSIE|Trident/,
};
// Check for Chrome first to avoid confusion with Safari
if (browserRegexPatterns.chrome.test(userAgent)) {
return 'chrome';
}
// Now we can safely check for Safari
if (browserRegexPatterns.safari.test(userAgent) && !browserRegexPatterns.chrome.test(userAgent)) {
return 'safari';
}
// Check other browsers
for (const browser in browserRegexPatterns) {
if (browserRegexPatterns[browser].test(userAgent)) {
return browser;
}
}
return 'unknown';
}
/**
* Detects the browser from the NavigatorUAData object
* @param {NavigatorUAData} userAgentData - The user agent data object from the browser
* @return {string} The name of the detected browser or 'unknown' if unable to detect
*/
export function detectBrowserFromUserAgentData(userAgentData) {
const brandNames = userAgentData.brands.map(brand => brand.brand);
if (brandNames.includes('Microsoft Edge')) {
return 'edge';
} else if (brandNames.includes('Opera')) {
return 'opera';
} else if (brandNames.some(brand => brand === 'Chromium' || brand === 'Google Chrome')) {
return 'chrome';
}
return 'unknown';
}
/**
* Processes raw client hints data into a structured format.
* @param {object} clientHints - Raw client hints data
* @return {string} A JSON string of processed client hints or an empty string if no hints
*/
export function handleClientHints(clientHints) {
const chParams = {};
for (const key in clientHints) {
if (clientHints.hasOwnProperty(key) && clientHints[key] !== '') {
if (['brands', 'fullVersionList'].includes(key)) {
let handledParam = '';
clientHints[key].forEach((element, index) => {
const isNotLast = index < clientHints[key].length - 1;
handledParam += `"${element.brand}";v="${element.version}"${isNotLast ? ', ' : ''}`;
});
chParams[encoderCH[key]] = handledParam;
} else if (typeof clientHints[key] === 'boolean') {
chParams[encoderCH[key]] = `?${clientHints[key] ? 1 : 0}`;
} else {
chParams[encoderCH[key]] = `"${clientHints[key]}"`;
}
}
}
return Object.keys(chParams).length ? JSON.stringify(chParams) : '';
}
function defineStorageType(params) {
if (!params || !Array.isArray(params)) return ['html5']; // use locale storage be default
const filteredArr = params.filter(item => SUPPORTED_TYPES.includes(item));
return filteredArr.length ? filteredArr : ['html5'];
}
/** @type {Submodule} */
export const intentIqIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {{string}} value
* @returns {{intentIqId: {string}}|undefined}
*/
decode(value) {
return value && value != '' && INVALID_ID != value ? { 'intentIqId': value } : undefined;
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @returns {IdResponse|undefined}
*/
getId(config) {
const configParams = (config?.params) || {};
let decryptedData, callbackTimeoutID;
let callbackFired = false
let runtimeEids = {}
const firePartnerCallback = () => {
if (configParams.callback && !callbackFired) {
callbackFired = true;
if (callbackTimeoutID) clearTimeout(callbackTimeoutID);
configParams.callback(runtimeEids, firstPartyData?.group || NOT_YET_DEFINED);
}
}
callbackTimeoutID = setTimeout(() => {
firePartnerCallback();
}, configParams.timeoutInMillis || 500
);
if (typeof configParams.partner !== 'number') {
logError('User ID - intentIqId submodule requires a valid partner to be defined');
firePartnerCallback()
return;
}
let rrttStrtTime = 0;
let partnerData = {};
let shouldCallServer = false
const currentBrowserLowerCase = detectBrowser();
const browserBlackList = typeof configParams.browserBlackList === 'string' ? configParams.browserBlackList.toLowerCase() : '';
const allowedStorage = defineStorageType(config.enabledStorageTypes);
// Check if current browser is in blacklist
if (browserBlackList?.includes(currentBrowserLowerCase)) {
logError('User ID - intentIqId submodule: browser is in blacklist!');
if (configParams.callback) configParams.callback('', BLACK_LIST);
return;
}
// Get consent information
const cmpData = {};
const uspData = uspDataHandler.getConsentData();
const gppData = gppDataHandler.getConsentData();
if (uspData) {
cmpData.us_privacy = uspData;
}
if (gppData) {
cmpData.gpp = '';
cmpData.gpi = 1;
if (gppData.parsedSections && 'usnat' in gppData.parsedSections) {
cmpData.gpp = handleGPPData(gppData.parsedSections['usnat']);
cmpData.gpi = 0
}
cmpData.gpp_sid = gppData.applicableSections;
}
// Read client hints from storage
let clientHints = readData(CLIENT_HINTS_KEY, allowedStorage);
// Get client hints and save to storage
if (navigator.userAgentData) {
navigator.userAgentData
.getHighEntropyValues([
'brands',
'mobile',
'bitness',
'wow64',
'architecture',
'model',
'platform',
'platformVersion',
'fullVersionList'
])
.then(ch => {
clientHints = handleClientHints(ch);
storeData(CLIENT_HINTS_KEY, clientHints, allowedStorage)
});
}
if (!FIRST_PARTY_DATA_KEY.includes(configParams.partner)) {
FIRST_PARTY_DATA_KEY += '_' + configParams.partner;
}
// Read Intent IQ 1st party id or generate it if none exists
let firstPartyData = tryParse(readData(FIRST_PARTY_KEY, allowedStorage));
if (!firstPartyData?.pcid) {
const firstPartyId = generateGUID();
firstPartyData = { pcid: firstPartyId, pcidDate: Date.now(), group: NOT_YET_DEFINED, cttl: 0, uspapi_value: EMPTY, gpp_value: EMPTY, date: Date.now() };
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
} else if (!firstPartyData.pcidDate) {
firstPartyData.pcidDate = Date.now();
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
}
const savedData = tryParse(readData(FIRST_PARTY_DATA_KEY, allowedStorage))
if (savedData) {
partnerData = savedData;
}
if (partnerData.data) {
if (partnerData.data.length) { // encrypted data
decryptedData = tryParse(decryptData(partnerData.data));
runtimeEids = decryptedData;
}
}
if (!firstPartyData.cttl || Date.now() - firstPartyData.date > firstPartyData.cttl || firstPartyData.uspapi_value !== cmpData.us_privacy || firstPartyData.gpp_value !== cmpData.gpp) {
firstPartyData.uspapi_value = cmpData.us_privacy;
firstPartyData.gpp_value = cmpData.gpp;
firstPartyData.isOptedOut = false
firstPartyData.cttl = 0
shouldCallServer = true;
partnerData.data = {}
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
storeData(FIRST_PARTY_DATA_KEY, JSON.stringify(partnerData), allowedStorage);
} else if (firstPartyData.isOptedOut) {
firePartnerCallback()
}
if (firstPartyData.group === WITHOUT_IIQ || (firstPartyData.group !== WITHOUT_IIQ && runtimeEids && Object.keys(runtimeEids).length)) {
firePartnerCallback()
}
if (!shouldCallServer) {
firePartnerCallback();
return { id: runtimeEids };
}
// use protocol relative urls for http or https
let url = `https://api.intentiq.com/profiles_engine/ProfilesEngineServlet?at=39&mi=10&dpi=${configParams.partner}&pt=17&dpn=1`;
url += configParams.pcid ? '&pcid=' + encodeURIComponent(configParams.pcid) : '';
url += configParams.pai ? '&pai=' + encodeURIComponent(configParams.pai) : '';
url += firstPartyData.pcid ? '&iiqidtype=2&iiqpcid=' + encodeURIComponent(firstPartyData.pcid) : '';
url += firstPartyData.pid ? '&pid=' + encodeURIComponent(firstPartyData.pid) : '';
url += (partnerData.cttl) ? '&cttl=' + encodeURIComponent(partnerData.cttl) : '';
url += (partnerData.rrtt) ? '&rrtt=' + encodeURIComponent(partnerData.rrtt) : '';
url += firstPartyData.pcidDate ? '&iiqpciddate=' + encodeURIComponent(firstPartyData.pcidDate) : '';
url += cmpData.us_privacy ? '&pa=' + encodeURIComponent(cmpData.us_privacy) : '';
url += cmpData.gpp ? '&gpv=' + encodeURIComponent(cmpData.gpp) : '';
url += cmpData.gpi ? '&gpi=' + cmpData.gpi : '';
url += clientHints ? '&uh=' + encodeURIComponent(clientHints) : '';
url += VERSION ? '&jsver=' + VERSION : '';
url += firstPartyData?.group ? '&testGroup=' + encodeURIComponent(firstPartyData.group) : '';
const storeFirstPartyData = () => {
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
storeData(FIRST_PARTY_DATA_KEY, JSON.stringify(partnerData), allowedStorage);
}
const resp = function (callback) {
const callbacks = {
success: response => {
let respJson = tryParse(response);
// If response is a valid json and should save is true
if (respJson) {
partnerData.date = Date.now();
firstPartyData.date = Date.now();
const defineEmptyDataAndFireCallback = () => {
respJson.data = partnerData.data = runtimeEids = {};
removeDataByKey(FIRST_PARTY_DATA_KEY, allowedStorage)
firePartnerCallback()
callback()
}
if (callbackTimeoutID) clearTimeout(callbackTimeoutID)
if ('cttl' in respJson) {
firstPartyData.cttl = respJson.cttl;
} else firstPartyData.cttl = 86400000;
if ('tc' in respJson) {
partnerData.terminationCause = respJson.tc;
if (respJson.tc == 41) {
firstPartyData.group = WITHOUT_IIQ;
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
defineEmptyDataAndFireCallback();
return;
} else {
firstPartyData.group = WITH_IIQ;
}
}
if ('isOptedOut' in respJson) {
if (respJson.isOptedOut !== firstPartyData.isOptedOut) {
firstPartyData.isOptedOut = respJson.isOptedOut;
}
if (respJson.isOptedOut === true) {
firstPartyData.group = OPT_OUT;
storeData(FIRST_PARTY_KEY, JSON.stringify(firstPartyData), allowedStorage);
defineEmptyDataAndFireCallback()
return;
}
}
if ('pid' in respJson) {
firstPartyData.pid = respJson.pid;
}
if ('ls' in respJson) {
if (respJson.ls === false) {
defineEmptyDataAndFireCallback();
return
}
// If data is empty, means we should save as INVALID_ID
if (respJson.data == '') {
respJson.data = INVALID_ID;
} else {
// If data is a single string, assume it is an id with source intentiq.com
if (respJson.data && typeof respJson.data === 'string') {
respJson.data = { eids: [respJson.data] }
}
}
partnerData.data = respJson.data;
}
if (rrttStrtTime && rrttStrtTime > 0) {
partnerData.rrtt = Date.now() - rrttStrtTime;
}
if (respJson.data?.eids) {
runtimeEids = respJson.data.eids
callback(respJson.data.eids);
firePartnerCallback()
const encryptedData = encryptData(JSON.stringify(respJson.data.eids))
partnerData.data = encryptedData;
} else {
callback();
firePartnerCallback()
}
storeFirstPartyData();
} else {
callback();
firePartnerCallback()
}
},
error: error => {
logError(MODULE_NAME + ': ID fetch encountered an error', error);
callback();
}
};
ajax(url, callbacks, undefined, { method: 'GET', withCredentials: true });
};
const respObj = { callback: resp };
if (runtimeEids?.length) respObj.id = runtimeEids;
return respObj
},
eids: {
'intentIqId': {
source: 'intentiq.com',
atype: 1,
getSource: function (data) {
return data.source;
},
getValue: function (data) {
if (data?.uids?.length) {
return data.uids[0].id
}
return null
},
getUidExt: function (data) {
if (data?.uids?.length) {
return data.uids[0].ext;
}
return null
}
},
}
};
submodule('userId', intentIqIdSubmodule);