BuySellAds Optimize Events
Optimize Events gives you greater flexibility in structuring your website’s interaction with ads. There are three key event types: when an ad slot is closed, when an ad slot reward has been granted, and when an ad is ready to be shown. By leveraging these events, you can enhance user experience and improve ad performance.
OptimizeEventType
The list of available events that you can pass when calling window.optimize.addEventListener
.
rewardedSlotClosed : undefined
This event fires when a rewarded slot's ad has been closed.
RewardedSlotGrantedEvent : payload
This event triggers when a rewarded slot's ad reward has been granted and returns a payload
object.
Return
payload : { amount: number; type: string }
amount: number
: The number of items included in the reward.type: string
: The type of item included in the reward (for example, "coin").
RewardedSlotReadyEvent: { makeRewardedVisible: () => void }
This event fires when a rewarded slot's ad is ready to be made visible.
Return
makeRewardedVisible
: Call this function whenever you are ready to make the ad visible.
addEventListener
Set up an event listener for the Optimize events.
Signature
window.optimize.addEventListener(eventType, eventListener) : Promise<void>
Parameters
eventType : OptimizeEventType
- The event type to be tracked. You can choose one of the available events:
rewardedSlotClosed | rewardedSlotGranted | rewardedSlotReady
.
- The event type to be tracked. You can choose one of the available events:
eventListener : OptimizeEventListener
- The callback that should be called when an event of the provided type has occurred
Example
// When initializing the web page
window.optimize = window.optimize || { queue: [] };
window.optimize.queue.push(() => {
// Set up a listener to track when a rewarded slot's ad is ready to be made visible.
window.optimize.addEventListener('rewardedSlotReady', ({ makeRewardedVisible }) => {
// Perform any necessary logic when a rewarded slot's ad is ready to be made visible.
// Then, call `makeRewardedVisible` to make the ad visible.
makeRewardedVisible();
// Perform any further necessary logic.
});
// Set up a listener to track when a rewarded slot's ad's reward has been granted.
window.optimize.addEventListener('rewardedSlotGranted', ({ payload }) => {
const rewardAmount = payload.amount;
const rewardType = payload.type;
// Perform any necessary logic when a rewarded slot's ad's reward has been granted
// using `rewardAmount` / `payload.amount` and `rewardType` / `payload.type`.
});
// Set up a listener to track when a rewarded slot's ad is closed.
window.optimize.addEventListener('rewardedSlotClosed', () => {
// Perform any necessary logic when a rewarded slot's ad is closed.
});
});
Events are not stored prior to the addition of a given event listener, so it is advised to add any relevant event listeners to Optimize via this function as soon as possible to ensure all relevant events are handled as desired. Note that this function returns a promise. This promise will resolve when the event listener has been added to Optimize.
This function should be used within a command passed to window.optimize.queue.push
to ensure that Optimize functions properly.
removeEventListener
Remove the previously configured event listener for the Optimize events.
Signature
window.optimize.removeEventListener(eventType, eventListener) : Promise<void>
Parameters
eventType : OptimizeEventType
- The event type for which
eventListener
was previously added to trackeventType
eventListener : OptimizeEventListener
- The callback that was provided in a previous
window.optimize.addEventListener
witheventType
Example
// When initializing the web page
window.optimize = window.optimize || { queue: [] };
window.optimize.queue.push(() => {
const handleRewardedSlotClosed = () => {
// Perform any necessary logic when a rewarded slot's ad is closed.
};
// Set up a listener to track when a rewarded slot's ad is closed.
window.optimize.addEventListener('rewardedSlotClosed', handleRewardedSlotClosed).then(() => {
// After the listener has been added to Optimize, if the listener
// should be removed for any reason...
window.optimize.removeEventListener('rewardedSlotClosed', handleRewardedSlotClosed);
});
});
If a previously added event listener for a given event type should no longer track such events, you can use this function to remove that event listener from Optimize. Make sure to wait until the listener has been added to Optimize asynchronously by window.optimize.addEventListener
before attempting to remove it from Optimize.
This function should be used within a command passed to window.optimize.queue.push
to ensure that Optimize functions properly.