Calling API Methods - API Event Hooks

Event hooks allow you to run your own code during different stages of the foresee trigger code lifecycle, as well as  request a boolean about whether a particular foresee trigger code lifecycle event has been executed.


Sample API Hook Use-cases:

  1. You want to alert a thank you message once the customer accepts the foresee invite.
  2. You want to know whether an invite has already been presented.

Available API Hooks:

  • onLoaded - Fired after trigger code has been loaded.
  • onInitialized - Fired after trigger code bas been initialized.
  • onInviteShown - Fired after the invite has been presented.
  • onInviteAccepted - Fired if the invite has been accepted.
  • onInviteDeclined - Fired if the invite has been declined.
  • onTrackerShown - Fired after the tracker window is shown.

API Hook Methods:

  • .didFire – checks if the event has happened, returns a boolean true or false.
  • .subscribe(cb, once, stragglers) – executes your callback function once the event has happened, returns a subscription object.
    • cb – optional parameter, a function that will be executed once the event fires.  Will also be executed immediately if the event has fired and the stragglers flag is set to true.
    • once – optional parameter, false be default, if set to true will create a one time subscription that will be removed once the event has fired.
    • stragglers – optional parameter, false by default, if set to true will fire the event again for this callback if it has already fired once before.

 

Sample Usage:

Code below can be used to present an alert after invite has been accepted.  Since once flag is set to true, the subscription will be deleted after the event has fired once.  Since the stragglers flag has been set to true, the callback will be executed even if the invite has already been accepted by the time the subscription is created.

// This should appear below the foresee embed snippet

acsReady(function(){
  FSR.onInviteAccepted.subscribe(
    function() {
      alert('Thank you for accepting our invite!');
    }, true, true
  );	
});

Code below can be used to check whether the invite has been presented.

// This should appear below the foresee embed snippet

FSR.onInviteShown.didFire; //returns true or false.

 

Other articles in this section:

  1. Event Logging
  2. API Event Hooks (current article)