Skip to content

Commit

Permalink
Capture BOOMR.page_ready() time after onload; BOOMR.addVar() for sing…
Browse files Browse the repository at this point in the history
…le beacons
  • Loading branch information
nicjansma committed Apr 4, 2018
1 parent 2a8b36d commit 9ef6d13
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 11 deletions.
59 changes: 48 additions & 11 deletions boomerang.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ BOOMR_check_doc_domain();
listenerCallbacks: {},

vars: {},
singleBeaconVars: {},

/**
* Variable priority lists:
Expand Down Expand Up @@ -1203,18 +1204,36 @@ BOOMR_check_doc_domain();
*/
page_ready_autorun: function(ev) {
if (impl.autorun) {
BOOMR.page_ready(ev);
BOOMR.page_ready(ev, true);
}
},

// The page dev calls this method when they determine the page is usable.
// Only call this if autorun is explicitly set to false
page_ready: function(ev) {
if (!ev) { ev = w.event; }
if (!ev) { ev = { name: "load" }; }
page_ready: function(ev, auto) {
if (!ev) {
ev = w.event;
}

if (!ev) {
ev = {
name: "load"
};
}

// if we were called manually, add the current timestamp and note
// this was 'pr' on the beacon
if (!auto) {
ev.timing = ev.timing || {};
ev.timing.loadEventEnd = BOOMR.now();

BOOMR.addVar("pr", 1, true);
}

if (impl.onloadfired) {
return this;
}

impl.fireEvent("page_ready", ev);
impl.onloadfired = true;
return this;
Expand Down Expand Up @@ -1483,7 +1502,7 @@ BOOMR_check_doc_domain();
(err.name === "Error" && err.message && err.message.match(/^(Permission|Access is) denied/));
},

addVar: function(name, value) {
addVar: function(name, value, singleBeacon) {
if (typeof name === "string") {
impl.vars[name] = value;
}
Expand All @@ -1495,6 +1514,11 @@ BOOMR_check_doc_domain();
}
}
}

if (singleBeacon) {
impl.singleBeaconVars[name] = 1;
}

return this;
},

Expand Down Expand Up @@ -1819,6 +1843,16 @@ BOOMR_check_doc_domain();

BOOMR.removeVar("qt");

// remove any vars that should only be on a single beacon
for (var singleVarName in impl.singleBeaconVars) {
if (impl.singleBeaconVars.hasOwnProperty(singleVarName)) {
BOOMR.removeVar(singleVarName);
}
}

// clear single beacon vars list
impl.singleBeaconVars = {};

// keep track of page load beacons
if (!impl.hasSentPageLoadBeacon && isPageLoad) {
impl.hasSentPageLoadBeacon = true;
Expand Down Expand Up @@ -2031,13 +2065,16 @@ BOOMR_check_doc_domain();
* @returns {string} URI-encoded string
*/
getUriEncodedVar: function(name, value) {
if (value === undefined || value === null) {
value = "";
}

if (typeof value === "object") {
value = BOOMR.utils.serializeForUrl(value);
}

var result = encodeURIComponent(name) +
"=" +
(
value === undefined || value === null ?
"" :
encodeURIComponent(value)
);
"=" + encodeURIComponent(value);

return result;
},
Expand Down
54 changes: 54 additions & 0 deletions tests/page-templates/00-basic/12-addvar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<%= header %>
<%= boomerangScript %>
<script src="12-addvar.js" type="text/javascript"></script>
<script>

// disable sendBeacon so we can look at the URL's parameters
if (window.navigator) {
window.navigator.sendBeacon = null;
}

BOOMR_test.init({
testAfterOnBeacon: 3,
afterFirstBeacon: function() {
// fire two beacons
BOOMR.responseEnd("foo");

setTimeout(function() {
BOOMR.responseEnd("foo2");
}, 1000);
}
});

// test different variables types
BOOMR.addVar("var1", 1);
BOOMR.addVar("var2", "abc");
BOOMR.addVar("var3", 0);
BOOMR.addVar("var4");
BOOMR.addVar("var5", undefined);
BOOMR.addVar("var6", null);
BOOMR.addVar("var7", "");
BOOMR.addVar("var8", { a: 1 });
BOOMR.addVar("var9", 1.1111111);

// overwrite a var
BOOMR.addVar("var10", 1);
BOOMR.addVar("var10", 2);

// set single-beacon vars
BOOMR.addVar("var11", "single", true);

// remove a var
BOOMR.addVar("var12", 2);
BOOMR.removeVar("var12");

// multiple sets
BOOMR.addVar({
var13: 1,
var14: 2
});

// remove a non-existent var
BOOMR.removeVar("var15");
</script>
<%= footer %>
Loading

0 comments on commit 9ef6d13

Please sign in to comment.