Connectif's JavaScript triggers the "conectif.loaded" and "connectif.managed.initialized" events throughout its lifecycle, which serve to determine when the JavaScript has been loaded on the page or when it has been successfully initialized.
How to use them
The "connectif.loaded" event is triggered when the Connectif JavaScript finishes loading on the page. So, after this event is triggered, the "window.connectif".
The "connectif.managed.initialized" event will trigger when Connectif JavaScript has initialized. At this point, the "window.connectif.managed" object will be available.
Examples
The "connectif.loaded" event will look like this:
document.addEventListener('connectif.loaded', function onConnectifLoaded() {
console.log('Connectif is loaded');
});
The "connectif.managed.initialized" event will look like this:
document.addEventListener('connectif.managed.initialized', function onConnectifInitialized() {
console.log('Connectif is initialized');
console.log(window.connectif.managed.isInitialized()) // this prints true
// now I can start tracking events
window.connectif.managed.sendEvents([{ type: 'page-visit' }]);
});
Its main use is to know when Connectif is ready to track events. Below is a more complete example for sending events to Connectif:
function trackPageVisit() {
window.connectif.managed.sendEvents([{ type: 'page-visit' }]);
}
if (window.connectif &&
window.connectif.managed &&
window.connectif.managed.isInitialized()) {
trackPageVisit();
} else {
document.addEventListener("connectif.managed.initialized", function onConnectifInitialized() {
trackPageVisit();
},
{ once: true });
}
Keep learning!
To make the most of your Connectif account, we recommend reading these articles next:
- Add Connectif JavaScript to your website, to insert the code and start collecting information about visits to your site.
- Tag integration, to discover all the notifications you can send from your ecommerce to Connectif and how they work.
- Integration with Connectif via API, to synchronize contacts, products, purchases and vouchers.
- Check the integration with your ecommerce, so you can be confident that your data is syncing correctly with your Connectif account.