Widget Lifecycle
The Ekoo widget exposes three JavaScript methods to control its lifecycle: ekooLoad, ekooUnload and ekooReload. These methods are essential for single-page applications (SPA) and dynamic integrations.
window.ekooLoad()
Initializes the widget and scans the DOM for Ekoo containers. This method is called automatically on page load. You only need to call it manually in the following cases:
- Your application is a SPA (React, Vue, Angular, Svelte…) and the container is added dynamically to the DOM.
- You inject the widget after the initial page load (e.g., via a third-party script or GTM).
1// Initialize all widgets on the page2window.ekooLoad();window.ekooUnload()
Removes all Ekoo widgets from the DOM and frees associated resources (event listeners, audio players, network connections). Essential to prevent memory leaks in SPAs.
1// Remove all widgets (call before leaving the page/component)2window.ekooUnload();Memory leaks
ekooUnload() before navigating to another page, old widgets remain in memory and continue consuming resources.window.ekooReload()
Forces a complete widget reload. Equivalent to an ekooUnload() followed by an ekooLoad(). New in v4, this method is particularly useful for dynamic product changes.
1// When the displayed product changes dynamically2document.querySelector('[data-ekoo-product-id]')3 .setAttribute('data-ekoo-product-id', 'new-product-456');45// Unload then re-initialize6window.ekooReload();Display Conditions
The widget only displays when all of the following conditions are met:
- The Ekoo script is loaded and executed.
- A container with the
data-ekooattribute (or an<ekoo-widget>element) is present in the DOM. - The
websiteIdis valid. - A
productIdis defined and matches an existing product in the backoffice. - At least one published audio is associated with the product.
- The requested locale matches an available locale (if specified).
Configuration Resolution
The widget automatically fetches configuration from the Ekoo backoffice based on the websiteId and productId. Local data-* attributes defined in the HTML take precedence over the remote backoffice configuration.
| Source | Behavior |
|---|---|
Local HTML attributes (data-ekoo-*) | Take precedence. Values defined in HTML are always used first. |
| Remote configuration (backoffice) | Fetched automatically. Used for values not defined locally. |
Complete SPA Pattern
Here is the recommended pattern for integrating the Ekoo widget in a single-page application:
1// 1. On product page mount2function onProductPageMount(productId) {3 // Make sure no residual widget exists4 window.ekooUnload();56 // Update the product ID in the DOM7 const container = document.querySelector('[data-ekoo]');8 if (container) {9 container.setAttribute('data-ekoo-product-id', productId);10 }1112 // Load the widget13 window.ekooLoad();14}1516// 2. On product change (without unmounting)17function onProductChange(newProductId) {18 const container = document.querySelector('[data-ekoo]');19 if (container) {20 container.setAttribute('data-ekoo-product-id', newProductId);21 }22 window.ekooReload();23}2425// 3. On component unmount26function onProductPageUnmount() {27 window.ekooUnload();28}Call order