Skip to content

Commit

Permalink
Core: add analytics option to markWinningBidAsUsed (#12437)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Nov 16, 2024
1 parent 72566c2 commit 8b039f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
16 changes: 13 additions & 3 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ import {newMetrics, useMetrics} from './utils/perfMetrics.js';
import {defer, GreedyPromise} from './utils/promise.js';
import {enrichFPD} from './fpd/enrichment.js';
import {allConsent} from './consentHandler.js';
import {insertLocatorFrame, markBidAsRendered, renderAdDirect, renderIfDeferred} from './adRendering.js';
import {
insertLocatorFrame,
markBidAsRendered,
markWinningBid,
renderAdDirect,
renderIfDeferred
} from './adRendering.js';
import {getHighestCpm} from './utils/reducers.js';
import {fillVideoDefaults, validateOrtbVideoFields} from './video.js';

Expand Down Expand Up @@ -905,7 +911,7 @@ if (FEATURES.VIDEO) {
*
* @alias module:pbjs.markWinningBidAsUsed
*/
pbjsInstance.markWinningBidAsUsed = function ({adId, adUnitCode}) {
pbjsInstance.markWinningBidAsUsed = function ({adId, adUnitCode, analytics = false}) {
let bids;
if (adUnitCode && adId == null) {
bids = targeting.getWinningBids(adUnitCode);
Expand All @@ -915,7 +921,11 @@ if (FEATURES.VIDEO) {
logWarn('Improper use of markWinningBidAsUsed. It needs an adUnitCode or an adId to function.');
}
if (bids.length > 0) {
auctionManager.addWinningBid(bids[0]);
if (analytics) {
markWinningBid(bids[0]);
} else {
auctionManager.addWinningBid(bids[0]);
}
markBidAsRendered(bids[0])
}
}
Expand Down
70 changes: 44 additions & 26 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3531,7 +3531,7 @@ describe('Unit: Prebid Module', function () {
if (FEATURES.VIDEO) {
describe('markWinningBidAsUsed', function () {
const adUnitCode = '/19968336/header-bid-tag-0';
let winningBid;
let winningBid, markedBid;

beforeEach(() => {
const bidsReceived = $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode);
Expand All @@ -3540,20 +3540,56 @@ describe('Unit: Prebid Module', function () {
// mark the bid and verify the state has changed to RENDERED
winningBid = targeting.getWinningBids(adUnitCode)[0];
auction.getAuctionId = function() { return winningBid.auctionId };
sandbox.stub(events, 'emit');
markedBid = find($$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode).bids,
bid => bid.adId === winningBid.adId);
})

afterEach(() => {
resetAuction();
})

it('marks the bid object as used for the given adUnitCode/adId combination', function () {
// make sure the auction has "state" and does not reload the fixtures
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: winningBid.adId });
const markedBid = find($$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode).bids,
bid => bid.adId === winningBid.adId);

function checkBidRendered() {
expect(markedBid.status).to.equal(BID_STATUS.RENDERED);
});
}

Object.entries({
'analytics=true': {
mark(options = {}) {
$$PREBID_GLOBAL$$.markWinningBidAsUsed(Object.assign({analytics: true}, options))
},
checkBidWon() {
sinon.assert.calledWith(events.emit, EVENTS.BID_WON, markedBid);
}
},
'analytics=false': {
mark(options = {}) {
$$PREBID_GLOBAL$$.markWinningBidAsUsed(options)
},
checkBidWon() {
sinon.assert.notCalled(events.emit)
}
}
}).forEach(([t, {mark, checkBidWon}]) => {
describe(`when ${t}`, () => {
it('marks the bid object as used for the given adUnitCode/adId combination', function () {
mark({ adUnitCode, adId: winningBid.adId });
checkBidRendered();
checkBidWon();
});
it('marks the winning bid object as used for the given adUnitCode', function () {
mark({ adUnitCode });
checkBidRendered();
checkBidWon();
});

it('marks a bid object as used for the given adId', function () {
mark({ adId: winningBid.adId });
checkBidRendered();
checkBidWon();
});
})
})

it('try and mark the bid object, but fail because we supplied the wrong adId', function () {
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode, adId: 'miss' });
Expand All @@ -3562,24 +3598,6 @@ describe('Unit: Prebid Module', function () {

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

it('marks the winning bid object as used for the given adUnitCode', function () {
// make sure the auction has "state" and does not reload the fixtures
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adUnitCode });
const markedBid = find($$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode).bids,
bid => bid.adId === winningBid.adId);

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

it('marks a bid object as used for the given adId', function () {
// make sure the auction has "state" and does not reload the fixtures
$$PREBID_GLOBAL$$.markWinningBidAsUsed({ adId: winningBid.adId });
const markedBid = find($$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode(adUnitCode).bids,
bid => bid.adId === winningBid.adId);

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

Expand Down

0 comments on commit 8b039f4

Please sign in to comment.