-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathconsentManagementGpp.js
281 lines (250 loc) · 9.29 KB
/
consentManagementGpp.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
/**
* This module adds GPP consentManagement support to prebid.js. It interacts with
* supported CMPs (Consent Management Platforms) to grab the user's consent information
* and make it available for any GPP supported adapters to read/pass this information to
* their system and for various other features/modules in Prebid.js.
*/
import {deepSetValue, isEmpty, isNumber, isPlainObject, isStr, logError, logInfo, logWarn} from '../src/utils.js';
import {config} from '../src/config.js';
import {gppDataHandler} from '../src/adapterManager.js';
import {enrichFPD} from '../src/fpd/enrichment.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {cmpClient, MODE_CALLBACK} from '../libraries/cmp/cmpClient.js';
import {PbPromise} from '../src/utils/promise.js';
import {buildActivityParams} from '../src/activities/params.js';
import {consentManagementHook, lookupConsentData} from '../libraries/consentManagement/cmUtils.js';
const DEFAULT_CMP = 'iab';
const DEFAULT_CONSENT_TIMEOUT = 10000;
export let userCMP;
export let consentTimeout;
let staticConsentData;
export let consentDataLoaded;
let addedConsentHook = false;
function lookupStaticConsentData() {
return new PbPromise((resolve) => resolve(processCmpData(staticConsentData)));
}
class GPPError {
constructor(message, arg) {
this.message = message;
this.args = arg == null ? [] : [arg];
}
}
export class GPPClient {
apiVersion = '1.1';
static INST;
static get(mkCmp = cmpClient) {
if (this.INST == null) {
const cmp = mkCmp({
apiName: '__gpp',
apiArgs: ['command', 'callback', 'parameter'], // do not pass version - not clear what it's for (or what we should use),
mode: MODE_CALLBACK
});
if (cmp == null) {
throw new GPPError('GPP CMP not found');
}
this.INST = new this(cmp);
}
return this.INST;
}
#resolve;
#reject;
#pending = [];
initialized = false;
constructor(cmp) {
this.cmp = cmp;
[this.#resolve, this.#reject] = [0, 1].map(slot => (result) => {
while (this.#pending.length) {
this.#pending.pop()[slot](result);
}
});
}
/**
* initialize this client - update consent data if already available,
* and set up event listeners to also update on CMP changes
*
* @param pingData
* @returns {Promise<{}>} a promise to GPP consent data
*/
init(pingData) {
const ready = this.updateWhenReady(pingData);
if (!this.initialized) {
if (pingData.gppVersion !== this.apiVersion) {
logWarn(`Unrecognized GPP CMP version: ${pingData.apiVersion}. Continuing using GPP API version ${this.apiVersion}...`);
}
this.initialized = true;
this.cmp({
command: 'addEventListener',
callback: (event, success) => {
if (success != null && !success) {
this.#reject(new GPPError('Received error response from CMP', event));
} else if (event?.pingData?.cmpStatus === 'error') {
this.#reject(new GPPError('CMP status is "error"; please check CMP setup', event));
} else if (this.isCMPReady(event?.pingData || {}) && ['sectionChange', 'signalStatus'].includes(event?.eventName)) {
this.#resolve(this.updateConsent(event.pingData));
}
}
});
}
return ready;
}
refresh() {
return this.cmp({command: 'ping'}).then(this.init.bind(this));
}
/**
* Retrieve and store GPP consent data.
*
* @param pingData
* @returns {Promise<{}>} a promise to GPP consent data
*/
updateConsent(pingData) {
return new PbPromise(resolve => {
if (pingData == null || isEmpty(pingData)) {
throw new GPPError('Received empty response from CMP', pingData);
}
const consentData = processCmpData(pingData);
logInfo('Retrieved GPP consent from CMP:', consentData);
resolve(consentData);
});
}
/**
* Return a promise to GPP consent data, to be retrieved the next time the CMP signals it's ready.
*
* @returns {Promise<{}>}
*/
nextUpdate() {
return new PbPromise((resolve, reject) => {
this.#pending.push([resolve, reject]);
});
}
/**
* Return a promise to GPP consent data, to be retrieved immediately if the CMP is ready according to `pingData`,
* or as soon as it signals that it's ready otherwise.
*
* @param pingData
* @returns {Promise<{}>}
*/
updateWhenReady(pingData) {
return this.isCMPReady(pingData) ? this.updateConsent(pingData) : this.nextUpdate();
}
isCMPReady(pingData) {
return pingData.signalStatus === 'ready';
}
}
function lookupIabConsent() {
return new PbPromise((resolve) => resolve(GPPClient.get().refresh()))
}
// add new CMPs here, with their dedicated lookup function
const cmpCallMap = {
'iab': lookupIabConsent,
'static': lookupStaticConsentData
};
/**
* If consentManagement module is enabled (ie included in setConfig), this hook function will attempt to fetch the
* user's encoded consent string from the supported CMP. Once obtained, the module will store this
* data as part of a gppConsent object which gets transferred to adapterManager's gppDataHandler object.
* This information is later added into the bidRequest object for any supported adapters to read/pass along to their system.
* @param {object} reqBidsConfigObj required; This is the same param that's used in pbjs.requestBids.
* @param {function} fn required; The next function in the chain, used by hook.js
*/
export const requestBidsHook = consentManagementHook('gpp', () => consentDataLoaded);
function processCmpData(consentData) {
if (
(consentData?.applicableSections != null && !Array.isArray(consentData.applicableSections)) ||
(consentData?.gppString != null && !isStr(consentData.gppString)) ||
(consentData?.parsedSections != null && !isPlainObject(consentData.parsedSections))
) {
throw new GPPError('CMP returned unexpected value during lookup process.', consentData);
}
['usnatv1', 'uscav1'].forEach(section => {
if (consentData?.parsedSections?.[section]) {
logWarn(`Received invalid section from cmp: '${section}'. Some functionality may not work as expected`, consentData);
}
});
return storeConsentData(consentData);
}
/**
* Stores CMP data locally in module to make information available in adaptermanager.js for later in the auction
* @param {{}} gppData the result of calling a CMP's `getGPPData` (or equivalent)
*/
export function storeConsentData(gppData = {}) {
const consentData = {
gppString: gppData?.gppString,
applicableSections: gppData?.applicableSections || [],
parsedSections: gppData?.parsedSections || {},
gppData: gppData
};
gppDataHandler.setConsentData(consentData);
return consentData;
}
/**
* Simply resets the module's consentData variable back to undefined, mainly for testing purposes
*/
export function resetConsentData() {
consentDataLoaded = undefined;
userCMP = undefined;
consentTimeout = undefined;
gppDataHandler.reset();
GPPClient.INST = null;
}
/**
* A configuration function that initializes some module variables, as well as add a hook into the requestBids function
* @param {{cmp:string, timeout:number, defaultGdprScope:boolean}} config required; consentManagement module config settings; cmp (string), timeout (int))
*/
export function setConsentConfig(config) {
config = config && config.gpp;
if (!config || typeof config !== 'object') {
logWarn('consentManagement.gpp config not defined, exiting consent manager module');
return;
}
if (isStr(config.cmpApi)) {
userCMP = config.cmpApi;
} else {
userCMP = DEFAULT_CMP;
logInfo(`consentManagement.gpp config did not specify cmp. Using system default setting (${DEFAULT_CMP}).`);
}
if (isNumber(config.timeout)) {
consentTimeout = config.timeout;
} else {
consentTimeout = DEFAULT_CONSENT_TIMEOUT;
logInfo(`consentManagement.gpp config did not specify timeout. Using system default setting (${DEFAULT_CONSENT_TIMEOUT}).`);
}
if (userCMP === 'static') {
if (isPlainObject(config.consentData)) {
staticConsentData = config.consentData;
consentTimeout = null;
} else {
logError(`consentManagement.gpp config with cmpApi: 'static' did not specify consentData. No consents will be available to adapters.`);
}
}
logInfo('consentManagement.gpp module has been activated...');
if (!addedConsentHook) {
getGlobal().requestBids.before(requestBidsHook, 50);
buildActivityParams.before((next, params) => {
return next(Object.assign({gppConsent: gppDataHandler.getConsentData()}, params));
});
}
addedConsentHook = true;
consentDataLoaded = lookupConsentData({
name: 'GPP',
consentDataHandler: gppDataHandler,
cmpHandler: userCMP,
cmpHandlerMap: cmpCallMap,
cmpTimeout: consentTimeout,
getNullConsent: () => storeConsentData()
});
return consentDataLoaded.catch(() => null);
}
config.getConfig('consentManagement', config => setConsentConfig(config.consentManagement));
export function enrichFPDHook(next, fpd) {
return next(fpd.then(ortb2 => {
const consent = gppDataHandler.getConsentData();
if (consent) {
if (Array.isArray(consent.applicableSections)) {
deepSetValue(ortb2, 'regs.gpp_sid', consent.applicableSections);
}
deepSetValue(ortb2, 'regs.gpp', consent.gppString);
}
return ortb2;
}));
}
enrichFPD.before(enrichFPDHook);