Skip to content

Instantly share code, notes, and snippets.

@nicjansma
Last active April 20, 2022 13:20
Show Gist options
  • Save nicjansma/a0cb4342caa67e5c612021f187ed8a60 to your computer and use it in GitHub Desktop.
Save nicjansma/a0cb4342caa67e5c612021f187ed8a60 to your computer and use it in GitHub Desktop.
Boomerang plugin that waits for the specified JavaScript variable to exist on the page before a beacon is sent.
/**
* The `WaitForJsVar` Boomerang plugin waits for the specified JavaScript variable
* to exist on the page before a beacon is sent.
*
* It does not affect the Page Load time (`t_done`) -- it just delays a beacon
* until the variable exists. This allows the beacon to contain additional
* ResourceTiming data and other metrics/timers that might happen after page load.
*
* NOTE: Any plugin like this that delays a beacon from being sent after onload
* will have an effect on total number of beacons captured (due to the visitor
* having more time to hard-close the browser, etc, before the beacon is sent).
*/
(function(w) {
//
// TODO: Configure the JavaScript variable you want to monitor below
//
var POLL_FREQUENCY = 100;
if (!w) {
return;
}
BOOMR = w.BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
BOOMR.plugins.WaitForJsVar = {
varFound: false,
init: function() {
function poll() {
//
// TODO: Var check goes here
//
if (typeof w.TODO_JS_VARIABLE_TO_CHECK === "undefined") {
return setTimeout(poll, POLL_FREQUENCY);
}
this.varFound = true;
if (typeof BOOMR.sendBeacon !== "function") {
return setTimeout(poll, POLL_FREQUENCY);
}
BOOMR.sendBeacon();
}
poll();
},
is_complete: function() {
return this.varFound;
}
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment