Subscribe to events
You can subscribe to specific events raised by the Kiflo snippet code. This helps you customize your integration. Events are mainly used to know when the tracking cookie is ready or when the tracking code is loaded, in order to customize the forms embedded on the given page.
Typical use case
You need to inject the Kiflo tracking code in a hidden field in your contact/signup form. In order to inject the Kiflo tracking code, you need to know when the Kiflo snippet is loaded (and it's loaded asynchronously), and read the tracking code from the tracking cookie.
Subscribing to the event "ready" helps you inject the tracking code at the right time.
Snippet code
In order to enable events on your integration, you need to adapt the snippet code, if it's not already done. You snippet code should look like:
var kjs = window.kjs || function (a) { var c = { apiKey: a.apiKey, callbacks: [], on: function(e,b) { c.callbacks.push([e,b]) } }; var d = document; setTimeout(function () { var b = d.createElement("script"); b.src = a.url || "https://cdn.kiflo.com/k.js", d.getElementsByTagName("script")[0].parentNode.appendChild(b) }); return c; }({ apiKey: "dd78dfcd-8992-4f7d-8e15-3c78085781f3" });
Please pay attention to lines 4 and 5 that enable events.
Event: " ready"
When: This event is raised every time the Kiflo JS snippet is loaded on the page and ready to be used. It is guaranteed to always be raised.
Parameters:
- trackingCode: contains the tracking cookie stored in the cookie, if there is one, undefined otherwise.
Here is an example of how to subscribe to the "ready" event:
kjs.on( 'ready', function(trackingCode) { console.log('Kiflo Tracking is ready and here is the tracking cookie (can be empty): ', trackingCode); // Add your personalization here // Ex: Insert the tracking code in a hidden field in a form });
Event: "cookie.ready"
When: This event is raised when the tracking cookie is available. Either because the page is loaded and the tracking cookie was already known, or because the page is loaded for the first time and the tracking cookie has been saved.
Parameters:
- trackingCode: contains the tracking cookie stored in the cookie, it is always specified.
Here is an example of how to subscribe to the "cookie.ready" event:
kjs.on( 'cookie.ready', function(trackingCode) { console.log('Kiflo Tracking Code: ', trackingCode); // Add your personalization here // Ex: Insert the tracking code in a hidden field in a form });