From f87d04036f918bdbb40d0d1ab3bb79bed680c19b Mon Sep 17 00:00:00 2001 From: dlogachev Date: Wed, 25 Jan 2017 16:25:35 +0200 Subject: [PATCH] added usersync for adkernel adapter --- src/adapters/adkernel.js | 31 +++++++++++++++++++++++------ test/spec/adapters/adkernel_spec.js | 11 ++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/adapters/adkernel.js b/src/adapters/adkernel.js index a78416999c2..b3383f44e9b 100644 --- a/src/adapters/adkernel.js +++ b/src/adapters/adkernel.js @@ -26,6 +26,7 @@ const AdKernelAdapter = function AdKernelAdapter() { const _dispatch = {}; const originalBids = {}; const site = createSite(); + const syncedHostZones = {}; //translate adunit info into rtb impression dispatched by host/zone this.addImp = function (bid) { @@ -48,16 +49,34 @@ const AdKernelAdapter = function AdKernelAdapter() { //save rtb impression for specified ad-network host and zone _dispatch[host][zone].push(imp); originalBids[bidId] = bid; + //perform user-sync + if (!(host in syncedHostZones)){ + syncedHostZones[host] = []; + } + if (syncedHostZones[host].indexOf(zone) === -1) { + syncedHostZones[host].push(zone); + insertUserSync(host, zone); + } }; + function insertUserSync(host, zone) { + var iframe = utils.createInvisibleIframe(); + iframe.src = `//${host}/user-sync?zone=${zone}`; + try { + document.body.appendChild(iframe); + } catch (error) { + utils.logError(error); + } + } + /** * Main function to get bid requests */ this.dispatch = function (callback) { utils._each(_dispatch, (zones, host) => { - utils.logMessage('processing network ' + host); + utils.logMessage(`processing network ${host}`); utils._each(zones, (impressions, zone) => { - utils.logMessage('processing zone ' + zone); + utils.logMessage(`processing zone ${zone}`); dispatchRtbRequest(host, zone, impressions, callback); }); }); @@ -106,7 +125,7 @@ const AdKernelAdapter = function AdKernelAdapter() { * Build ad-network specific endpoint url */ function buildEndpointUrl(host) { - return window.location.protocol + '//' + host + '/rtbg'; + return `${window.location.protocol}//${host}/rtbg`; } function buildRequestParams(zone, rtbReq) { @@ -134,7 +153,7 @@ const AdKernelAdapter = function AdKernelAdapter() { //process individual bids utils._each(bids, (bid) => { if (!validateBidParams(bid.params)) { - utils.logError('Incorrect configuration for adkernel bidder:', bid.params); + utils.logError(`Incorrect configuration for adkernel bidder: ${bid.params}`); bidmanager.addBidResponse(bid.placementCode, createEmptyBidObject(bid)); } else { dispatcher.addImp(bid); @@ -144,10 +163,10 @@ const AdKernelAdapter = function AdKernelAdapter() { dispatcher.dispatch((bid, imp, bidResp) => { let adUnitId = bid.placementCode; if (bidResp) { - utils.logMessage('got response for ' + adUnitId); + utils.logMessage(`got response for ${adUnitId}`); bidmanager.addBidResponse(adUnitId, createBidObject(bidResp, bid, imp.banner.w, imp.banner.h)); } else { - utils.logMessage('got empty response for ' + adUnitId); + utils.logMessage(`got empty response for ${adUnitId}`); bidmanager.addBidResponse(adUnitId, createEmptyBidObject(bid)); } }); diff --git a/test/spec/adapters/adkernel_spec.js b/test/spec/adapters/adkernel_spec.js index 149f9c7a44d..4d692d32056 100644 --- a/test/spec/adapters/adkernel_spec.js +++ b/test/spec/adapters/adkernel_spec.js @@ -241,6 +241,17 @@ describe('Adkernel adapter', () => { expect(result.bids[0].ad).to.include(bidResponse1.seatbid[0].bid[0].nurl); }); + it('should perform usersync for each unique host/zone combination', () => { + ajaxStub.callsArgWith(1, ''); + const expectedSyncUrls = ['http://rtb.adkernel.com/user-sync?zone=1', 'http://rtb.adkernel.com/user-sync?zone=2', + 'http://rtb-private.adkernel.com/user-sync?zone=1']; + sandbox.spy(utils, 'createInvisibleIframe'); + doRequest([bid1_zone1, bid2_zone2, bid2_zone2, bid3_host2]); + expect(utils.createInvisibleIframe.calledThrice); + let userSyncUrls = utils.createInvisibleIframe.returnValues.map( val => val.src); + expect(userSyncUrls).to.be.eql(expectedSyncUrls); + }); + }); describe('adapter aliasing', () => {