#
JS API
Bonbon exposes a JavaScript API to your pages so you can customize your site's behavior.
#
API Methods
#
Programmatically Opening a Rewards Modal
You can programmatically open a Bonbon Reward modal by calling:
window.Bonbon.openModal({component: 'bonbon-rewards'}).load();
Be careful to ensure this happens after the bonbon::initialized
event or else window.Bonbon
will be undefined.
#
Getting the Earn Token
You can access the current user's earn token by calling
window.Bonbon.getEarnToken();
Be careful, it will return falsey if the user is not logged in!
#
SPA
If your application is a Single Page Application (SPA), you must let Bonbon know by either configuring your Web Tags for SPA or by calling enableSPA
after the bonbon::initialized
event:
window.Bonbon.enableSPA();
#
Event Lifecycle
Bonbon Web Tags emit JavaScript events to let the page know about various state changes during a user's visit. The first event in the lifecycle is the bonbon::initialized
event which occurs after the page's DOMContentLoaded
event. You can listen for this event like this:
window.addEventListener('bonbon::initialized', function() {
console.log("Bonbon is initialized!");
});
Here is a complete list of Bonbon Web Tag events: