Skip to main content

Events and listeners

When an important event occurs within @hmcts/cookie-manger, an event is emitted by the library. Utilising the exported on and off functions from the library, you can easily add callbacks to each event which fires. This could be used to disable a third party analytics package, conditionally change the DOM etc.

Adding a listener

         
import cookieManager from '@hmcts/cookie-manager';

const someEventCallback = function (eventData) { ... };
cookieManager.on('<EVENT-NAME-HERE>', someEventCallback);

const config = { ... }
cookieManager.init(config);


Adding an event listener is as simple as using the exposed on function from the library. The first parameter is the event to listen for (see events list below) and the second parameter is the function to use as a callback. Some events will provide data to the function which can then be used for whatever purpose you like.

Listener functions

on (eventName, callback)

Adds a callback/event listener to the specified event, which will be called when the event occurs.

  • param string – eventName – The event to listen for.
  • param Function – callback – The function to be called when the event is emitted.

  • return EventToken – Object which acts as unique identifier for added listener, to be passed to the off function to remove the added listener..

off (eventToken)

Removes the listener identified by the eventToken parameter.

  • param string – eventToken – The EventToken of the callback/listener to remove.

Events list

Event name Callback data Description
CookieManagerLoaded Event fired when cookie banner has finished loading and adding it’s event listeners.
PreferenceFormInitialized Event fired when preferences form is setup and event listeners have been added.
PreferenceFormSubmitted preferences Event fired when preferences form is submitted/saved.
CookieBannerInitialized Event fired when cookie banner is setup and event listeners have been added.
CookieBannerAction actionName Event fired an cookie banner action has been fired, see action.
UserPreferencesSet preferences Event fired when user preferences are set within @hmcts/cookie-manager.
UserPreferencesSaved preferences Event fired when user preferences are saved to a cookie.

Preferences in on/off format.
UserPreferencesLoaded preferences Event fired when user preferences are loaded from cookie or generated from defaults.

Preferences in on/off format.