Skip to main content

Event Module

Introduction

Trigger an event with the name of your choice. The event is (1) emitted via LoyJoy JavaScript API, (2) stored in the LoyJoy Analytics database, and (3) triggers Signal process modules that wait for the event name.

How to Trigger Custom JavaScript With the Event Module

The event module can be used to trigger custom JavaScript without using eval(). To do this, you can choose any event name such as trigger_custom_javascript and then listen for this event on your website. To listen for the event, you can use the standard event listeners in JavaScript. Example:

document.getElementById('my-widget').addEventListener('event', function (e) {
if (e.detail.type === 'trigger_custom_javascript') {
console.log('Custom JavaScript event triggered:', e.detail.type);
// add your code here
}
});

This example assumes that the LoyJoy widget has the id my-widget.

When the event module triggers an event with the name trigger_custom_javascript, the event listener will execute the custom JavaScript code inside the if statement. We have to check for the event type trigger_custom_javascript because the event listener will receive all events triggered by the LoyJoy widget, and we only want to execute our custom JavaScript for the specific event name. Using this method, we can also use different event names to trigger different custom JavaScript code blocks.