Advanced: Using the ‘crl8’ API

Overview

Curalate’s Experience snippet adds a crl8 JavaScript global object to your webpage. The global object comes with a number of advanced features that a developer can use to interact with the Experience and control its behavior. Functionality includes:

  • Creating, retrieving, and destroying Experiences, including controlling the loading behavior
  • Listening to events
  • Firing metric events

 

Experience API

Note: Some of the Experience API is asynchronous. This is because certain methods require fetching additional resources. The Promises returned by these functions can be interacted with using normal Promise methods: See MDN’s guide to JavaScript Promises.

General

crl8.ready
@param (callback function)

The rest of the crl8 API is loaded asynchronously from the Curalate snippet, so use this hook to wrap all other calls to the crl8 API. This will ensure that you are accessing the other methods safely.

 

Create, Retrieve, and Destroy Experience Instances

crl8.createExperience
@param (string containerId | node element)
@returns Promise[ExperienceInstance]

One common use case for this method is controlling when an Experience loads. To do so, first place a data-crl8-auto-init=false attribute on the <div> included with your Experience snippet. Then, later, call the crl8.createExperience method to boot up the Experience at a time of your choosing.

crl8.getExperience
@param (string containerId | node element)
@returns Promise[ExperienceInstance]

crl8.getAllExperiences
@returns Promise[Array[ExperienceInstance]]

crl8.destroyExperience
@params (string containerId | node element)


crl8.destroyAllExperiences

 

Event Listeners

The crl8.EventBus object contains a standard set of event listener capabilities. Use this to interact with various kinds of event streams being generated by the Experience.

crl8.EventBus.addEventListener
@param (string type)
@param (function callback)

crl8.EventBus.removeEventListener
@param (string type)
@param (function callback)

crl8.EventBus.dispatchEvent
@param (string type)
@param (various data)

Events

crl8ExperienceRenderStatus
@callback crl8ExperienceRenderStatusCallback
@param ({ experienceContainer: string, isExperienceRendered: boolean }) – Event Data

Experience event fired when experience is rendered.  This allows you to programmatically adjust the display of other items on the page based on whether content is displayed in the experience.  This is most commonly used to show/hide a header on the experience that is controlled by the client.

Metrics

The crl8.metrics object adds capabilities to allow you to send metric events from your Experience into the Curalate system.  For more information on the functionality available, see this additional guide.

API Examples

Manually Initialize Experience

Here is an example on how the API can be used to defer auto-loading of an Experience and to set up an event listener.

 

Hide Header if No Content is Displayed

Here is an example of how the API can be used to show or hide a custom heading when the experience renders. (Requires manual experience initialization).