Skip to content

Commit

Permalink
Prebid core: cached bids should contain the ID of the auction they ac…
Browse files Browse the repository at this point in the history
…tually won (#10541)

* Cached bids should contain the ID of the auction they actually won

* Property latestTargetedAuctionId should be set on all bids in the pool
  • Loading branch information
msmeza authored Oct 10, 2023
1 parent 353bad4 commit 6c7f17f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ export function newTargeting(auctionManager) {
.filter(bid => deepAccess(bid, 'video.context') !== ADPOD)
.filter(isBidUsable);

bidsReceived
.forEach(bid => {
bid.latestTargetedAuctionId = latestAuctionForAdUnit[bid.adUnitCode];
return bid;
});

return getHighestCpmBidsFromBidPool(bidsReceived, getOldestHighestCpmBid);
}

Expand Down
22 changes: 22 additions & 0 deletions test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,15 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(1);
expect(bids[0].adId).to.equal('adid-1');
expect(bids[0].latestTargetedAuctionId).to.equal(2);

useBidCache = false;

bids = targetingInstance.getWinningBids(adUnitCodes);

expect(bids.length).to.equal(1);
expect(bids[0].adId).to.equal('adid-2');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
});

it('should use bidCacheFilterFunction', function() {
Expand Down Expand Up @@ -989,9 +991,13 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(4);
expect(bids[0].adId).to.equal('adid-1');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
expect(bids[1].adId).to.equal('adid-4');
expect(bids[1].latestTargetedAuctionId).to.equal(2);
expect(bids[2].adId).to.equal('adid-5');
expect(bids[2].latestTargetedAuctionId).to.equal(2);
expect(bids[3].adId).to.equal('adid-8');
expect(bids[3].latestTargetedAuctionId).to.equal(2);

// Bid Caching Off, No Filter Function
useBidCache = false;
Expand All @@ -1000,9 +1006,13 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(4);
expect(bids[0].adId).to.equal('adid-2');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
expect(bids[1].adId).to.equal('adid-4');
expect(bids[1].latestTargetedAuctionId).to.equal(2);
expect(bids[2].adId).to.equal('adid-6');
expect(bids[2].latestTargetedAuctionId).to.equal(2);
expect(bids[3].adId).to.equal('adid-8');
expect(bids[3].latestTargetedAuctionId).to.equal(2);

// Bid Caching On AGAIN, No Filter Function (should be same as first time)
useBidCache = true;
Expand All @@ -1011,9 +1021,13 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(4);
expect(bids[0].adId).to.equal('adid-1');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
expect(bids[1].adId).to.equal('adid-4');
expect(bids[1].latestTargetedAuctionId).to.equal(2);
expect(bids[2].adId).to.equal('adid-5');
expect(bids[2].latestTargetedAuctionId).to.equal(2);
expect(bids[3].adId).to.equal('adid-8');
expect(bids[3].latestTargetedAuctionId).to.equal(2);

// Bid Caching On, with Filter Function to Exclude video
useBidCache = true;
Expand All @@ -1026,9 +1040,13 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(4);
expect(bids[0].adId).to.equal('adid-1');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
expect(bids[1].adId).to.equal('adid-4');
expect(bids[1].latestTargetedAuctionId).to.equal(2);
expect(bids[2].adId).to.equal('adid-6');
expect(bids[2].latestTargetedAuctionId).to.equal(2);
expect(bids[3].adId).to.equal('adid-8');
expect(bids[3].latestTargetedAuctionId).to.equal(2);
// filter function should have been called for each cached bid (4 times)
expect(bcffCalled).to.equal(4);

Expand All @@ -1044,9 +1062,13 @@ describe('targeting tests', function () {

expect(bids.length).to.equal(4);
expect(bids[0].adId).to.equal('adid-2');
expect(bids[0].latestTargetedAuctionId).to.equal(2);
expect(bids[1].adId).to.equal('adid-4');
expect(bids[1].latestTargetedAuctionId).to.equal(2);
expect(bids[2].adId).to.equal('adid-6');
expect(bids[2].latestTargetedAuctionId).to.equal(2);
expect(bids[3].adId).to.equal('adid-8');
expect(bids[3].latestTargetedAuctionId).to.equal(2);
// filter function should not have been called
expect(bcffCalled).to.equal(0);
});
Expand Down
16 changes: 11 additions & 5 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {stubAuctionIndex} from '../../helpers/indexStub.js';
import {createBid} from '../../../src/bidfactory.js';
import {enrichFPD} from '../../../src/fpd/enrichment.js';
import {mockFpdEnrichments} from '../../helpers/fpd.js';
import {generateUUID} from '../../../src/utils.js';
var assert = require('chai').assert;
var expect = require('chai').expect;

Expand All @@ -42,11 +43,12 @@ var adUnits = getAdUnits();
var adUnitCodes = getAdUnits().map(unit => unit.code);
var bidsBackHandler = function() {};
const timeout = 2000;
const auctionId = generateUUID();
let auction;

function resetAuction() {
if (auction == null) {
auction = auctionManager.createAuction({adUnits, adUnitCodes, callback: bidsBackHandler, cbTimeout: timeout});
auction = auctionManager.createAuction({adUnits, adUnitCodes, callback: bidsBackHandler, cbTimeout: timeout, labels: undefined, auctionId: auctionId});
}
$$PREBID_GLOBAL$$.setConfig({ enableSendAllBids: false });
auction.getBidRequests = getBidRequests;
Expand Down Expand Up @@ -3302,16 +3304,20 @@ describe('Unit: Prebid Module', function () {
const highestBid = $$PREBID_GLOBAL$$.getHighestUnusedBidResponseForAdUnitCode('/19968336/header-bid-tag-0');
expect(highestBid).to.deep.equal(_bidsReceived[2])
})
})
});

describe('getHighestCpm', () => {
describe('getHighestCpmBids', () => {
after(() => {
resetAuction();
});
it('returns an array containing the highest bid object for the given adUnitCode', function () {
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids('/19968336/header-bid-tag-0');
const adUnitcode = '/19968336/header-bid-tag-0';
targeting.setLatestAuctionForAdUnit(adUnitcode, auctionId)
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids(adUnitcode);
expect(highestCpmBids.length).to.equal(1);
expect(highestCpmBids[0]).to.deep.equal(auctionManager.getBidsReceived()[1]);
const expectedBid = auctionManager.getBidsReceived()[1];
expectedBid.latestTargetedAuctionId = auctionId;
expect(highestCpmBids[0]).to.deep.equal(expectedBid);
});

it('returns an empty array when the given adUnit is not found', function () {
Expand Down

0 comments on commit 6c7f17f

Please sign in to comment.