SublimDocs

Calling before the script loads

The snippet loads the tracker asynchronously, so your code can run before it is ready. The first line of the snippet handles that:

JavaScript
window.sublim = window.sublim || function () {
  (sublim.q = sublim.q || []).push(arguments);
};

Until the tracker has loaded, window.sublim is a small function that queues its arguments. Once loaded, the tracker sends the queued calls, then adds the methods: sublim.trackEvent, sublim.trackPageview, sublim.trackPurchase and sublim.init.

Two ways to call the tracker

CallBefore the tracker loadsAfter it loads
sublim('trackEvent', 'sign_up', { … })✅ queued, then sent❌ queued but never sent
sublim.trackEvent('sign_up', { … })❌ TypeError, the method doesn't exist yet✅ sent

So:

  • Code that runs after the page has loaded (a click handler, a form callback, an API response) uses sublim.trackEvent(…). This is almost always the case.
  • Code that runs very early (an inline script in <head>) uses the queued form, sublim('trackEvent', …).

A helper that always works

If you can't be sure when your code runs, wrap the call:

JavaScript
function track(name, options) {
  if (!window.sublim) return; // tracker not installed on this page
  if (typeof window.sublim.trackEvent === 'function') {
    window.sublim.trackEvent(name, options);
  } else {
    window.sublim('trackEvent', name, options); // queued until the tracker loads
  }
}

track('appointment_cancelled', { props: { reason: 'customer_request' } });

The same pattern works for trackPurchase and trackPageview.

With the npm package

The exported functions (trackEvent, trackPurchase…) can be called at any time. Calls made before init() are queued (up to 100) and sent once tracking starts.

Good to know

  • sublim('init', {…}) in the queue is ignored. Options go in sublim.o.
  • On localhost (without trackLocalhost), queued calls are dropped, since tracking never starts.