Skip to content

Trace winning auction bids to rendered creatives on the page#922

Open
prk-Jr wants to merge 10 commits into
mainfrom
trace-auction-winners-to-creatives
Open

Trace winning auction bids to rendered creatives on the page#922
prk-Jr wants to merge 10 commits into
mainfrom
trace-auction-winners-to-creatives

Conversation

@prk-Jr

@prk-Jr prk-Jr commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Verifying that a winning server-side auction bid actually rendered on the page was impossible: no TS-owned identifier survived from the auction to the DOM on either render path. This PR mints a trace tuple — (auction_id, slot_id, bidder, bid_id, crid, adm_hash) — server-side and carries it end-to-end.
  • Server-side ad templates (creative_opportunities) and SPA /__ts/page-bids now inject hb_auction_id/hb_crid/hb_adm_hash per slot; /auction responses carry bid.ext.ts = {auction_id, adm_hash} and pass through the upstream crid/bid id (with the legacy synthetic fallback when absent).
  • The client stamps rendered slot elements and iframes with data-ts-* attributes, keeps a per-slot registry at window.tsjs.renders, and fires a tsjs:adRendered CustomEvent — so a creative in the DOM joins back to the server-side auction winner: / auction delivered creative: log lines by inspection, and e2e tooling can await and assert renders. The adm_hash (16-hex SHA-256 prefix of the delivered markup) enables byte-exact content verification on the /auction iframe path and the inject_adm_for_testing path.

Changes

File Change
crates/trusted-server-core/src/auction/types.rs Add Bid.crid and Bid.bid_id (serde-default, skip-if-none); add adm_trace_hash() and Bid::creative_trace_hash() with tests
crates/trusted-server-core/src/auction/mod.rs Re-export adm_trace_hash
crates/trusted-server-core/src/auction/orchestrator.rs Log one auction winner: trace line per winning bid on the sync (run_auction) and dispatched-collect paths
crates/trusted-server-core/src/auction/formats.rs /auction bids carry ext.ts = {auction_id, adm_hash} (hash of post-sanitize/rewrite markup), pass through upstream crid/bid id with legacy fallback; auction delivered creative: info log; tests
crates/trusted-server-core/src/publisher.rs build_bid_map emits hb_auction_id/hb_crid/hb_adm_hash; auction ID threaded through write_bids_to_state (both streaming call sites) and handle_page_bids; debug_bid gains bid_id/crid; tests
crates/trusted-server-core/src/integrations/prebid.rs Extract crid and bid id from PBS bid responses; tests
crates/trusted-server-core/src/integrations/adserver_mock.rs Restore crid/bid_id from the original SSP bid when parsing mediation responses
crates/trusted-server-core/src/integrations/aps.rs New Bid fields set to None (APS carries no creative pre-decode)
crates/trusted-server-core/src/auction/telemetry.rs Test fixture updated for new Bid fields
crates/trusted-server-js/lib/src/core/trace.ts New: window.tsjs.renders registry, data-ts-* stamping (stale attributes removed on re-render), tsjs:adRendered event
crates/trusted-server-js/lib/src/core/auction.ts parseAuctionResponse retains auctionId (bid ext.ts first, top-level id fallback) and admHash
crates/trusted-server-js/lib/src/core/request.ts renderCreativeInline stamps container + iframe and records renders (including rendered:false for rejected/missing-slot)
crates/trusted-server-js/lib/src/core/types.ts AuctionBidData trace fields, RenderRecord, TsjsApi.renders, debug-bid mirror fields
crates/trusted-server-js/lib/src/integrations/gpt/index.ts slotRenderEnded stamps + records; pbRender bridge records debug-adm/pbs-cache serves with the slot element captured at message-receipt time (SPA-navigation safe); debug adm iframe marked
crates/trusted-server-js/lib/test/core/trace.test.ts New: registry, count/overwrite, event, stamping, stale-attribute removal
crates/trusted-server-js/lib/test/core/auction.test.ts Auction-ID retention and ext.ts precedence tests
crates/trusted-server-js/lib/test/core/request.test.ts Stamping/registry/event tests, rejected-render marker test
crates/trusted-server-js/lib/test/integrations/gpt/ad_init.test.ts slotRenderEnded stamping/registry test incl. empty-render and re-render count

