Bid caching might duplicate ads when running auctions in quick succession
There are use cases where auctions might be triggered immediately/soon after each other (or even in parallel). In these cases we can't be certain that the ad request corresponding to the first auction have completed and its' bids' status have been set to 'rendered'.
In these cases, when re-using the same ad unit code, setTargetingForGPTAsync() will re-use the targeting as the isUnusedBid() function will return true, as seen below:
Test page HTML:
<script src="https://cdn.jsdelivr.net/npm/prebid.js@latest/dist/not-for-prod/prebid.js"></script>
<script src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<div id='div-1' style="min-height:250px;"></div>
<div id='div-2' style="min-height:250px;"></div>
<script>
const sizes = [[300, 250]];
// Ad unit for auction 1
const adUnits1 = [{
code: '/19968336/header-bid-tag-1',
mediaTypes: {banner: {sizes}},
bids: [{
bidder: 'pubmatic',
params: {publisherId: '45710', adSlot: '829342'}, // test placement
}]
}];
// Ad unit for auction 2
const adUnits2 = [{
code: '/19968336/header-bid-tag-1',
mediaTypes: {banner: {sizes}},
bids: [{
bidder: 'appnexus',
params: {placementId: 12345678}, // This won't generate any bid
}],
}];
googletag.pubads().disableInitialLoad();
const slot1 = googletag.defineSlot('/19968336/header-bid-tag-1', sizes, 'div-1')
.addService(googletag.pubads());
const slot2 = googletag.defineSlot('/19968336/header-bid-tag-1', sizes, 'div-2')
.addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
// Enable bid caching
pbjs.setConfig({debug: true, useBidCache: true});
// Auction 1
pbjs.requestBids({
adUnits: adUnits1,
bidsBackHandler: () => {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh([slot1]);
// Auction 2
pbjs.requestBids({
adUnits: adUnits2,
bidsBackHandler: () => {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh([slot2]);
},
});
},
});
</script>
You might need to refresh the page a few times to trigger the duplication. To make sure to see it every time you might want to simulate a slow ad-request by replacing googletag.pubads().refresh([slot1]); with setTimeout(() => googletag.pubads().refresh([slot1]), 3000);.
@samuel-palmer-relevant-digital are you using HBT or the creative.js or the puc?
@patmmccann ..we're using the prebid universal creative, and I think the site we first found it on loaded the (non hb_format dependent) https://cdn.jsdelivr.net/npm/prebid-universal-creative@latest/dist/creative.js URL directly - just like for the test-placement (/19968336/header-bid-tag-1) in the example.
But looking into the code I think it doesn't matter what creative that is used, as the decision to re-use a bid from a previous auction seems to be done "prematurely" based upon if the bid.status have been set to "rendered" or not (even though it might still be set e.g. 100 ms later, as the first auction's ad-request is a bit slow).
I see that this might be a bit tricky problem to solve, as although one might listen for adserver events ("slotRenderEnded" with gpt.js) it might still be possible that the 'Prebid Request' message from a safe-frame hasn't been delivered yet, etc.
(only consider a bid unused if it has been an ad response on the slot where its' targeting was last set, after it was set, and that happened > X ms ago, perhaps would work)
You can hold the bids as might be used if they are targeted and release the holds if they aren't used. HBT becomes a problem bc it doesn't hold the bids or might have selected a different bid than the bid targeted
Cc @zhengweiwithoutthei
Related https://github.com/prebid/Prebid.js/pull/2328 which also implemented the get highest Prebid... Function @dgirardi was proposing eliminating. #2448 also seems to replace the logic there
This is only true for useBidCache:true otherwise in both Prebid and HBT, we filter bid from the latest auction for the ad unit. We can implement a lock on the bids targeted and release it after the ad response is received to avoid this race condition.
I was working on a fix for this using a lock, and noticed that there may be a larger issue. Even with the bid cache disabled, I was able to get the same adId to render twice using the below example. GPT allows adUnitPath to be duplicated across slots, and it looks like pbjs is setting the same targeting on all slots where the adUnitPath matches. Is this expected behavior, or should the targeting associated with a bid be set on at most one GPT slot?
<script src="https://cdn.jsdelivr.net/npm/prebid.js@latest/dist/not-for-prod/prebid.js"></script>
<script src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<div id="div-1" style="min-height: 250px"></div>
<div id="div-2" style="min-height: 250px"></div>
<script>
const sizes = [[300, 250]];
// Ad unit for auction 1
const adUnits1 = [
{
code: "/19968336/header-bid-tag-1",
mediaTypes: { banner: { sizes } },
bids: [
{
bidder: "pubmatic",
params: { publisherId: "45710", adSlot: "829342" }, // test placement
},
],
},
];
googletag.pubads().disableInitialLoad();
const slot1 = googletag
.defineSlot("/19968336/header-bid-tag-1", sizes, "div-1")
.addService(googletag.pubads());
const slot2 = googletag
.defineSlot("/19968336/header-bid-tag-1", sizes, "div-2")
.addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
pbjs.setConfig({ debug: true, useBidCache: false });
pbjs.requestBids({
adUnits: adUnits1,
bidsBackHandler: () => {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh([slot1, slot2]);
},
});
</script>
There are use cases where auctions might be triggered immediately/soon after each other (or even in parallel).
For the same ad unit? Doesn't that mean the ad gets no screen time? Or is there a reason one would want to re-use the same ad unit for different slots, as in the OP?
We can implement a lock on the bids targeted and release it after the ad response is received to avoid this race condition.
A response is not guaranteed to come, Prebid can't distinguish between an ad yet to be rendered and one that never won the GAM auction. So if we are to implement a lock, I think a simpler one based on time makes more sense - something like don't send out the same ad within X seconds.
For the same ad unit? Doesn't that mean the ad gets no screen time? Or is there a reason one would want to re-use the same ad unit for different slots, as in the OP?
Yes for the same ad unit path, but for different divs/ids. There are some sites that are using the same GAM ad unit path, e.g. "/1234/leaderboard" multiple times - for example if there is infinite scrolling. And whenever the user scrolls down it might start new auctions + ad-requests for these placements, which are different - but has the same ad unit path.
A response is not guaranteed to come, Prebid can't distinguish between an ad yet to be rendered and one that never won the GAM auction. So if we are to implement a lock, I think a simpler one based on time makes more sense - something like don't send out the same ad within X seconds.
Sure, as long as it's the time after "slotRenderEnded" in GAM and not after the auction - as there is this lazy load functionality in GAM etc which sometimes makes it "normal" that it takes minutes from the auction ends until the creative code actually executes.
pbjs.setTargetingForGPTAsync(); should optionally include a lock so that the bid cannot get reused for another pbjs.setTargetingForGPTAsync(); call until the lock expires or is removed.
A lock seems like a reasonable way to solve this issue, but is complicated by pbjs setting the same targeting on more than one slot. This can also cause duplicate impressions. I wrote a short design doc explaining these issues and potential solutions - https://docs.google.com/document/d/1wS5gXMuWRPCGNOaUYg89kBtN1810TviqnxmeekZ5Cc0/edit?usp=sharing I wanted to get feedback on the approach before writing all the tests for the slotRenderEnded behavior which will be a bit tricky, especially if we don't address the first issue.
Change for warns on setting targeting on multiple slots was approved, and planning to work on the fix for bid caching using SlotRenderEnded in the next week or two. Since the same targeting can be set on multiple slots, will clear the targeting from every slot where the adId is currently set.
I started implementing @geoff616's suggestion to use slotRenderEnded, but it turns out to not be that straightforward.
While in the simple case we may have one bid submitted to one slot that we can lock and then unlock when that slot signals render ended, in theory any number of bids could be involved, with logic in GAM that decides which one(s) to pick, if any. This could be done for example with enableSendAllBids and picking a "loser" in the GAM creative, or by custom targeting through bidderSettings.
The fact that targeting is customizable also makes the simple case not so simple, as the slot's targeting appears to be the only information from the slotRenderEnded event that we can use to correlate it back to a bid; but we can't assume that for example hb_adid will be there for that purpose.
I think our options are:
-
forget about slotRenderEnded and just add a simple time based lock, e.g.
setConfig({targetingControls: {bidCacheLockTime: 10}}), once a bid has been used for targeting, would prevent it from being used again for 10 seconds. -
ask the publisher to identify a specific targeting key to use for locking with GPT. For example,
setConfig({targetingControls: {gptLockTargetingKey: 'hb_adid'}})would prevent bids with the samehb_adidfrom being used until a slot with thathb_adidsignals slotRenderEnded. The targeting key would basically always behb_adid, but I don't think we can do it by default because that can potentially break people that are doing custom targeting.
ask the publisher to identify a specific targeting key to use for locking with GPT. For example, setConfig({targetingControls: {gptLockTargetingKey: 'hb_adid'}}) would prevent bids with the same hb_adid from being used until a slot with that hb_adid signals slotRenderEnded. The targeting key would basically always be hb_adid, but I don't think we can do it by default because that can potentially break people that are doing custom targeting.
we discussed an array of targeting keys to lock and unlock on render, this seems excessively complicated