Skip to content

Bid selector for winner override - #1276

Draft
mdanylov-sigma wants to merge 2 commits into
masterfrom
feature/950-bid-selector
Draft

mdanylov-sigma wants to merge 2 commits into
masterfrom
feature/950-bid-selector

Conversation

@mdanylov-sigma

@mdanylov-sigma mdanylov-sigma commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Implements the bid selector from Part I of the API proposal linked from #950 (part 2 of the issue: let the publisher override the SDK's winner).

The selector is a plugin. Register it once and it applies to every ad unit of the Original, Rendering and Mediation APIs:

final class HighestPriceSelector: NSObject, PrebidBidSelecting {

    let pluginInfo = PrebidPluginInfo(name: "com.example.highest-price", version: "1.0")

    func selectBid(from bids: [Bid], context: BidSelectionContext) -> Bid? {
        bids.max { $0.price < $1.price }
    }
}

Prebid.register(HighestPriceSelector(), as: .bidSelector)

Workflow (proposal 1.4.1)

With no selector registered, nothing changes. With one registered, for each auction the SDK:

  1. Prepares the eligible bids. With filterOutUncachedBids, the Original API offers only bids with a Prebid Cache entry. The Rendering API gets no new cache requirement.
  2. Calls selectBid(from:context:) once, on the main thread. context gives the ad unit's configId and adFormats, since one selector now serves all ad units.
  3. Validates the result by identity. nil, or an object that isn't one of the given bids (logged), ends the auction with no winner: .prebidDemandNoBids, no keywords, no rendering, no winner tracking. There is no fallback to the server's winner, to forceSdkToChooseWinner, or to the cached-bid promotion from PS bid caching failures v2: align with reviewed design doc #1327.
  4. Builds targeting from the selected bid alone. It keeps the bid's own keys and fills in the generic keys (hb_pb, hb_bidder, hb_size, hb_format, hb_deal, hb_cache_id, hb_uuid, hb_cache_host, hb_cache_path, hb_env) from its bidder-specific keys. The bidder code comes from the bid's own hb_bidder_<code>, so aliases work, and keys that Prebid Server truncated are matched by prefix. If a key could match more than one entry, the targeting is invalid rather than guessed. Missing optional keys stay missing, and no other bid's keys are merged. The SDK adds the generic keys to the selected bid's targetingInfo too, so the code that reads the winning bid directly (mediation interstitials, win notifier, SKOverlay) sees the same set.
  5. Validates the targeting. If an ad server integration (Original API, GAM Rendering, Mediation) can't get hb_pb and hb_bidder (plus hb_cache_id with cache reporting), the request ends with the new ResultCode.prebidDemandInvalidTargeting and nothing is attached. Standalone Rendering ad units render the bid's markup, so they don't need the keys.
  6. Commits and completes once.

