Repository navigation
Optable RTD Module: use cached data before the bundle loads - #15748
MO-Thibault wants to merge 7 commits into
Conversation
The default handler now runs as soon as the auction starts: it calls the bundle's window.optable.rtd.handleRtd when available, merges the targeting data cached under OPTABLE_RESOLVED (via the storage manager) when the bundle has not loaded yet, and only waits for the bundle when nothing is cached. A custom handleRtd still runs once the bundle has loaded, unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
This PR introduces changes that may not work on all browsers. According to Babel, the following polyfills may be needed, and they are not automatically included:
The best way to address this is to provide good test coverage, as normal PR checks run unit tests on older browsers. |
Barecheck - Code coverage reportTotal: 91.66%Your code coverage diff: 0.00% ▴ Uncovered files and lines
|
A custom handleRtd was still queued on window.optable.cmd (or behind the bundleUrl onload), so handlers that read cached data before the bundle loads never got to. The module now calls every handler as soon as the auction starts and loads bundleUrl in parallel. A custom handler decides for itself whether to wait for the bundle; the docs and example show how. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
mergeOptableData only awaited handlers whose constructor is a native AsyncFunction. Builds that transpile async functions (gulp --ES5, or a publisher's own Babel setup with older targets) turn the default handler into a plain function, so the RTD callback fired immediately and, with no cached data, the auction went out without waiting for the Optable bundle. Always await the handler instead. Awaiting a non-promise value is a no-op, so synchronous handlers behave the same. Co-Authored-By: Claude <noreply@anthropic.com>
Caches written by older Optable bundles carry UID2 refresh material as `_ref` on each EID. The Optable SDK strips it before EIDs reach bid requests, but the cache path merged the cached ORTB2 as is, so `_ref` was sent to every bidder. Strip it in readCachedTargeting.
|
|
||
| const result = { bundleUrl, adserverTargeting, handleRtd }; | ||
| if (instance !== null) { | ||
| result.instance = instance; |
There was a problem hiding this comment.
Needs test coverage for warning
There was a problem hiding this comment.
Added tests for every warning and error the module logs. The module's line coverage is now 100%. Line 55 is result.instance = instance, though, so I wasn't sure which warning you meant. Let me know if I missed one.
|
|
||
| // Call the handler right away. The default handler copes with the bundle | ||
| // not being loaded yet; a custom handler decides for itself whether to wait. | ||
| mergeOptableData(handleRtdFn, reqBidsConfigObj, optableExtraData, mergeDeep).then(callback, callback); |
There was a problem hiding this comment.
Log error when this fails IMO
There was a problem hiding this comment.
Done in ffb602c. A rejected handler now logs handleRtd failed, continuing the auction without Optable data with the error, then calls the callback.
| const cachedOrtb2 = readCachedTargeting(); | ||
| if (cachedOrtb2) { | ||
| logMessage('Optable bundle not loaded yet, using cached targeting data'); | ||
| mergeFn(reqBidsConfigObj.ortb2Fragments.global, cachedOrtb2); |
There was a problem hiding this comment.
This should also put them in user.ext.eids
There was a problem hiding this comment.
Done in ffb602c, for both the cache path and the targeting-event path. I put the EIDs in user.ext.eids only, not in both places. The ORTB 2.5 translator (moveRule('user.eids', …)) overwrites user.ext.eids with user.eids, which would wipe other ID modules' EIDs for 2.5 bidders. user.ext.eids is also where the userId module puts EIDs and where userIdAsEids reads them. Happy to write both if you'd rather.
| const isBundleLoaded = () => { | ||
| const optableBundle = /** @type {Object} */ (window.optable); | ||
| return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.instance; | ||
| }; |
There was a problem hiding this comment.
This function doesn't support multi instance. We need to consider params.instance as well
| const isBundleLoaded = () => { | |
| const optableBundle = /** @type {Object} */ (window.optable); | |
| return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.instance; | |
| }; | |
| const isBundleLoaded = (instance = null) => { | |
| const optableBundle = /** @type {Object} */ (window.optable); | |
| return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.[instance || 'instance']; | |
| }; |
There was a problem hiding this comment.
Done in ffb602c. isBundleLoaded now takes params.instance as you suggested. I also passed it through to waitForOptableEvent, which had the same hardcoded window.optable.instance. The handler receives it as an optional 4th argument, so custom handlers get it too.
- Honour params.instance when checking whether the bundle has loaded and when reading targeting from the SDK, so named instances work. - Merge EIDs from the cache and the targeting event into user.ext.eids, where the userId module puts them and userIdAsEids reads them. Writing user.eids would let the ORTB 2.5 translator overwrite user.ext.eids. - Log an error when the handler fails instead of swallowing it. - Cover every logged warning and error in tests; module line coverage is now 100%.
params.instance is the name of the SDK instance on window.optable, a string defaulting to 'instance', not an object defaulting to window.optable.rtd.instance. Also document the handler's instance argument, when the bundle counts as loaded, and that cached EIDs go to user.ext.eids.
|
Docs PR: prebid/prebid.github.io#6961 (draft until this one is released). |
Folds MO-Thibault's Optable/Prebid.js mo/optable-rtd-handlertd-early (af3301a) into this PR. Conflicts were the duplicate storage manager setup, the handleRtd typings, the spec imports and the example page (kept the enableTargeting example). Adds OPTABLE_RESOLVED to the storage disclosure.
Type of change
Bugfix
Does this change affect user-facing APIs or examples documented on http://prebid.org?
Description of change
Without
bundleUrl,getBidRequestDataqueues the whole call onwindow.optable.cmd, which only runs once the Optable bundle has loaded. WithbundleUrlit waits for the script's onload. Either way, nothing runs before the bundle is ready. An auction that starts earlier waits for it (up toauctionDelaywithwaitForIt) or goes out with no Optable data, even when the bundle cached that visitor's targeting data on a previous page.This also defeats publisher handlers written to cover that case (call
window.optable.rtd.handleRtdif the bundle is ready, otherwise merge cached data). Since the module only calls them from inside the queue (or after thebundleUrlonload), the bundle is always ready by then and the cache branch never runs. In production we see 3 to 5% of first auctions on a page complete before the bundle loads.The fix calls the handler, default or custom, as soon as the auction starts, and moves the cache logic into the default handler:
window.optable.rtd.handleRtd: call it. Bundles keep overriding that function to change how data is merged.localStorageunderOPTABLE_RESOLVED, read through the storage manager sodeviceAccessand activity controls apply. No wait.OPTABLE_RESOLVEDis the defaultoptableCacheTargetingkey of the Optable SDK's Prebid RTD helper (buildRTD), and the key Optable's bundles write their resolved targeting to. Pages that use the SDK with a different cache key get no cache hit and fall through to step 3, which is the previous behaviour.window.optable.cmd, then continue as in 1. If the bundle has nortd.handleRtd, this is the previous behaviour (wait foroptable-targeting:change).With
bundleUrl, the bundle is still loaded throughloadExternalScript, but no handler waits for onload.Cached EIDs can carry
_ref, cache-only UID2 refresh material written by older Optable bundles. The SDK strips it before EIDs reach bid requests;readCachedTargetingnow strips it too, so it never reaches bidders.A custom
params.handleRtdis now also called right away, before the bundle may have loaded, and decides for itself whether to wait (for example throughwindow.optable.cmd). Handlers written to read the cache before the bundle loads now work as intended. Behaviour change: a custom handler that assumes the bundle is loaded (like the previous docs example, which calledwindow.optable.instancedirectly) now runs too early; the error is caught and the auction continues without Optable data for that call. The docs and the example page now show how to wait for the bundle first. Publishers who wrote a handler only to get the cache behaviour can remove it.mergeOptableDatanow always awaits the handler. It used to await it only whenhandleRtdFn.constructor.name === 'AsyncFunction'. Builds that transpile async functions (gulp build --ES5, or a publisher's own Babel setup with older targets) turn the default handler into a plain function, so that check failed. With no cached data, the RTD callback then fired immediately, and the auction went out without waiting for the bundle. Awaiting a non-promise value is a no-op, so synchronous handlers behave as before. Custom handlers no longer need to stay nativeasyncfunctions.Tests in
test/spec/modules/optableRtdProvider_spec.js:rtd.handleRtdwhen availablertd.handleRtd_refmetadata from cached EIDsmergeOptableDatawaits for a promise returned by a non-async function (e.g. a transpiled async function)getBidRequestDatacalls back right away with cached datahandleRtdis called right away, with and withoutbundleUrlAlso checked in a browser against builds of Prebid 9.53, 10.29 and 11.41, plus 10.29 with
--ES5, with a simulated Optable bundle: cached EIDs are merged at the start of the auction; with nothing cached, the auction waits for the bundle; an unreadable cache entry falls back to waiting; a bundle withoutrtd.handleRtdstill works through the targeting event.modules/optableRtdProvider.mddocuments the default handler and when a custom handler runs; the commented-out custom handler inintegrationExamples/gpt/optableRtdProvider_example.htmlnow waits for the bundle. Docs PR on prebid.github.io to follow.Other information
Supersedes the approach in #14291 (reverted in #14340 for lint), which also read the cache but kept the queue in front of it.
🤖 Generated with Claude Code