Closes

Closes #920

Test plan

  • cargo test-fastly && cargo test-axum (also cargo test-cloudflare && cargo test-spin and the integration parity suite)
  • cargo clippy-fastly && cargo clippy-axum (also clippy-cloudflare, clippy-cloudflare-wasm, clippy-spin-native, clippy-spin-wasm)
  • cargo fmt --all -- --check
  • JS tests: cd crates/trusted-server-js/lib && npx vitest run (415 passed)
  • JS format: cd crates/trusted-server-js/lib && npm run format
  • Docs format: cd docs && npm run format
  • WASM build: cargo build --package trusted-server-adapter-fastly --release --target wasm32-wasip1
  • Manual testing via fastly compute serve
  • Other: new unit tests for adm_trace_hash, bid-map trace fields, /auction ext.ts/crid/bid-id passthrough, PBS crid/bid-id extraction, and JS trace registry/stamping on both render paths

Checklist

  • Changes follow CLAUDE.md conventions
  • No unwrap() in production code — use expect("should ...")
  • Uses tracing macros (not println!)
  • New code has tests
  • No secrets or credentials committed

Mint a trace tuple (auction_id, slot_id, bidder, bid/creative IDs,
adm_hash) server-side and carry it end-to-end on both render paths so a
creative in the DOM can be joined back to the winning bid in server
logs.

Server: Bid gains crid and bid_id (extracted from PBS, restored through
the mock mediator); adm_trace_hash() computes a 16-hex SHA-256 prefix of
the creative markup; build_bid_map emits hb_auction_id/hb_crid/
hb_adm_hash for SSAT and SPA page-bids; /auction responses carry
bid.ext.ts = {auction_id, adm_hash} and pass through upstream crid and
bid id with the legacy synthetic fallback; the orchestrator logs one
"auction winner:" line per winning bid on both the sync and dispatched
collect paths.

Client: new core/trace.ts writes a per-slot render registry to
window.tsjs.renders, stamps slot elements and iframes with data-ts-*
attributes (removing stale ones on re-render), and fires a
tsjs:adRendered CustomEvent. renderCreativeInline, the GPT
slotRenderEnded handler, and the pbRender bridge all record and stamp;
the bridge captures the slot element at message-receipt time so an SPA
navigation during a PBS Cache fetch cannot stamp the new route's slot
with stale data.
@prk-Jr prk-Jr self-assigned this Jul 16, 2026
ChristianPavilonis added a commit that referenced this pull request Jul 17, 2026
…tives

# Conflicts:
#	crates/trusted-server-core/src/auction/formats.rs
#	crates/trusted-server-core/src/auction/orchestrator.rs
#	crates/trusted-server-core/src/auction/telemetry.rs
#	crates/trusted-server-core/src/auction/types.rs
#	crates/trusted-server-core/src/integrations/adserver_mock.rs
#	crates/trusted-server-core/src/integrations/aps.rs
#	crates/trusted-server-core/src/integrations/prebid.rs
#	crates/trusted-server-core/src/publisher.rs
#	crates/trusted-server-js/lib/src/core/auction.ts
#	crates/trusted-server-js/lib/src/core/request.ts
#	crates/trusted-server-js/lib/src/core/types.ts
#	crates/trusted-server-js/lib/src/integrations/gpt/index.ts
prk-Jr added 9 commits July 18, 2026 09:51
Introduce an operator-armed render-trace overlay so the winning auction
recorded in window.tsjs.renders can be confirmed visually on the page,
on both the SSAT/GAM and /auction paths.

Server: GET /_ts/trace toggles a host-only ts-trace cookie and redirects
to /, gated by the new [debug] trace_route_enabled flag (404 when off).
Registered on all four adapters (Fastly/Cloudflare/Axum/Spin) and
documented in trusted-server.example.toml.

