Loading the LeadDyno JavaScript asynchronously with JS

In some cases, loading LeadDyno's JavaScript directly your HTML file is not ideal. As an alternative, you can load the JavaScript asynchronously in your app like so:

function loadLeadDyno() {
  return new Promise((res) => {
    const sc = document.createElement("script");
    sc.onload = function () {
      Promise.resolve(window.LeadDyno);
    };
    sc.src = "https://static.leaddyno.com/js";
    document.head.appendChild(sc);
  });
}

loadLeadDyno();

This function can be placed anywhere in your code to load the LeadDyno JavaScript. The LeadDyno object will then be automatically available on the `window` object, where you can later access it like so:

window.LeadDyno; // the base object
window.LeadDyno.recordVisit(); // record a visit

The function will also resolve with the LeadDyno object, so you can access it and pass it around without directly accessing the window object. See below for examples.

Using a standard JavaScript promise
loadLeadDyno().then(ld => {
  ld.key = "[YOUR_PUBLIC_KEY]";
  ld.recordVisit();
});
Using async/await
const leadDyno = await loadLeadDyno(); // async await form
leadDyno.key = "[YOUR_PUBLIC_KEY]";
leadDyno.recordVisit();

Still need help? Contact Us Contact Us