Helper events

When working with online data and tooling, you will have noticed that asynchronuous functions make your life difficult. To make life easier, we have created several “helper events” to which you can subscribe.

Harvest Loaded

Harvest is loaded asynchronuously. We emit an event when Harvest is successfully loaded.

Below you find an example of how to use the harvest-loaded event. Of course, if you know for certain Harvest is not loaded, you could skip the checks for Harvest.

var toDo = function() {
        console.log("Did my to do");
};

if(typeof harvest === "undefined" || !harvest.isInitialized){
        document.addEventListener("harvest-loaded", function() {
                toDo();
        }
} else {
        toDo();
}

Harvest DOM Ready

We have provided a helper function that will emit an event when the DOM is ready. To make use of this function, you are required to set the event listener before DOM ready.

document.addEventListener("harvest-dom-ready", function() {
        console.log("DOM is ready");
}

Harvest and DOM Ready

Sometimes, you might want to perform an action when both the DOM and Harvest are ready. To use this function, make sure you set the event listener before the DOM is ready.

document.addEventListener("harvest-and-dom-ready", function() {
        console.log("DOM is ready");
}

Harvest Profile Received

When you make use of our user profile service in the Harvest Store, each event that is sent to the Harvest Store endpoint will return the updated user profile.

When the profile is returned, it will be processed and put into local storage. After processing, this helper event will be emitted, so you can subscribe to the changes to the user profile.

Read our user profile documentation for more information about the visitor profile service.

Read more about how the information of a user profile is saved in our storage documentation.

document.addEventListener("harvest-profile-received", function (e) {
        console.log(e.eventID); //This logs the ID of the event from which the profile was returned
        console.log(e.profile); //This logs the current profile, could be a message that says no profile is returned
        console.log(e.profileChanged); //This logs true when a new profile is returned or false when the error message is returned
}

Harvest Event Processed

Each time you use sent an event to Harvest, Harvest Collect will process the event. After it has finished processing the event, it will emit this event, to let you know the event finished processing.

This listener could for example be used for debugging purposes when scraping the website.

document.addEventListener("harvest-event-processed", function (e) {
        console.log(e.event); //This outputs the complete event
}