Client: while the cookie is armed, TSJS draws a floating
Google-Publisher-Console-style panel summarising every traced slot and a
confirmation badge on each genuinely-rendered creative. The panel reports
honest per-slot status — ok / hidden / gam-only / empty — derived from
separate signals (gamEmpty from GAM's slotRenderEnded, injected = whether
TS actually placed the creative, visible = ancestor-aware visibility) so
it never overclaims a render GAM merely fired. Clicking a row copies the
full record; the badge is shown only on ok slots.
The prod page runs two independent auctions against the same placements:
the one-time SSAT auction on navigation, and an ongoing client-side
/auction driven by GAM refresh through the Prebid.js trustedServer
adapter. Only the SSAT path was traced, so every Prebid render was
invisible to the overlay.

Instrument the /auction path at its authoritative render signal. The
adapter now forwards the server-side trace tuple to Prebid as
meta.tsAuctionId / meta.tsAdmHash, and a bidWon listener records an
`auction`-path entry (servedFrom: prebid) keyed by the ad-unit code with
that auction's own ID, hash and visibility. Only bids carrying
meta.tsAuctionId — i.e. the trustedServer seat — are traced, so a
client-side bidder's render is never attributed to Trusted Server. The
listener only observes and stamps; it does not touch Prebid's rendering.

Also fix an overclaim in the SSAT path: with inject_adm_for_testing off
there is no adm to inject, which left `injected` unset and let
panelStatus fall through to `ok` — claiming a confirmed TS render for a
slot TS had merely targeted. Targeting-only renders now report
`injected: false`, and `ok` requires an explicit confirmed placement so
any future path that omits the signal degrades to gam-only rather than
silently claiming success.
A client framework can replace the ad divs after GPT slots were bound to
them: the publisher's React app serves ids like `ad-header-0-_R_ssr_` and
swaps them for client ids (`ad-header-0-_r_1_`) during hydration. GPT is
left holding slots whose element no longer exists — GAM reports
"defineSlot was called without a corresponding DIV", still fetches a
creative for them, and the bid is silently wasted with nowhere to render.

Waiting for the divs to merely exist cannot help, because at adInit()
time the server-rendered divs are present and are only later replaced.
So rather than delaying the initial ad request, detect the swap after the
fact: a debounced MutationObserver armed after adInit() looks for
TS-defined slots whose element left the document and re-runs adInit(),
which destroys the orphans and re-binds against the live DOM — reusing
the publisher's own slot for that div when they have since defined one.

Bounded deliberately, since each re-bind re-requests the affected slots:
a 250ms quiet period, a 5s watch window (measured: the swap lands ~2-3s
in, so the existing 2s SPA wait would miss it), and at most two re-binds
per page load, with the attempt budget shared across the adInit() the
watcher itself triggers so it cannot loop.

Verified against the live page through the dev proxy: two orphaned slots
were detected and re-bound, leaving zero orphans, with all three slots
tracing to live client-id elements.

(cherry picked from commit e1badbb)
The panel collapsed each slot to a single row with a ×N counter, so a
publisher page that refreshes its slots on every render (autoblog's
ad-service refreshes from its own slotRenderEnded handler) showed a
climbing number instead of what actually happened. Keep an append-only
`window.tsjs.renderLog` alongside the per-slot `renders` registry and
render it newest-first, one entry per render, with a wall-clock time and
a `#N` sequence. The log is trimmed to the most recent entries so a page
that refreshes indefinitely cannot grow it without bound; `renders` still
collapses per slot for "did this ever render" checks.

Also badge every slot that actually shows a creative, not just confirmed
TS renders. Gating the badge on `ok` meant production — where
inject_adm_for_testing is off and TS only applies GAM targeting — never
displayed one at all, since every slot is honestly `gam-only`. The badge
now carries its status colour and mark: green ✓ for a confirmed TS
render, blue ◐ for gam-only. Slots with nothing on screen (`empty`) or
nothing visible (`hidden`) stay unbadged, as there is no creative to
label.

