Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubi Analytics handles > 1 bidResponse per bidRequest #4224

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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