Skip to content

Commit 3ac0008

Browse files
Merge branch 'v12' into codex/add-callback-to-sa_pageview
2 parents 7dc389f + cfdd6f6 commit 3ac0008

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed

dist/latest/latest.dev.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@
191191
attr(scriptElement, namespaceText) ||
192192
defaultNamespace;
193193

194-
var metadataObject = window[namespace + "_metadata"];
195194
var appendMetadata = function (metadata, data) {
195+
var metadataObject = window[namespace + "_metadata"];
196196
if (isObject(metadataObject)) metadata = assign(metadata, metadataObject);
197197
var metadataCollectorFunction = window[metadataCollector];
198198
if (!isFunction(metadataCollectorFunction)) return metadata;
@@ -559,7 +559,13 @@
559559
// sendData will assign payload to request
560560
sendData(append, undefinedVar, trueVar);
561561
} else {
562-
nav.sendBeacon(fullApiUrl + "/append", stringify(append));
562+
try {
563+
nav.sendBeacon
564+
.bind(nav)(fullApiUrl + "/append", stringify(append));
565+
} catch (e) {
566+
// Fallback for browsers throwing "Illegal invocation" when the URL is invalid
567+
sendData(append, undefinedVar, trueVar);
568+
}
563569
}
564570
};
565571

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/default.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@
206206
defaultNamespace;
207207

208208
/** if metadata **/
209-
var metadataObject = window[namespace + "_metadata"];
210209
var appendMetadata = function (metadata, data) {
210+
var metadataObject = window[namespace + "_metadata"];
211211
if (isObject(metadataObject)) metadata = assign(metadata, metadataObject);
212212
var metadataCollectorFunction = window[metadataCollector];
213213
if (!isFunction(metadataCollectorFunction)) return metadata;
@@ -638,7 +638,12 @@
638638
// sendData will assign payload to request
639639
sendData(append, undefinedVar, trueVar);
640640
} else {
641-
nav.sendBeacon(fullApiUrl + "/append", stringify(append));
641+
try {
642+
nav.sendBeacon.bind(nav)(fullApiUrl + "/append", stringify(append));
643+
} catch (e) {
644+
// Fallback for browsers throwing "Illegal invocation" when the URL is invalid
645+
sendData(append, undefinedVar, trueVar);
646+
}
642647
}
643648
};
644649

test/unit/metadata.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,47 @@ describe("metadata", function () {
3939
done();
4040
}, 10);
4141
});
42+
43+
it("reloads global metadata on each call", function (done) {
44+
const dom = createDOM({ settings: { autoCollect: false } });
45+
const { runInContext } = require("vm");
46+
47+
runInContext(
48+
"window.sa_metadata = { first: true };",
49+
dom.getInternalVMContext()
50+
);
51+
52+
dom.window.sa_pageview("/first");
53+
54+
setTimeout(() => {
55+
let req = dom.sent.find(
56+
(r) => r.type === "image" && /path=%2Ffirst/.test(r.url)
57+
);
58+
expect(req, "first pageview request").to.exist;
59+
let url = new URL(req.url);
60+
let meta = JSON.parse(
61+
decodeURIComponent(url.searchParams.get("metadata"))
62+
);
63+
expect(meta).to.include({ first: true });
64+
65+
runInContext(
66+
"window.sa_metadata = { second: true };",
67+
dom.getInternalVMContext()
68+
);
69+
70+
dom.window.sa_pageview("/second");
71+
72+
setTimeout(() => {
73+
req = dom.sent.find(
74+
(r) => r.type === "image" && /path=%2Fsecond/.test(r.url)
75+
);
76+
expect(req, "second pageview request").to.exist;
77+
url = new URL(req.url);
78+
meta = JSON.parse(decodeURIComponent(url.searchParams.get("metadata")));
79+
expect(meta).to.include({ second: true });
80+
expect(meta).to.not.have.property("first");
81+
done();
82+
}, 10);
83+
}, 10);
84+
});
4285
});

test/unit/pageview.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,34 @@ describe("pageview", function () {
3434
done();
3535
}, 10);
3636
});
37+
38+
it("falls back to pixel when sendBeacon fails", function (done) {
39+
const dom = createDOM({ navigationType: "reload" });
40+
41+
dom.window.navigator.sendBeacon = function () {
42+
throw new TypeError("Illegal invocation");
43+
};
44+
45+
dom.window.sa_event("unit_test");
46+
47+
if (!("onpagehide" in dom.window)) {
48+
dom.window.document.hidden = true;
49+
dom.window.document.dispatchEvent(
50+
new dom.window.Event("visibilitychange")
51+
);
52+
} else {
53+
dom.window.dispatchEvent(new dom.window.Event("pagehide"));
54+
}
55+
56+
setTimeout(() => {
57+
const beacon = dom.sent.find((r) => r.type === "beacon");
58+
const appendGif = dom.sent.find(
59+
(r) => r.type === "image" && /type=append/.test(r.url)
60+
);
61+
62+
expect(beacon, "append beacon request").to.not.exist;
63+
expect(appendGif, "append gif request").to.exist;
64+
done();
65+
}, 10);
66+
});
3767
});

0 commit comments

Comments
 (0)