(cherry picked from commit 013f5e6)
The server-side auction runs once per navigation, but the slotRenderEnded
handler read the winning bid out of window.tsjs.bids on every render. That
map never changes, so each publisher-driven GAM refresh re-stamped the
page-load auction's id, bidder and adm hash and labelled itself `ssat` —
claiming a render the server-side auction never produced. A slot refreshed
six times over a minute showed six `ssat` rows all pointing at one auction.

Scope the claim to the render that actually consumes it: adInit arms a
per-slot flag when it applies bid targeting, and the first slotRenderEnded
clears it. Later renders are recorded as `gam-refresh` with the stale
attribution dropped from both the record and the DOM markers, since GAM
re-requested the slot on its own and the returned creative cannot be traced
to any Trusted Server auction.

These rows are where the client-side /auction path will report real
attribution via Prebid's bidWon once that bundle ships; labelling them
`ssat` hid that gap instead of showing it. The /auction recording path is
unchanged.

(cherry picked from commit c4999f7)
Three trace-panel fixes, all surfaced by the gam-refresh rows the previous
commit introduced:

- Restore the `gam:filled`/`gam:empty` marker on gam-refresh rows. It was
  gated on `path === 'ssat'`, which hid GAM's own fill signal on exactly the
  rows where "did GAM fill it this time" is the whole question.
- Stop rendering absent attribution as `? · ?` and `auction ?`. An
  unattributed GAM refresh carries no bidder/hash/auction id by design, so
  the row now says `no TS attribution` and drops the auction segment rather
  than looking like a failed lookup.
- Give each render a page-global `seq`, shown as `#N` on both the panel row
  and the on-creative badge so the two point at each other, and mark the row
  still live for its slot as `◂ current`. The per-slot render count keeps its
  own `×N`. seq is module-scoped, not stored on window.tsjs, so a re-executed
  bundle restarts the sequence instead of handing two renders one number.

(cherry picked from commit c0811bc)
The visible row text already says "no TS attribution" for a gam-refresh, but
the badge title and row hover tooltip still rendered the same absent fields
as `?`, which reads like a failed lookup rather than "there is nothing to
attribute here by design". Switch both to `—`, matching the `gam_empty ?? '—'`
convention the row tooltip already used elsewhere in the same list.

(cherry picked from commit b4908cd)
The pbRender bridge has two branches for serving a winning SSAT bid into
GAM's Universal Creative: fetch from PBS Cache, or use `bid.adm` directly
when present (added by the SSAT inline-creative work, and the only path
production actually exercises for bidders that carry markup inline). The
PBS Cache branch calls recordBridgeRender after replying; the inline-adm
branch replied, fired win/billing beacons, and logged success, but never
called it — so this render path, the strongest confirmation signal SSAT
has (TS supplies the exact bytes GAM's own creative asked for by name),
was invisible to the trace panel. Every SSAT row capped at gam-only even
when this branch was serving real creative.

Add the missing call, matching the PBS Cache branch's placement. Add
regression coverage on both branches — neither had a trace assertion
before, so this gap could recur silently on either one.

(cherry picked from commit 25316dd)
…re absent

Confirmed live on autoblog: Kargo's response carries neither a Prebid Cache
UUID nor an `adid`, so hb_adid was omitted for every SSAT bid from that
bidder. That broke the pbRender bridge's reverse lookup (adId -> hb_adid) for
GAM's Universal Creative postMessage protocol — verified in the browser that
GAM sends real 'Prebid Request' messages with real adIds on this account,
but window.tsjs.bids[slot].hb_adid was undefined for all three winning bids,
so the bridge could never match any of them. Confirmed rendering (injected:
true) was structurally unreachable for this bidder regardless of what GAM
did.

bid.bid_id (the OpenRTB bid's own `id`, always present per spec) already
flows through the pipeline unused for this purpose. It is unique per bid
instance rather than a creative identifier, but that is exactly what hb_adid
needs here: a stable value GAM's Universal Creative echoes back so the
bridge can find the winning bid. Add it as the last-resort fallback, after
cache_id, the APS renderer's bid id, and ad_id — all three still take
priority where present, locked in by test.

(cherry picked from commit 281df38)
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.

Design how to trace winning TS auction to corresponding creatives on the page

1 participant