diff --git a/modules/prebidServerBidAdapter/index.js b/modules/prebidServerBidAdapter/index.js index 9476b7a76a3..86fdfb2e719 100644 --- a/modules/prebidServerBidAdapter/index.js +++ b/modules/prebidServerBidAdapter/index.js @@ -366,10 +366,48 @@ let nativeEventTrackerMethodMap = { */ let bidIdMap = {}; let nativeAssetCache = {}; // store processed native params to preserve + +/** + * map wurl to auction id and adId for use in the BID_WON event + */ +`const wurlMap = {}; + +/** + * @param {string} auctionId + * @param {string} adId generated value set to bidObject.adId by bidderFactory Bid() + * @param {string} wurl events.winurl passed from prebidServer as wurl + */ +function addWurl(auctionId, adId, wurl) { + if (![auctionId, adId].some(utils.isEmptyStr)) { + wurlMap[`${auctionId}${adId}`] = wurl; + } +} + +/** + * @param {string} auctionId + * @param {string} adId generated value set to bidObject.adId by bidderFactory Bid() + */ +function removeWurl(auctionId, adId) { + if (![auctionId, adId].some(utils.isEmptyStr)) { + wurlMap[`${auctionId}${adId}`] = undefined; + } +} +/** + * @param {string} auctionId + * @param {string} adId generated value set to bidObject.adId by bidderFactory Bid() + * @return {(string|undefined)} events.winurl which was passed as wurl + */ +function getWurl(auctionId, adId) { + if (![auctionId, adId].some(utils.isEmptyStr)) { + return wurlMap[`${auctionId}${adId}`]; + } +} + const OPEN_RTB_PROTOCOL = { buildRequest(s2sBidRequest, bidRequests, adUnits) { let imps = []; let aliases = {}; + const firstBidRequest = bidRequests[0]; // transform ad unit into array of OpenRTB impression objects adUnits.forEach(adUnit => { @@ -515,14 +553,14 @@ const OPEN_RTB_PROTOCOL = { * @type {(string|undefined)} */ const pbAdSlot = utils.deepAccess(adUnit, 'fpd.context.pbAdSlot'); - if (typeof pbAdSlot === 'string' && pbAdSlot) { + if (!utils.isEmptyStr(pbAdSlot)) { utils.deepSetValue(imp, 'ext.context.data.adslot', pbAdSlot); } Object.assign(imp, mediaTypes); // if storedAuctionResponse has been set, pass SRID - const storedAuctionResponseBid = find(bidRequests[0].bids, bid => (bid.adUnitCode === adUnit.code && bid.storedAuctionResponse)); + const storedAuctionResponseBid = find(firstBidRequest.bids, bid => (bid.adUnitCode === adUnit.code && bid.storedAuctionResponse)); if (storedAuctionResponseBid) { utils.deepSetValue(imp, 'ext.prebid.storedauctionresponse.id', storedAuctionResponseBid.storedAuctionResponse.toString()); } @@ -544,6 +582,8 @@ const OPEN_RTB_PROTOCOL = { test: getConfig('debug') ? 1 : 0, ext: { prebid: { + // set ext.prebid.auctiontimestamp with the auction timestamp. Data type is long integer. + auctiontimestamp: firstBidRequest.auctionStart, targeting: { // includewinners is always true for openrtb includewinners: true, @@ -571,9 +611,9 @@ const OPEN_RTB_PROTOCOL = { request.cur = [adServerCur[0]]; } - _appendSiteAppDevice(request, bidRequests[0].refererInfo.referer); + _appendSiteAppDevice(request, firstBidRequest.refererInfo.referer); - const digiTrust = _getDigiTrustQueryParams(bidRequests && bidRequests[0]); + const digiTrust = _getDigiTrustQueryParams(firstBidRequest); if (digiTrust) { utils.deepSetValue(request, 'user.ext.digitrust', digiTrust); } @@ -596,19 +636,19 @@ const OPEN_RTB_PROTOCOL = { } if (bidRequests) { - if (bidRequests[0].gdprConsent) { + if (firstBidRequest.gdprConsent) { // note - gdprApplies & consentString may be undefined in certain use-cases for consentManagement module let gdprApplies; - if (typeof bidRequests[0].gdprConsent.gdprApplies === 'boolean') { - gdprApplies = bidRequests[0].gdprConsent.gdprApplies ? 1 : 0; + if (typeof firstBidRequest.gdprConsent.gdprApplies === 'boolean') { + gdprApplies = firstBidRequest.gdprConsent.gdprApplies ? 1 : 0; } utils.deepSetValue(request, 'regs.ext.gdpr', gdprApplies); - utils.deepSetValue(request, 'user.ext.consent', bidRequests[0].gdprConsent.consentString); + utils.deepSetValue(request, 'user.ext.consent', firstBidRequest.gdprConsent.consentString); } // US Privacy (CCPA) support - if (bidRequests[0].uspConsent) { - utils.deepSetValue(request, 'regs.ext.us_privacy', bidRequests[0].uspConsent); + if (firstBidRequest.uspConsent) { + utils.deepSetValue(request, 'regs.ext.us_privacy', firstBidRequest.uspConsent); } } @@ -658,10 +698,26 @@ const OPEN_RTB_PROTOCOL = { bidRequest.serverResponseTimeMs = serverResponseTimeMs; } - const extPrebidTargeting = utils.deepAccess(bid, 'ext.prebid.targeting'); + // Look for seatbid[].bid[].ext.prebid.bidid and place it in the bidResponse object for use in analytics adapters as 'pbsBidId' + const bidId = utils.deepAccess(bid, 'ext.prebid.bidid'); + if (!utils.isEmptyStr(bidId)) { + bidObject.pbsBidId = bidId; + } + + // store wurl by auctionId and adId so it can be access from the BID_WON event handler + if (!utils.isEmptyStr(utils.deepAccess(bid, 'ext.prebid.event.win'))) { + addWurl(bidRequest.auctionId, bidObject.adId, utils.deepAccess(bid, 'ext.prebid.event.win')); + } + + let extPrebidTargeting = utils.deepAccess(bid, 'ext.prebid.targeting'); // If ext.prebid.targeting exists, add it as a property value named 'adserverTargeting' if (extPrebidTargeting && typeof extPrebidTargeting === 'object') { + // If wurl exists, remove hb_winurl and hb_bidid targeting attributes + if (!utils.isEmptyStr(utils.deepAccess(bid, 'ext.prebid.event.win'))) { + extPrebidTargeting = utils.getDefinedParams(extPrebidTargeting, Object.keys(extPrebidTargeting) + .filter(i => (i.indexOf('hb_winurl') === -1 && i.indexOf('hb_bidid') === -1))); + } bidObject.adserverTargeting = extPrebidTargeting; } @@ -773,6 +829,21 @@ const OPEN_RTB_PROTOCOL = { } }; +/** + * BID_WON event to request the wurl + * @param {Bid} bid the winning bid object + */ +function bidWonHandler(bid) { + const wurl = getWurl(bid.auctionId, bid.adId); + if (!utils.isEmptyStr(wurl)) { + utils.logMessage(`Invoking image pixel for wurl on BID_WIN: "${wurl}"`); + utils.triggerPixel(wurl); + + // remove from wurl cache, since the wurl url was called + removeWurl(bid.auctionId, bid.adId); + } +} + /** * Bidder adapter for Prebid Server */ @@ -856,6 +927,9 @@ export function PrebidServer() { doClientSideSyncs(requestedBidders); } + // Listen for bid won to call wurl + events.on(EVENTS.BID_WON, bidWonHandler); + return Object.assign(this, { callBids: baseAdapter.callBids, setBidderCode: baseAdapter.setBidderCode, diff --git a/modules/rubiconAnalyticsAdapter.js b/modules/rubiconAnalyticsAdapter.js index 28c11ef8264..8117662ad81 100644 --- a/modules/rubiconAnalyticsAdapter.js +++ b/modules/rubiconAnalyticsAdapter.js @@ -87,7 +87,7 @@ function sendMessage(auctionId, bidWonId) { function formatBid(bid) { return utils.pick(bid, [ 'bidder', - 'bidId', bidId => utils.deepAccess(bid, 'bidResponse.seatBidId') || bidId, + 'bidId', bidId => utils.deepAccess(bid, 'bidResponse.pbsBidId') || utils.deepAccess(bid, 'bidResponse.seatBidId') || bidId, 'status', 'error', 'source', (source, bid) => { diff --git a/modules/rubiconBidAdapter.js b/modules/rubiconBidAdapter.js index 3c17f609a37..f09bc51e2c5 100644 --- a/modules/rubiconBidAdapter.js +++ b/modules/rubiconBidAdapter.js @@ -339,6 +339,9 @@ export const spec = { utils.deepSetValue(data.imp[0], 'ext.prebid.storedauctionresponse.id', bidRequest.storedAuctionResponse.toString()); } + // set ext.prebid.auctiontimestamp using auction time + utils.deepSetValue(data.imp[0], 'ext.prebid.auctiontimestamp', bidderRequest.auctionStart); + return { method: 'POST', url: VIDEO_ENDPOINT, diff --git a/src/adapterManager.js b/src/adapterManager.js index 2108bb7a4f6..14f158f9f3c 100644 --- a/src/adapterManager.js +++ b/src/adapterManager.js @@ -69,7 +69,9 @@ function getBids({bidderCode, auctionId, bidderRequestId, adUnits, labels, src}) 'fpd', 'mediaType', 'renderer', - 'storedAuctionResponse' + 'storedAuctionResponse', + 'seatBidId', + 'pbsBidId' ])); let { diff --git a/test/spec/modules/prebidServerBidAdapter_spec.js b/test/spec/modules/prebidServerBidAdapter_spec.js index 4744bef0ee3..fb545793504 100644 --- a/test/spec/modules/prebidServerBidAdapter_spec.js +++ b/test/spec/modules/prebidServerBidAdapter_spec.js @@ -261,7 +261,12 @@ const RESPONSE_OPENRTB = { 'w': 300, 'h': 250, 'ext': { - 'prebid': { 'type': 'banner' }, + 'prebid': { + 'type': 'banner', + 'event': { + 'win': 'http://wurl.org?id=333' + } + }, 'bidder': { 'appnexus': { 'brand_id': 1, @@ -304,6 +309,7 @@ const RESPONSE_OPENRTB_VIDEO = { ext: { prebid: { type: 'video', + bidid: '654321' }, bidder: { appnexus: { @@ -955,6 +961,7 @@ describe('S2S Adapter', function () { aliases: { brealtime: 'appnexus' }, + auctiontimestamp: 1510852447530, targeting: { includebidderkeys: false, includewinners: true @@ -989,6 +996,7 @@ describe('S2S Adapter', function () { aliases: { [alias]: 'appnexus' }, + auctiontimestamp: 1510852447530, targeting: { includebidderkeys: false, includewinners: true @@ -1240,6 +1248,7 @@ describe('S2S Adapter', function () { expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); expect(requestBid.ext.prebid).to.deep.equal({ + auctiontimestamp: 1510852447530, foo: 'bar', targeting: { includewinners: true, @@ -1271,6 +1280,7 @@ describe('S2S Adapter', function () { expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); expect(requestBid.ext.prebid).to.deep.equal({ + auctiontimestamp: 1510852447530, targeting: { includewinners: false, includebidderkeys: true @@ -1304,6 +1314,7 @@ describe('S2S Adapter', function () { expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); expect(requestBid.ext.prebid).to.deep.equal({ + auctiontimestamp: 1510852447530, cache: { vastxml: 'vastxml-set-though-extPrebid.cache.vastXml' }, @@ -1768,6 +1779,80 @@ describe('S2S Adapter', function () { expect(response).to.have.property('vastUrl', 'https://prebid-cache.net/cache?uuid=a5ad3993'); }); + it('handles response cache from ext.prebid.targeting with wurl', function () { + const s2sConfig = Object.assign({}, CONFIG, { + endpoint: 'https://prebidserverurl/openrtb2/auction?querystring=param' + }); + config.setConfig({ s2sConfig }); + const cacheResponse = utils.deepClone(RESPONSE_OPENRTB_VIDEO); + cacheResponse.seatbid.forEach(item => { + item.bid[0].ext.prebid.event = { + win: 'https://wurl.com?a=1&b=2' + }; + item.bid[0].ext.prebid.targeting = { + hb_uuid: 'a5ad3993', + hb_cache_host: 'prebid-cache.net', + hb_cache_path: '/cache' + } + }); + server.respondWith(JSON.stringify(cacheResponse)); + adapter.callBids(VIDEO_REQUEST, BID_REQUESTS, addBidResponse, done, ajax); + server.respond(); + + sinon.assert.calledOnce(addBidResponse); + const response = addBidResponse.firstCall.args[1]; + expect(response).to.have.property('pbsBidId', '654321'); + }); + + it('handles response cache from ext.prebid.targeting with wurl and removes invalid targeting', function () { + const s2sConfig = Object.assign({}, CONFIG, { + endpoint: 'https://prebidserverurl/openrtb2/auction?querystring=param' + }); + config.setConfig({ s2sConfig }); + const cacheResponse = utils.deepClone(RESPONSE_OPENRTB_VIDEO); + cacheResponse.seatbid.forEach(item => { + item.bid[0].ext.prebid.event = { + win: 'https://wurl.com?a=1&b=2' + }; + item.bid[0].ext.prebid.targeting = { + hb_uuid: 'a5ad3993', + hb_cache_host: 'prebid-cache.net', + hb_cache_path: '/cache', + hb_winurl: 'https://hbwinurl.com?a=1&b=2', + hb_bidid: '1234567890', + } + }); + server.respondWith(JSON.stringify(cacheResponse)); + adapter.callBids(VIDEO_REQUEST, BID_REQUESTS, addBidResponse, done, ajax); + server.respond(); + + sinon.assert.calledOnce(addBidResponse); + const response = addBidResponse.firstCall.args[1]; + + expect(response.adserverTargeting).to.deep.equal({ + hb_uuid: 'a5ad3993', + hb_cache_host: 'prebid-cache.net', + hb_cache_path: '/cache' + }); + }); + + it('add request property pbsBidId with ext.prebid.bidid value', function () { + const s2sConfig = Object.assign({}, CONFIG, { + endpoint: 'https://prebidserverurl/openrtb2/auction?querystring=param' + }); + config.setConfig({ s2sConfig }); + const cacheResponse = utils.deepClone(RESPONSE_OPENRTB_VIDEO); + + server.respondWith(JSON.stringify(cacheResponse)); + adapter.callBids(VIDEO_REQUEST, BID_REQUESTS, addBidResponse, done, ajax); + server.respond(); + + sinon.assert.calledOnce(addBidResponse); + const response = addBidResponse.firstCall.args[1]; + + expect(response).to.have.property('pbsBidId', '654321'); + }); + it('handles OpenRTB native responses', function () { sinon.stub(utils, 'getBidRequest').returns({ adUnitCode: 'div-gpt-ad-1460505748561-0', @@ -1796,6 +1881,59 @@ describe('S2S Adapter', function () { }); }); + describe('bid won events', function () { + let server; + let logWarnSpy; + let getUniqueIdentifierStrStub; + + beforeEach(function () { + server = sinon.fakeServer.create(); + sinon.stub(utils, 'triggerPixel'); + sinon.stub(utils, 'insertUserSyncIframe'); + sinon.stub(utils, 'logError'); + logWarnSpy = sinon.spy(utils, 'logWarn'); + getUniqueIdentifierStrStub = sinon.stub(utils, 'getUniqueIdentifierStr').callsFake(() => '101010101010'); + }); + + afterEach(function () { + server.restore(); + utils.triggerPixel.restore(); + utils.insertUserSyncIframe.restore(); + utils.logError.restore(); + logWarnSpy.restore(); + getUniqueIdentifierStrStub.restore(); + }); + + it('should call triggerPixel on bid won event when wurl is defined', function () { + const s2sConfig = Object.assign({}, CONFIG, { + endpoint: 'https://prebid.adnxs.com/pbs/v1/openrtb2/auction' + }); + config.setConfig({ s2sConfig }); + + const clonedResponse = utils.deepClone(RESPONSE_OPENRTB); + clonedResponse.seatbid[0].bid[0].ext.prebid.event = { + win: 'https://wurl.org' + }; + server.respondWith(JSON.stringify(clonedResponse)); + adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); + server.respond(); + + events.emit(CONSTANTS.EVENTS.BID_WON, { + auctionId: '173afb6d132ba3', + adId: '101010101010' + }); + + sinon.assert.calledOnce(addBidResponse); + + const response = addBidResponse.firstCall.args[1]; + expect(response).to.have.property('bidderCode', 'appnexus'); + expect(response).to.have.property('requestId', '123'); + + expect(utils.triggerPixel.called).to.be.true; + expect(utils.triggerPixel.getCall(0).args[0]).to.include('https://wurl.org'); + }); + }) + describe('s2sConfig', function () { let logErrorSpy; diff --git a/test/spec/modules/rubiconBidAdapter_spec.js b/test/spec/modules/rubiconBidAdapter_spec.js index 99928b41d7b..11ed17df5f8 100644 --- a/test/spec/modules/rubiconBidAdapter_spec.js +++ b/test/spec/modules/rubiconBidAdapter_spec.js @@ -1461,6 +1461,7 @@ describe('the rubicon adapter', function () { expect(post.site.content.language).to.equal('en'); expect(imp.ext.rubicon.video.skip).to.equal(1); expect(imp.ext.rubicon.video.skipafter).to.equal(15); + expect(imp.ext.prebid.auctiontimestamp).to.equal(1472239426000); expect(post.user.ext.consent).to.equal('BOJ/P2HOJ/P2HABABMAAAAAZ+A=='); expect(post.user.ext.eids[0].source).to.equal('liveintent.com'); expect(post.user.ext.eids[0].uids[0].id).to.equal('0000-1111-2222-3333');