Skip to content

Commit 8ed3786

Browse files
committed
test: add metadata collector attr test
1 parent cfdd6f6 commit 8ed3786

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

dist/latest/auto-events.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/latest/latest.dev.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; 2025-05-30; bf05; v12) */
1+
/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; 2025-06-04; 2fe0; v12) */
22
/* eslint-env browser */
33

44
(function (
@@ -560,8 +560,7 @@
560560
sendData(append, undefinedVar, trueVar);
561561
} else {
562562
try {
563-
nav.sendBeacon
564-
.bind(nav)(fullApiUrl + "/append", stringify(append));
563+
nav.sendBeacon.bind(nav)(fullApiUrl + "/append", stringify(append));
565564
} catch (e) {
566565
// Fallback for browsers throwing "Illegal invocation" when the URL is invalid
567566
sendData(append, undefinedVar, trueVar);

test/unit/metadata-attr.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { expect } = require("chai");
2+
const { createDOM } = require("./helpers/dom");
3+
4+
describe("metadata via data attribute", function () {
5+
it("uses data-metadata-collector attribute", function (done) {
6+
const dom = createDOM({
7+
settings: { autoCollect: false },
8+
beforeRun(vmContext) {
9+
const { runInContext } = require("vm");
10+
runInContext(
11+
"window.collector = function(data){ return { fromAttr: true, path: data.path }; };" +
12+
"Object.defineProperty(document, 'currentScript', {get: function(){ return { getAttribute: function(name){ return name === 'data-metadata-collector' ? 'collector' : null; } }; }});",
13+
vmContext
14+
);
15+
},
16+
});
17+
18+
dom.window.sa_pageview("/attr");
19+
20+
setTimeout(() => {
21+
const req = dom.sent.find(
22+
(r) => r.type === "image" && /path=%2Fattr/.test(r.url)
23+
);
24+
expect(req, "pageview request").to.exist;
25+
const url = new URL(req.url);
26+
const meta = JSON.parse(
27+
decodeURIComponent(url.searchParams.get("metadata"))
28+
);
29+
expect(meta).to.include({ fromAttr: true });
30+
done();
31+
}, 10);
32+
});
33+
});

0 commit comments

Comments
 (0)