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

New function markWinningBidAsUsed for marking video bids #2777

Merged
merged 8 commits into from
Jul 20, 2018
27 changes: 27 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,33 @@ $$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) {
.map(removeRequestId);
};

/**
* Mark the winning bid as used, should only be used in conjunction with video
* @typedef {Object} MarkBidRequest
* @property {string} adUnitCode The ad unit code
* @property {string} adId The id representing the ad we want to mark
*
* @alias module:pbjs.markWinningBidAsUsed
*/
$$PREBID_GLOBAL$$.markWinningBidAsUsed = function (markBidRequest) {
let bids = [];

if (markBidRequest.adUnitCode && markBidRequest.adId) {
bids = auctionManager.getBidsReceived()
.filter(bid => bid.adId === markBidRequest.adId && bid.adUnitCode === markBidRequest.adUnitCode);
} else if (markBidRequest.adUnitCode) {
bids = targeting.getWinningBids(markBidRequest.adUnitCode);
} else if (markBidRequest.adId) {
bids = auctionManager.getBidsReceived().filter(bid => bid.adId === markBidRequest.adId);
} else {
utils.logWarn('Inproper usage of markWinningBidAsUsed. It\'ll need an adUnitCode and/or adId to function.');
}

if (bids.length > 0) {
bids[0].status = RENDERED;
}
};

/**
* Get Prebid config options
* @param {Object} options
Expand Down
70 changes: 69 additions & 1 deletion test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createBidReceived
} from 'test/fixtures/fixtures';
import { auctionManager, newAuctionManager } from 'src/auctionManager';
import { targeting, newTargeting } from 'src/targeting';
import { targeting, newTargeting, RENDERED } from 'src/targeting';
import { config as configObj } from 'src/config';
import * as ajaxLib from 'src/ajax';
import * as auctionModule from 'src/auction';
Expand Down Expand Up @@ -1829,6 +1829,74 @@ describe('Unit: Prebid Module', function () {
});
});

describe('markWinningBidAsUsed', () => {
it('marks the bid object as used for the given adUnitCode/adId combination', () => {
// make sure the auction has "state" and does not reload the fixtures
const adUnitCode = '/19968336/header-bid-tag-0';
const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode);
auction.getBidsReceived = function() { return bidsReceived.bids };

// mark the bid and verify the state has changed to RENDERED
const winningBid = targeting.getWinningBids(adUnitCode)[0];
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: winningBid.adId });
const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode)
.bids
.find(bid => bid.adId === winningBid.adId);

expect(markedBid.status).to.equal(RENDERED);
resetAuction();
});

it('try and mark the bid object, but fail because we supplied the wrong adId', () => {
const adUnitCode = '/19968336/header-bid-tag-0';
const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode);
auction.getBidsReceived = function() { return bidsReceived.bids };

const winningBid = targeting.getWinningBids(adUnitCode)[0];
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: 'miss' });
const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode)
.bids
.find(bid => bid.adId === winningBid.adId);

expect(markedBid.status).to.not.equal(RENDERED);
resetAuction();
});

it('marks the winning bid object as used for the given adUnitCode', () => {
// make sure the auction has "state" and does not reload the fixtures
const adUnitCode = '/19968336/header-bid-tag-0';
const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode);
auction.getBidsReceived = function() { return bidsReceived.bids };

// mark the bid and verify the state has changed to RENDERED
const winningBid = targeting.getWinningBids(adUnitCode)[0];
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode });
const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode)
.bids
.find(bid => bid.adId === winningBid.adId);

expect(markedBid.status).to.equal(RENDERED);
resetAuction();
});

it('marks a bid object as used for the given adId', () => {
// make sure the auction has "state" and does not reload the fixtures
const adUnitCode = '/19968336/header-bid-tag-0';
const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode);
auction.getBidsReceived = function() { return bidsReceived.bids };

// mark the bid and verify the state has changed to RENDERED
const winningBid = targeting.getWinningBids(adUnitCode)[0];
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adId: winningBid.adId });
const markedBid = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode)
.bids
.find(bid => bid.adId === winningBid.adId);

expect(markedBid.status).to.equal(RENDERED);
resetAuction();
});
});

describe('setTargetingForAst', () => {
let targeting;
let auctionManagerInstance;
Expand Down