forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairIdSystem.js
111 lines (99 loc) · 3.27 KB
/
pairIdSystem.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
/**
* This module adds PAIR Id to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/pairIdSystem
* @requires module:modules/userId
*/
import { submodule } from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js'
import { logInfo } from '../src/utils.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
*/
const MODULE_NAME = 'pairId';
const PAIR_ID_KEY = 'pairId';
const DEFAULT_LIVERAMP_PAIR_ID_KEY = '_lr_pairId';
export const storage = getStorageManager({moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME});
function pairIdFromLocalStorage(key) {
return storage.localStorageIsEnabled() ? storage.getDataFromLocalStorage(key) : null;
}
function pairIdFromCookie(key) {
return storage.cookiesAreEnabled() ? storage.getCookie(key) : null;
}
/** @type {Submodule} */
export const pairIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
/**
* used to specify vendor id
* @type {number}
*/
gvlid: 755,
/**
* decode the stored id value for passing to bid requests
* @function
* @param { string | undefined } value
* @returns {{pairId:string} | undefined }
*/
decode(value) {
return value && Array.isArray(value) ? {'pairId': value} : undefined
},
/**
* Performs action to obtain ID and return a value in the callback's response argument.
* @function getId
* @param {Object} config - The configuration object.
* @param {Object} config.params - The parameters from the configuration.
* @returns {{id: string[] | undefined}} The obtained IDs or undefined if no IDs are found.
*/
getId(config) {
const pairIdsString = pairIdFromLocalStorage(PAIR_ID_KEY) || pairIdFromCookie(PAIR_ID_KEY)
let ids = []
if (pairIdsString && typeof pairIdsString == 'string') {
try {
ids = ids.concat(JSON.parse(atob(pairIdsString)))
} catch (error) {
logInfo(error)
}
}
const configParams = (config && config.params) || {};
if (configParams && configParams.liveramp) {
let LRStorageLocation = configParams.liveramp.storageKey || DEFAULT_LIVERAMP_PAIR_ID_KEY;
const liverampValue = pairIdFromLocalStorage(LRStorageLocation) || pairIdFromCookie(LRStorageLocation);
if (liverampValue) {
try {
const parsedValue = atob(liverampValue);
if (parsedValue) {
const obj = JSON.parse(parsedValue);
if (obj && typeof obj === 'object' && obj.envelope) {
ids = ids.concat(obj.envelope);
} else {
logInfo('Pairid: Parsed object is not valid or does not contain envelope');
}
} else {
logInfo('Pairid: Decoded value is empty');
}
} catch (error) {
logInfo('Pairid: Error parsing JSON: ', error);
}
} else {
logInfo('Pairid: liverampValue for pairId from storage is empty or null');
}
}
if (ids.length == 0) {
logInfo('PairId not found.')
return undefined;
}
return {'id': ids};
},
eids: {
'pairId': {
source: 'google.com',
atype: 571187
},
}
};
submodule('userId', pairIdSubmodule);