Skip to content

Instantly share code, notes, and snippets.

@nicjansma
Last active April 20, 2022 13:21
Show Gist options
  • Save nicjansma/2098fbd58a9be9a46921b349c567e9a5 to your computer and use it in GitHub Desktop.
Save nicjansma/2098fbd58a9be9a46921b349c567e9a5 to your computer and use it in GitHub Desktop.
Boomerang plugin that waits for the specified UserTiming Mark (or Measure) to exist on the page before a beacon is sent.
/**
* The `WaitForMark` Boomerang plugin waits for the specified UserTiming Mark
* (or Measure) 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 Mark 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 mark you want to wait for:
//
var MARK_NAME = "";
var POLL_FREQUENCY = 100;
if (!w || !("performance" in w) || !w.performance || typeof w.performance.getEntriesByName !== "function") {
return;
}
BOOMR = w.BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
BOOMR.plugins.WaitForMark = {
markFound: false,
init: function() {
function poll() {
var entries = w.performance.getEntriesByName(MARK_NAME);
if (entries.length == 0) {
return setTimeout(poll, POLL_FREQUENCY);
}
this.markFound = true;
if (typeof BOOMR.sendBeacon !== "function") {
return setTimeout(poll, POLL_FREQUENCY);
}
BOOMR.sendBeacon();
}
poll();
},
is_complete: function() {
return this.markFound;
}
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment