Skip to content

Commit

Permalink
Change prebidServer to call client user syncs if they exist (prebid#1734
Browse files Browse the repository at this point in the history
)
  • Loading branch information
snapwich authored and mattpr committed Oct 31, 2017
1 parent 675f0af commit baa4d03
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
8 changes: 8 additions & 0 deletions modules/prebidServerBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ function PrebidServer() {
});
}

// do client-side syncs if available
requestedBidders.forEach(bidder => {
let clientAdapter = adaptermanager.getBidAdapter(bidder);
if (clientAdapter && clientAdapter.registerSyncs) {
clientAdapter.registerSyncs();
}
});

if (result.bids) {
result.bids.forEach(bidObj => {
let bidRequest = utils.getBidRequest(bidObj.bid_id);
Expand Down
4 changes: 2 additions & 2 deletions modules/rubiconBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ export const spec = {
return bids;
}, []);
},
getUserSyncs: function() {
if (!hasSynced) {
getUserSyncs: function(syncOptions) {
if (!hasSynced && syncOptions.iframeEnabled) {
hasSynced = true;
return {
type: 'iframe',
Expand Down
4 changes: 4 additions & 0 deletions src/adaptermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ exports.setBidderSequence = function (order) {
}
};

exports.getBidAdapter = function(bidder) {
return _bidderRegistry[bidder];
};

exports.setS2SConfig = function (config) {
_s2sConfig = config;
};
Expand Down
33 changes: 19 additions & 14 deletions src/adapters/bidderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export function newBidder(spec) {
getSpec: function() {
return Object.freeze(spec);
},
registerSyncs,
callBids: function(bidderRequest) {
if (!Array.isArray(bidderRequest.bids)) {
return;
Expand Down Expand Up @@ -195,20 +196,7 @@ export function newBidder(spec) {
const responses = [];
function afterAllResponses() {
fillNoBids();
if (spec.getUserSyncs) {
let syncs = spec.getUserSyncs({
iframeEnabled: config.getConfig('userSync.iframeEnabled'),
pixelEnabled: config.getConfig('userSync.pixelEnabled'),
}, responses);
if (syncs) {
if (!Array.isArray(syncs)) {
syncs = [syncs];
}
syncs.forEach((sync) => {
userSync.registerSync(sync.type, spec.code, sync.url)
});
}
}
registerSyncs(responses);
}

const validBidRequests = bidderRequest.bids.filter(filterAndWarn);
Expand Down Expand Up @@ -337,6 +325,23 @@ export function newBidder(spec) {
}
});

function registerSyncs(responses) {
if (spec.getUserSyncs) {
let syncs = spec.getUserSyncs({
iframeEnabled: config.getConfig('userSync.iframeEnabled'),
pixelEnabled: config.getConfig('userSync.pixelEnabled'),
}, responses);
if (syncs) {
if (!Array.isArray(syncs)) {
syncs = [syncs];
}
syncs.forEach((sync) => {
userSync.registerSync(sync.type, spec.code, sync.url)
});
}
}
}

function filterAndWarn(bid) {
if (!spec.isBidRequestValid(bid)) {
logWarn(`Invalid bid sent to bidder ${spec.code}: ${JSON.stringify(bid)}`);
Expand Down
18 changes: 18 additions & 0 deletions test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import Adapter from 'modules/prebidServerBidAdapter';
import adapterManager from 'src/adaptermanager';
import bidmanager from 'src/bidmanager';
import CONSTANTS from 'src/constants.json';
import * as utils from 'src/utils';
Expand Down Expand Up @@ -353,6 +354,23 @@ describe('S2S Adapter', () => {
expect(response).to.have.property('adserverTargeting').that.deep.equals({'foo': 'bar'});
});

it('registers client user syncs when client bid adapter is present', () => {
let rubiconAdapter = {
registerSyncs: sinon.spy()
};
sinon.stub(adapterManager, 'getBidAdapter', () => rubiconAdapter);

server.respondWith(JSON.stringify(RESPONSE_NO_PBS_COOKIE));

adapter.setConfig(CONFIG);
adapter.callBids(REQUEST);
server.respond();

sinon.assert.calledOnce(rubiconAdapter.registerSyncs);

adapterManager.getBidAdapter.restore();
});

it('registers bid responses when server requests cookie sync', () => {
server.respondWith(JSON.stringify(RESPONSE_NO_PBS_COOKIE));

Expand Down
9 changes: 7 additions & 2 deletions test/spec/modules/rubiconBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { spec, masSizeOrdering, resetUserSync } from 'modules/rubiconBidAdapter'
import { parse as parseQuery } from 'querystring';
import { newBidder } from 'src/adapters/bidderFactory';
import { userSync } from 'src/userSync';
import { config } from 'src/config';

var CONSTANTS = require('src/constants.json');

Expand Down Expand Up @@ -779,13 +780,17 @@ describe('the rubicon adapter', () => {
});

it('should register the Emily iframe', () => {
let syncs = spec.getUserSyncs();
let syncs = spec.getUserSyncs({
iframeEnabled: true
});

expect(syncs).to.deep.equal({type: 'iframe', url: emilyUrl});
});

it('should not register the Emily iframe more than once', () => {
let syncs = spec.getUserSyncs();
let syncs = spec.getUserSyncs({
iframeEnabled: true
});
expect(syncs).to.deep.equal({type: 'iframe', url: emilyUrl});

// when called again, should still have only been called once
Expand Down

0 comments on commit baa4d03

Please sign in to comment.