Skip to content

Commit c256994

Browse files
Merge pull request #70 from simpleanalytics/codex/fix-sa_metadata-loading-in-appendmetadata
Fix metadata reload on each call
2 parents e38d8f6 + 21ae5b7 commit c256994

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

dist/latest/latest.dev.js

Lines changed: 2 additions & 2 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-29; 36a5; v12) */
1+
/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; 2025-05-30; 1686; v12) */
22
/* eslint-env browser */
33

44
(function (
@@ -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;

src/default.js

Lines changed: 1 addition & 1 deletion
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;

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
});

0 commit comments

Comments
 (0)