addAdsCallbacks() is used for adding adserver independent event listeners, which also triggers when using the direct rendering settings in Yield.
When using the Enable direct rendering/exclusion of Prebid.js bids matching conditions setting in Relevant Yield, it's no longer possible to know whether an ad is shown based just upon the the normal event listeners provided by the adserver's JavaScript.
For example, the 'slotRenderEnded' event triggered by Google Ad Manager might say isEmpty: true - which means the adserver returned no ad. But if there is a winning bid that should be rendered directly without adserver involvement - we can't use only that event to trigger logic such as hiding the ad div.
For this purpose, the addAdsCallbacks() function can be used instead. For every slot the following 3 events will be triggered, in order:
- slotRequested
- slotResponseReceived
- slotRenderEnded
The meaning is the same as in the Google Ad Manager Javascript API. But the names here are the same no matter which adserver that is being used.
Example:
window.relevantDigital = window.relevantDigital || {};
relevantDigital.cmd = relevantDigital.cmd || [];
relevantDigital.cmd.push(function() {
const printEvent = (type, ev, adUnitInstance) => {
console.info(`********** ${type} **********`);
console.info('Event: ', ev);
console.info('AdUnitInstance (if any): ', adUnitInstance);
console.info('Is direct rendering: ', ev.directRender);
console.info('Direct rendered bid: ', ev.bid);
console.info('Ad slot: ', ev.slot);
console.info('Is empty: ', ev.isEmpty);
console.info();
};
relevantDigital.addAdsCallbacks({
slotRequested: (...args) => {
printEvent('slotRequested', ...args);
},
slotResponseReceived: (...args) => {
printEvent('slotResponseReceived', ...args);
},
slotRenderEnded: (ev, data) => {
printEvent('slotRenderEnded', ev, data);
},
});
});
The adUnitInstance contains Yield data about the placement, but will be null for slots loaded outside of the relevantDigital.loadPrebid() call, or loaded as a result of the `loadUnknownSlots: true` parameter being supplied to loadPrebid().
In the case of direct rendering, the .directRender and .bid members might exist on the ev object.
Google Ad Manager:
The ev object in the callbacks might be the event object provided by Google Ad Manager when used as an adserver, depending on direct rendering settings and whether a bid is directly rendered or not. If an ad request is actually made, and if a bid is not rendered directly - then all 3 event objects will be the ones provided by GAM.
Microsoft Advertising (Xandr):
An .ad member will be available in the event object, which is the Ad Object provided in the event - when the event is indeed triggered by the adserver.