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

Count bids with function on adapter if present #773

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 6 additions & 1 deletion src/adapters/indexExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,13 @@ var IndexExchangeAdapter = function IndexExchangeAdapter() {
}

return {
callBids: _callBids
callBids: _callBids,
countBids: _countBids
};
};

function _countBids(bid) {
return bid && bid.sizes && bid.sizes.length || null;
}

module.exports = IndexExchangeAdapter;
34 changes: 19 additions & 15 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { uniques, flatten } from './utils';
import { uniques, flatten, add } from './utils';
import {getPriceBucketString} from './cpmBucketManager';

var CONSTANTS = require('./constants.json');
Expand Down Expand Up @@ -40,34 +40,38 @@ function getBidders(bid) {
return bid.bidder;
}

/**
* Bidders handle multiple requests and multiple sizes in different ways.
* A Bidder Adapter can export a `countBids` function if this is anything
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we call this "countExpectedBids" ?

* other than the default behavior (e.g. a bid object in the `bidderRequest` array
* is counted as 1 bid response expected).
* @param bid
* @returns {*|number}
*/
function countExpectedBids(bid) {
const adapter = $$PREBID_GLOBAL$$.getBidderRegistry()[bid.bidder];
const countBids = adapter && adapter.countBids || function() { return 1; };
return countBids(bid);
}

function bidsBackAdUnit(adUnitCode) {
const requested = $$PREBID_GLOBAL$$._bidsRequested
.map(request => request.bids
.filter(bid => bid.placementCode === adUnitCode))
.reduce(flatten)
.map(bid => {
return bid.bidder === 'indexExchange' ?
bid.sizes.length :
1;
}).reduce(add, 0);
.map(countExpectedBids)
.reduce(add, 0);

const received = $$PREBID_GLOBAL$$._bidsReceived.filter(bid => bid.adUnitCode === adUnitCode).length;
return requested === received;
}

function add(a, b) {
return a + b;
}

function bidsBackAll() {
const requested = $$PREBID_GLOBAL$$._bidsRequested
.map(request => request.bids)
.reduce(flatten)
.map(bid => {
return bid.bidder === 'indexExchange' ?
bid.sizes.length :
1;
}).reduce(add, 0);
.map(countExpectedBids)
.reduce(add, 0);

const received = $$PREBID_GLOBAL$$._bidsReceived.length;
return requested === received;
Expand Down
8 changes: 8 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,12 @@ $$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) {
return getWinningBids(adUnitCode);
};

/**
* Returns the adaptermanager.bidderRegistry
* @returns {*}
*/
$$PREBID_GLOBAL$$.getBidderRegistry = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this added to the global? We shouldn't need to expose this. If we need a means to store objects let's put it into a separate file that does not have global access.

return adaptermanager.bidderRegistry;
};

processQue();
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,7 @@ export function shuffle(array) {

return array;
}

export function add(a, b) {
return a + b;
}