Skip to content

Events

The SDK emits events that you can listen to. This can be useful to track the status of the SDK or to know when a slot is loaded.

Usage

Adding an event listener

You can add an event listener by accessing the events property on the Adhese instance and calling the desired event name as a property and call the addEventListener method on it.

js
function listener(location) {
  console.log('Location changed to', location);
}

adhese.events.locationChange.addEventListener(listener);

Removing an event listener

You can remove an event listener by accessing the events property on the Adhese instance and calling the desired event name as a property and call the removeEventListener method on it.

js
adhese.events.locationChange.removeEventListener(listener);

NOTE

It is important to remove event listeners when they are no longer needed to prevent memory leaks.

WARNING

Do not pass an anonymous function to the addEventListener method. This will make it impossible to remove the event listener later on. It is recommended to use a named function instead.

See the MDN article for more information.

Available events

Event nameDescriptionPayload
locationChangeWhen the page location changes.string
consentChangeWhen the consent settings changeboolean
addSlotWhen a slot is added to the pageSlot
removeSlotWhen a slot is removed from the pageSlot
responseReceivedWhen the request sent to the Adhese API returns with adsReadonlyArray<Ad>
requestAdWhen the request is sent out to the APIAdRequestOptions
requestErrorWhen somewhere in the request pipeline an error pops upError
previewReceivedWhen there is a preview detected in the in browser URL and it has a matching ad in the requested response.ReadonlyArray<Ad>
parametersChangeWhen the global parameters are changedMap<string, ReadonlyArray<string> | string>
debugChangeWhen the the debug mode is changedboolean