Changes since the first version of this PR

  • Rebased onto master (pbxproj conflicts with Add fetchDemandBidInfo to the Mediation API ad units #1426's MediationNativeAdUnitTest.swift, and with the video controls properties from Add video banner configuration #1430).
  • bidSelector properties on AdUnitConfig, PrebidRequest, BannerView, the interstitial/rewarded ad units and the Mediation ad units are removed in favour of Prebid.register(_:as: .bidSelector) (proposal 1.4.2). Because the selector is global, this also answers the review question about Original API ad units: they are covered.
  • The selector runs once, before targeting is built. It used to run once more after cache filtering.
  • Losing bids' keys are no longer merged. Before, only hb_pb/hb_bidder/hb_cache_id were stripped from them, so a runner-up pick still left the server winner's other generic keys (hb_size, hb_deal, ...) in the targeting.
  • The hb_* key literals (Bid.isWinning, AdUnit, WinNotifierImpl, SKOverlay, SKAdNetwork) now come from one internal TargetingKey enum, per the review comment.

Public API

  • PrebidBidSelecting: pluginInfo, selectBid(from:context:) (selectBidFrom:context: in Objective-C), optional onRegister() / onUnregister().
  • BidSelectionContext: configId, adFormats.
  • PluginCapability.bidSelector: at most one selector is registered. A second registration is ignored with a warning.
  • ResultCode.prebidDemandInvalidTargeting = 12.

Open points for review

  • The proposal leaves the API shape (1.4.2) open, and multi-impression support still has to be designed. BidSelectionContext is where per-impression data, such as an impression ID, would go later.
  • Goal 1.2 also asks to deprecate the current levers in favour of the new API. This PR doesn't deprecate Targeting.forceSdkToChooseWinner yet: what replaces it should be agreed first.
  • Android parity (Bid selector for winner override prebid-mobile-android#964) still has the earlier per-ad-unit design.

Test plan

  • BidSelectorTests (25): registration and the one-selector limit, default path unchanged, selection, main thread, nil and foreign results, eligibility with and without filterOutUncachedBids, forceSdkToChooseWinner precedence, targeting validation, standalone rendering, and the key mapping (aliases, truncation, other key families, ambiguity).
  • PrebidBidSelectingObjCTest (2): an Objective-C selector registers and unregisters, and the capability and result code are visible from Objective-C.
  • PR test plan (PrebidMobilePRTests) on CI

🤖 Generated with Claude Code



/// A publisher-supplied strategy for choosing the winning bid. See `PrebidBidSelecting`.
public var bidSelector: PrebidBidSelecting?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this feature be enabled for original ad units as well?

/// Standard winner-marker keys (see `Bid.isWinning`). These specifically identify "the" winner,
/// so they must never be attributed to a bid that isn't the (possibly selector-chosen) winner --
/// otherwise `targetingInfo` could still point at a different bid than `winningBid` does.
private static let winningBidMarkerKeys: Set<String> = ["hb_pb", "hb_bidder", "hb_cache_id"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

these markers are duplicated throughout the SDK, consider consolidating them into a single source

@YuriyVelichkoPI

Copy link
Copy Markdown
Contributor

@mdanylov-sigma Is there a community approved spec or proposal describing the way SDK should provide bid selection logic?

@mdanylov-sigma

Copy link
Copy Markdown
Collaborator Author

@mdanylov-sigma Is there a community approved spec or proposal describing the way SDK should provide bid selection logic?

@YuriyVelichkoPI no, there wasn’t a community approved spec or proposal for this. The PrebidBidSelecting protocol / bidSelector design here is my own proposal for how to satisfy that ask - not something reviewed or signed off on beforehand. My fault.

If this doesn’t match what the maintainers/community have in mind, let's close the PR and working out the actual design together first.

@YuriyVelichkoPI

Copy link
Copy Markdown
Contributor

Let's make PRs as draft.

Then - create a shared docoment with the implementation proposal (feature specification). Once it ready and approved for we'll return back to implementation according to that spec. Because for now, without approved requirements, it's hard to review the PR.

@mdanylov-sigma
mdanylov-sigma marked this pull request as draft July 14, 2026 20:46
@mdanylov-sigma

mdanylov-sigma commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

I put together a full proposal: problem statement, API shape (shown as pseudocode), design rationale for each non-obvious decision, alternatives considered, and open questions for maintainers. Posting it for review before this PR goes further.

Full doc, Part I.

@mdanylov-sigma
mdanylov-sigma force-pushed the feature/950-bid-selector branch from e78a1a8 to 0471b3b Compare November 11, 2026 17:19
Aligns the bid selector with Part I of the API proposal linked from #950:

- The selector is a plugin: PrebidBidSelecting gains pluginInfo,
  selectBid(from:context:) and optional onRegister/onUnregister, and is
  registered with Prebid.register(_:as: .bidSelector). At most one is active.
  The per-ad-unit bidSelector properties are gone, so the selector also
  covers the Original API ad units.
- Eligibility first: with filterOutUncachedBids, Original API offers only
  cached bids. Rendering API gets no new cache requirement.
- The selector is called once, on the main thread. A nil or foreign result
  ends the auction with no winner; there is no fallback to the server's
  winner, to forceSdkToChooseWinner or to the cached-bid promotion.
- Targeting comes from the selected bid alone. Generic keys are filled in
  from its bidder-specific keys, resolving aliases and truncated keys; no
  other bid's keys are merged.
- If an ad server integration can't get hb_pb/hb_bidder (and hb_cache_id
  with cache reporting) for the selected bid, the request ends with the new
  ResultCode.prebidDemandInvalidTargeting. Standalone Rendering ad units
  don't need the keys.
- The hb_* key literals now come from one TargetingKey enum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mdanylov-sigma added a commit to prebid/prebid-mobile-android that referenced this pull request Nov 11, 2026
Aligns the bid selector with Part I of the API proposal linked from
prebid/prebid-mobile-ios#950, the same way as prebid/prebid-mobile-ios#1276:

- The selector is a plugin: PrebidBidSelecting gains getPluginInfo(),
  selectBid(bids, context) and default onRegister/onUnregister, and is
  registered with PrebidMobile.registerPlugin(selector,
  PluginCapability.BID_SELECTOR). At most one is active. The per-ad-unit
  setBidSelector methods are gone.
- The selection runs in BidLoader after the response is parsed, once, on
  the main thread, over the eligible bids: with filterOutUncachedBids the
  Original API offers only cached bids. Rendering gets no new cache
  requirement.
- A null or foreign result is a load error with no winner; there is no
  fallback to the server's winner or to the cached-bid promotion.
- Targeting comes from the selected bid alone. Generic keys are filled in
  from its bidder-specific keys, resolving aliases and truncated keys; no
  other bid's keys are merged.
- If an ad server integration can't get the winner keys for the selected
  bid, the load fails with the new ResultCode/FetchDemandResult
  INVALID_TARGETING. Standalone Rendering ad units don't need the keys.
- The hb_* key literals now come from one TargetingKey class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants