Ad Mediation
Waterfall between Koah and fallback ad networks using fill callbacks.
See Mediation for how the waterfall and fill work. In JavaScript, you implement it with the onFill and onNoFill callbacks on requestAd, or by branching on the returned AdResult.filled directly.
Backup Ad Network
Load a fallback when Koah has no fill:
window.koah.requestAd({
target: document.getElementById('ad-slot'),
context: { type: 'conversation', question: userMessage, answer: aiResponse },
onFill: () => console.log('Koah ad served'),
onNoFill: () => loadBackupAdNetwork(),
})
The same callbacks work with every context, including static placements. Pass { type: 'static' }:
window.koah.requestAd({
target: document.getElementById('homepage-ad'),
context: { type: 'static' },
onFill: () => console.log('Homepage ad served'),
onNoFill: () => loadBackupAdNetwork(),
})
You can equivalently await the promise and branch on result.filled:
const result = await window.koah.requestAd({
target: document.getElementById('ad-slot'),
context: { type: 'article', title, content },
})
if (!result.filled) {
loadBackupAdNetwork()
}
Pre-Answer to Post-Answer Waterfall
Try requesting an ad as soon as the user sends a message (question only), and fall back to a post-answer request if the first has no fill. Both requests share the same target, so the later render replaces the earlier one.
const adSlot = document.getElementById('ad-slot')
// 1. Try as soon as the question is sent
window.koah.requestAd({
target: adSlot,
context: { type: 'conversation', question: userMessage },
onFill: () => console.log('Filled early, skipping post-answer request'),
onNoFill: () => {
// 2. Fall back once the answer is ready
window.koah.requestAd({
target: adSlot,
context: {
type: 'conversation',
question: userMessage,
answer: aiResponse,
},
})
},
})
Re-using the same target element replaces any previously rendered ad in that slot.
Was this helpful?