Using Listeners

Listen & reacting to analytic lifecycle events

The analytics library comes with a large variety of event listeners.

These can be used to fire custom functionality when a specific lifecycle event occurs.


Using the .on listener will fire a callback every time the event occurs.

analytics.on('pageEnd', ({ payload }) => {
  console.log('event payload', payload)
  // Do your custom logic
  alert('Page view happened')


Using the .once listener will fire a callback once and only once.

analytics.once('track', ({ payload }) => {
  console.log('event payload', payload)
  alert('Track call happened. Call this only once')

Detaching listeners

You can detach listeners by calling the function they return.

function myCustomCallback({ payload }) {
  /* Custom business logic */

const removeListener = analytics.on('identify', myCustomCallback)

// Remove listener

// `myCustomCallback` will no longer trigger