forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33acrossIdSystem.js
261 lines (215 loc) · 7.09 KB
/
33acrossIdSystem.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
/**
* This module adds 33acrossId to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/33acrossIdSystem
* @requires module:modules/userId
*/
import { logMessage, logError, logWarn } from '../src/utils.js';
import { ajaxBuilder } from '../src/ajax.js';
import { submodule } from '../src/hook.js';
import { uspDataHandler, coppaDataHandler, gppDataHandler } from '../src/adapterManager.js';
import { getStorageManager, STORAGE_TYPE_COOKIES, STORAGE_TYPE_LOCALSTORAGE } from '../src/storageManager.js';
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
import { domainOverrideToRootDomain } from '../libraries/domainOverrideToRootDomain/index.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 MODULE_NAME = '33acrossId';
const API_URL = 'https://lexicon.33across.com/v1/envelope';
const AJAX_TIMEOUT = 10000;
const CALLER_NAME = 'pbjs';
const GVLID = 58;
const STORAGE_FPID_KEY = '33acrossIdFp';
const STORAGE_TPID_KEY = '33acrossIdTp';
const STORAGE_HEM_KEY = '33acrossIdHm'
const DEFAULT_1PID_SUPPORT = true;
const DEFAULT_TPID_SUPPORT = true;
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
export const domainUtils = {
domainOverride: domainOverrideToRootDomain(storage, MODULE_NAME)
};
function calculateResponseObj(response) {
if (!response.succeeded) {
if (response.error == 'Cookied User') {
logMessage(`${MODULE_NAME}: Unsuccessful response`.concat(' ', response.error));
} else {
logError(`${MODULE_NAME}: Unsuccessful response`.concat(' ', response.error));
}
return {};
}
if (!response.data.envelope) {
logMessage(`${MODULE_NAME}: No envelope was received`);
return {};
}
return {
envelope: response.data.envelope,
fp: response.data.fp,
tp: response.data.tp
};
}
function calculateQueryStringParams({ pid, hem }, gdprConsentData, enabledStorageTypes) {
const uspString = uspDataHandler.getConsentData();
const coppaValue = coppaDataHandler.getCoppa();
const gppConsent = gppDataHandler.getConsentData();
const params = {
pid,
gdpr: 0,
src: CALLER_NAME,
ver: '$prebid.version$',
coppa: Number(coppaValue)
};
if (uspString) {
params.us_privacy = uspString;
}
if (gppConsent) {
const { gppString = '', applicableSections = [] } = gppConsent;
params.gpp = gppString;
params.gpp_sid = encodeURIComponent(applicableSections.join(','))
}
if (gdprConsentData?.consentString) {
params.gdpr_consent = gdprConsentData.consentString;
}
const fp = getStoredValue(STORAGE_FPID_KEY, enabledStorageTypes);
if (fp) {
params.fp = encodeURIComponent(fp);
}
const tp = getStoredValue(STORAGE_TPID_KEY, enabledStorageTypes);
if (tp) {
params.tp = encodeURIComponent(tp);
}
const hemParam = hem || getStoredValue(STORAGE_HEM_KEY, enabledStorageTypes);
if (hemParam) {
params.sha256 = encodeURIComponent(hemParam);
}
return params;
}
function deleteFromStorage(key) {
if (storage.cookiesAreEnabled()) {
const expiredDate = new Date(0).toUTCString();
storage.setCookie(key, '', expiredDate, 'Lax', domainUtils.domainOverride());
}
storage.removeDataFromLocalStorage(key);
}
function storeValue(key, value, { enabledStorageTypes, expires }) {
enabledStorageTypes.forEach(storageType => {
if (storageType === STORAGE_TYPE_COOKIES) {
const expirationInMs = 60 * 60 * 24 * 1000 * expires;
const expirationTime = new Date(Date.now() + expirationInMs);
storage.setCookie(key, value, expirationTime.toUTCString(), 'Lax', domainUtils.domainOverride());
} else if (storageType === STORAGE_TYPE_LOCALSTORAGE) {
storage.setDataInLocalStorage(key, value);
}
});
}
function getStoredValue(key, enabledStorageTypes) {
let storedValue;
enabledStorageTypes.find(storageType => {
if (storageType === STORAGE_TYPE_COOKIES) {
storedValue = storage.getCookie(key);
} else if (storageType === STORAGE_TYPE_LOCALSTORAGE) {
storedValue = storage.getDataFromLocalStorage(key);
}
return !!storedValue;
});
return storedValue;
}
function handleSupplementalId(key, id, storageConfig) {
id
? storeValue(key, id, storageConfig)
: deleteFromStorage(key);
}
/** @type {Submodule} */
export const thirtyThreeAcrossIdSubmodule = {
/**
* used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,
gvlid: GVLID,
/**
* decode the stored id value for passing to bid requests
* @function
* @param {string} id
* @returns {{'33acrossId':{ envelope: string}}}
*/
decode(id) {
return {
[MODULE_NAME]: {
envelope: id
}
};
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleConfig} [config]
* @returns {IdResponse|undefined}
*/
getId({ params = { }, enabledStorageTypes = [], storage: storageConfig = {} }, gdprConsentData) {
if (typeof params.pid !== 'string') {
logError(`${MODULE_NAME}: Submodule requires a partner ID to be defined`);
return;
}
if (gdprConsentData?.gdprApplies === true) {
logWarn(`${MODULE_NAME}: Submodule cannot be used where GDPR applies`);
return;
}
const {
storeFpid = DEFAULT_1PID_SUPPORT,
storeTpid = DEFAULT_TPID_SUPPORT, apiUrl = API_URL,
...options
} = params;
return {
callback(cb) {
ajaxBuilder(AJAX_TIMEOUT)(apiUrl, {
success(response) {
let responseObj = { };
try {
responseObj = calculateResponseObj(JSON.parse(response));
} catch (err) {
logError(`${MODULE_NAME}: ID reading error:`, err);
}
if (!responseObj.envelope) {
['', '_last', '_exp', '_cst'].forEach(suffix => {
deleteFromStorage(`${MODULE_NAME}${suffix}`);
});
}
if (storeFpid) {
handleSupplementalId(STORAGE_FPID_KEY, responseObj.fp, {
enabledStorageTypes,
expires: storageConfig.expires
});
}
if (storeTpid) {
handleSupplementalId(STORAGE_TPID_KEY, responseObj.tp, {
enabledStorageTypes,
expires: storageConfig.expires
});
}
cb(responseObj.envelope);
},
error(err) {
logError(`${MODULE_NAME}: ID error response`, err);
cb();
}
}, calculateQueryStringParams(options, gdprConsentData, enabledStorageTypes), {
method: 'GET',
withCredentials: true
});
}
};
},
domainOverride: domainUtils.domainOverride,
eids: {
'33acrossId': {
source: '33across.com',
atype: 1,
getValue: function(data) {
return data.envelope;
}
},
}
};
submodule('userId', thirtyThreeAcrossIdSubmodule);