@@ -55,21 +55,9 @@ const AdKernelAdapter = function AdKernelAdapter() {
55
55
}
56
56
if ( syncedHostZones [ host ] . indexOf ( zone ) === - 1 ) {
57
57
syncedHostZones [ host ] . push ( zone ) ;
58
- insertUserSync ( host , zone ) ;
59
58
}
60
59
} ;
61
60
62
- function insertUserSync ( host , zone ) {
63
- var iframe = utils . createInvisibleIframe ( ) ;
64
- iframe . src = `//${ host } /user-sync?zone=${ zone } ` ;
65
- try {
66
- document . body . appendChild ( iframe ) ;
67
- } catch ( error ) {
68
- /* istanbul ignore next */
69
- utils . logError ( error ) ;
70
- }
71
- }
72
-
73
61
/**
74
62
* Main function to get bid requests
75
63
*/
@@ -82,6 +70,16 @@ const AdKernelAdapter = function AdKernelAdapter() {
82
70
} ) ;
83
71
} ) ;
84
72
} ;
73
+ /**
74
+ * Build flat user-sync queue from host->zones mapping
75
+ */
76
+ this . buildUserSyncQueue = function ( ) {
77
+ return Object . keys ( syncedHostZones )
78
+ . reduce ( ( m , k ) => {
79
+ syncedHostZones [ k ] . forEach ( ( v ) => m . push ( [ k , v ] ) ) ;
80
+ return m ;
81
+ } , [ ] ) ;
82
+ } ;
85
83
86
84
function dispatchRtbRequest ( host , zone , impressions , callback ) {
87
85
let url = buildEndpointUrl ( host ) ;
@@ -160,7 +158,9 @@ const AdKernelAdapter = function AdKernelAdapter() {
160
158
dispatcher . addImp ( bid ) ;
161
159
}
162
160
} ) ;
163
- //process bids grouped into bidrequests
161
+ //start async usersync
162
+ processUserSyncQueue ( dispatcher . buildUserSyncQueue ( ) ) ;
163
+ //process bids grouped into bid requests
164
164
dispatcher . dispatch ( ( bid , imp , bidResp ) => {
165
165
let adUnitId = bid . placementCode ;
166
166
if ( bidResp ) {
@@ -221,6 +221,32 @@ const AdKernelAdapter = function AdKernelAdapter() {
221
221
} ;
222
222
}
223
223
224
+ /**
225
+ * Recursively process user-sync queue
226
+ */
227
+ function processUserSyncQueue ( queue ) {
228
+ if ( queue . length === 0 ) {
229
+ return ;
230
+ }
231
+ let entry = queue . pop ( ) ;
232
+ insertUserSync ( entry [ 0 ] , entry [ 1 ] , ( ) => processUserSyncQueue ( queue ) ) ;
233
+ }
234
+
235
+ /**
236
+ * Insert single iframe user-sync
237
+ */
238
+ function insertUserSync ( host , zone , callback ) {
239
+ var iframe = utils . createInvisibleIframe ( ) ;
240
+ iframe . src = `//sync.adkernel.com/user-sync?zone=${ zone } &r=%2F%2F${ host } %2Fuser-synced%3Fuid%3D%7BUID%7D` ;
241
+ utils . addEventHandler ( iframe , 'load' , callback ) ;
242
+ try {
243
+ document . body . appendChild ( iframe ) ;
244
+ } catch ( error ) {
245
+ /* istanbul ignore next */
246
+ utils . logError ( error ) ;
247
+ }
248
+ }
249
+
224
250
return {
225
251
callBids : baseAdapter . callBids ,
226
252
setBidderCode : baseAdapter . setBidderCode ,
0 commit comments