Skip to content

Commit

Permalink
Rubi Analytics handles > 1 bidResponse per bidRequest (#4224)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrmartinez authored and bretg committed Sep 26, 2019
1 parent 0274410 commit d79483b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
32 changes: 20 additions & 12 deletions modules/rubiconAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,26 @@ function sendMessage(auctionId, bidWonId) {
);
}

export function parseBidResponse(bid) {
function getBidPrice(bid) {
if (typeof bid.currency === 'string' && bid.currency.toUpperCase() === 'USD') {
return Number(bid.cpm);
}
// use currency conversion function if present
if (typeof bid.getCpmInNewCurrency === 'function') {
return Number(bid.getCpmInNewCurrency('USD'));
}
utils.logWarn('Rubicon Analytics Adapter: Could not determine the bidPriceUSD of the bid ', bid);
}

export function parseBidResponse(bid, previousBidResponse) {
// The current bidResponse for this matching requestId/bidRequestId
let responsePrice = getBidPrice(bid)
// we need to compare it with the previous one (if there was one)
if (previousBidResponse && previousBidResponse.bidPriceUSD > responsePrice) {
return previousBidResponse;
}
return utils.pick(bid, [
'bidPriceUSD', () => {
if (typeof bid.currency === 'string' && bid.currency.toUpperCase() === 'USD') {
return Number(bid.cpm);
}
// use currency conversion function if present
if (typeof bid.getCpmInNewCurrency === 'function') {
return Number(bid.getCpmInNewCurrency('USD'));
}
utils.logWarn('Rubicon Analytics Adapter: Could not determine the bidPriceUSD of the bid ', bid);
},
'bidPriceUSD', () => responsePrice,
'dealId',
'status',
'mediaType',
Expand Down Expand Up @@ -405,7 +413,7 @@ let rubiconAdapter = Object.assign({}, baseAdapter, {
};
}
bid.clientLatencyMillis = Date.now() - cache.auctions[args.auctionId].timestamp;
bid.bidResponse = parseBidResponse(args);
bid.bidResponse = parseBidResponse(args, bid.bidResponse);
break;
case BIDDER_DONE:
args.bids.forEach(bid => {
Expand Down
50 changes: 50 additions & 0 deletions test/spec/modules/rubiconAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,56 @@ describe('rubicon analytics adapter', function () {
expect(message).to.deep.equal(ANALYTICS_MESSAGE);
});

it('should pick the highest cpm bid if more than one bid per bidRequestId', function () {
// Only want one bid request in our mock auction
let bidRequested = utils.deepClone(MOCK.BID_REQUESTED);
bidRequested.bids.shift();
let auctionInit = utils.deepClone(MOCK.AUCTION_INIT);
auctionInit.adUnits.shift();

// clone the mock bidResponse and duplicate
let duplicateResponse1 = utils.deepClone(BID2);
duplicateResponse1.cpm = 1.0;
duplicateResponse1.adserverTargeting.hb_pb = '1.0';
duplicateResponse1.adserverTargeting.hb_adid = '1111';
let duplicateResponse2 = utils.deepClone(BID2);
duplicateResponse2.cpm = 5.5;
duplicateResponse2.adserverTargeting.hb_pb = '5.5';
duplicateResponse2.adserverTargeting.hb_adid = '5555';
let duplicateResponse3 = utils.deepClone(BID2);
duplicateResponse3.cpm = 0.1;
duplicateResponse3.adserverTargeting.hb_pb = '0.1';
duplicateResponse3.adserverTargeting.hb_adid = '3333';

const setTargeting = {
[duplicateResponse2.adUnitCode]: duplicateResponse2.adserverTargeting
};

const bidWon = Object.assign({}, duplicateResponse2, {
'status': 'rendered'
});

// spoof the auction with just our duplicates
events.emit(AUCTION_INIT, auctionInit);
events.emit(BID_REQUESTED, bidRequested);
events.emit(BID_RESPONSE, duplicateResponse1);
events.emit(BID_RESPONSE, duplicateResponse2);
events.emit(BID_RESPONSE, duplicateResponse3);
events.emit(AUCTION_END, MOCK.AUCTION_END);
events.emit(SET_TARGETING, setTargeting);
events.emit(BID_WON, bidWon);

let message = JSON.parse(requests[0].requestBody);
validate(message);
expect(message.auctions[0].adUnits[0].bids[0].bidResponse.bidPriceUSD).to.equal(5.5);
expect(message.auctions[0].adUnits[0].adserverTargeting.hb_pb).to.equal('5.5');
expect(message.auctions[0].adUnits[0].adserverTargeting.hb_adid).to.equal('5555');
expect(message.bidsWon.length).to.equal(1);
expect(message.bidsWon[0].bidResponse.bidPriceUSD).to.equal(5.5);
expect(message.bidsWon[0].adserverTargeting.hb_pb).to.equal('5.5');
expect(message.bidsWon[0].adserverTargeting.hb_adid).to.equal('5555');
});

it('should send batched message without BID_WON if necessary and further BID_WON events individually', function () {
events.emit(AUCTION_INIT, MOCK.AUCTION_INIT);
events.emit(BID_REQUESTED, MOCK.BID_REQUESTED);
Expand Down

0 comments on commit d79483b

Please sign in to comment.