@@ -18,16 +18,12 @@ const CMP_VERSION = 2;
18
18
19
19
export let userCMP ;
20
20
export let consentTimeout ;
21
- export let actionTimeout ;
22
21
export let gdprScope ;
23
22
export let staticConsentData ;
23
+ let actionTimeout ;
24
24
25
25
let consentData ;
26
26
let addedConsentHook = false ;
27
- let provisionalConsent ;
28
- let onTimeout ;
29
- let timer = null ;
30
- let actionTimer = null ;
31
27
32
28
// add new CMPs here, with their dedicated lookup function
33
29
const cmpCallMap = {
@@ -43,20 +39,14 @@ function lookupStaticConsentData({onSuccess, onError}) {
43
39
processCmpData ( staticConsentData , { onSuccess, onError} )
44
40
}
45
41
46
- export function setActionTimeout ( timeout = setTimeout ) {
47
- clearTimeout ( timer ) ;
48
- timer = null ;
49
- actionTimer = timeout ( onTimeout , actionTimeout ) ;
50
- }
51
-
52
42
/**
53
43
* This function handles interacting with an IAB compliant CMP to obtain the consent information of the user.
54
44
* Given the async nature of the CMP's API, we pass in acting success/error callback functions to exit this function
55
45
* based on the appropriate result.
56
46
* @param {function({}) } onSuccess acts as a success callback when CMP returns a value; pass along consentObjectfrom CMP
57
47
* @param {function(string, ...{}?) } cmpError acts as an error callback while interacting with CMP; pass along an error message (string) and any extra error arguments (purely for logging)
58
48
*/
59
- function lookupIabConsent ( { onSuccess, onError} ) {
49
+ function lookupIabConsent ( { onSuccess, onError, onEvent } ) {
60
50
function findCMP ( ) {
61
51
let f = window ;
62
52
let cmpFrame ;
@@ -90,11 +80,9 @@ function lookupIabConsent({onSuccess, onError}) {
90
80
function cmpResponseCallback ( tcfData , success ) {
91
81
logInfo ( 'Received a response from CMP' , tcfData ) ;
92
82
if ( success ) {
83
+ onEvent ( tcfData ) ;
93
84
if ( tcfData . gdprApplies === false || tcfData . eventStatus === 'tcloaded' || tcfData . eventStatus === 'useractioncomplete' ) {
94
85
processCmpData ( tcfData , { onSuccess, onError} ) ;
95
- } else {
96
- provisionalConsent = tcfData ;
97
- if ( ! isNaN ( actionTimeout ) && actionTimer === null && timer != null ) setActionTimeout ( ) ;
98
86
}
99
87
} else {
100
88
onError ( 'CMP unable to register callback function. Please check CMP setup.' ) ;
@@ -173,20 +161,27 @@ function lookupIabConsent({onSuccess, onError}) {
173
161
* @param cb A callback that takes: a boolean that is true if the auction should be canceled; an error message and extra
174
162
* error arguments that will be undefined if there's no error.
175
163
*/
176
- export function loadConsentData ( cb , callMap = cmpCallMap , timeout = setTimeout ) {
164
+ function loadConsentData ( cb ) {
177
165
let isDone = false ;
166
+ let timer = null ;
167
+ let onTimeout , provisionalConsent ;
168
+ let cmpLoaded = false ;
178
169
179
- function done ( consentData , shouldCancelAuction , errMsg , ... extraArgs ) {
170
+ function resetTimeout ( timeout ) {
180
171
if ( timer != null ) {
181
172
clearTimeout ( timer ) ;
182
- timer = null ;
183
173
}
184
-
185
- if ( actionTimer != null ) {
186
- clearTimeout ( actionTimer ) ;
187
- actionTimer = null ;
174
+ if ( ! isDone && timeout != null ) {
175
+ if ( timeout === 0 ) {
176
+ onTimeout ( )
177
+ } else {
178
+ timer = setTimeout ( onTimeout , timeout ) ;
179
+ }
188
180
}
181
+ }
189
182
183
+ function done ( consentData , shouldCancelAuction , errMsg , ...extraArgs ) {
184
+ resetTimeout ( null ) ;
190
185
isDone = true ;
191
186
gdprDataHandler . setConsentData ( consentData ) ;
192
187
if ( typeof cb === 'function' ) {
@@ -203,32 +198,30 @@ export function loadConsentData(cb, callMap = cmpCallMap, timeout = setTimeout)
203
198
onSuccess : ( data ) => done ( data , false ) ,
204
199
onError : function ( msg , ...extraArgs ) {
205
200
done ( null , true , msg , ...extraArgs ) ;
201
+ } ,
202
+ onEvent : function ( consentData ) {
203
+ provisionalConsent = consentData ;
204
+ if ( cmpLoaded ) return ;
205
+ cmpLoaded = true ;
206
+ if ( actionTimeout != null ) {
207
+ resetTimeout ( actionTimeout ) ;
208
+ }
206
209
}
207
210
}
208
211
209
- callMap [ userCMP ] ( callbacks ) ;
210
-
211
- if ( ! isDone ) {
212
- onTimeout = ( ) => {
213
- const continueToAuction = ( data ) => {
214
- done ( data , false , 'CMP did not load, continuing auction...' ) ;
215
- }
216
- processCmpData ( provisionalConsent , {
217
- onSuccess : continueToAuction ,
218
- onError : ( ) => continueToAuction ( storeConsentData ( undefined ) )
219
- } )
212
+ onTimeout = ( ) => {
213
+ const continueToAuction = ( data ) => {
214
+ done ( data , false , `${ cmpLoaded ? 'Timeout waiting for user action on CMP' : 'CMP did not load' } , continuing auction...` ) ;
220
215
}
216
+ processCmpData ( provisionalConsent , {
217
+ onSuccess : continueToAuction ,
218
+ onError : ( ) => continueToAuction ( storeConsentData ( undefined ) ) ,
219
+ } )
220
+ }
221
221
222
- if ( consentTimeout === 0 ) {
223
- onTimeout ( ) ;
224
- } else {
225
- if ( timer != null ) {
226
- clearTimeout ( timer ) ;
227
- timer = null ;
228
- }
229
-
230
- timer = timeout ( onTimeout , consentTimeout ) ;
231
- }
222
+ cmpCallMap [ userCMP ] ( callbacks ) ;
223
+ if ( ! ( actionTimeout != null && cmpLoaded ) ) {
224
+ resetTimeout ( consentTimeout ) ;
232
225
}
233
226
}
234
227
@@ -352,17 +345,15 @@ export function setConsentConfig(config) {
352
345
logInfo ( `consentManagement config did not specify cmp. Using system default setting (${ DEFAULT_CMP } ).` ) ;
353
346
}
354
347
355
- if ( isNumber ( config . actionTimeout ) ) {
356
- actionTimeout = config . actionTimeout ;
357
- }
358
-
359
348
if ( isNumber ( config . timeout ) ) {
360
349
consentTimeout = config . timeout ;
361
350
} else {
362
351
consentTimeout = DEFAULT_CONSENT_TIMEOUT ;
363
352
logInfo ( `consentManagement config did not specify timeout. Using system default setting (${ DEFAULT_CONSENT_TIMEOUT } ).` ) ;
364
353
}
365
354
355
+ actionTimeout = isNumber ( config . actionTimeout ) ? config . actionTimeout : null ;
356
+
366
357
// if true, then gdprApplies should be set to true
367
358
gdprScope = config . defaultGdprScope === true ;
368
359
0 commit comments