Skip to content

Commit 080a011

Browse files
committed
Move sa_metadata fetch to appendMetadata
1 parent 4e0ebca commit 080a011

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

src/default.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
version,
99
defaultNamespace,
1010
sendError,
11-
warn
11+
warn,
1212
) {
1313
try {
1414
/////////////////////
@@ -117,8 +117,8 @@
117117
return Array.isArray(csv)
118118
? csv
119119
: isString(csv) && csv.length
120-
? csv.split(/, ?/)
121-
: [];
120+
? csv.split(/, ?/)
121+
: [];
122122
};
123123

124124
var isObject = function (object) {
@@ -152,7 +152,7 @@
152152
/** if ignoremetrics **/
153153
// Customers can skip data points
154154
var ignoreMetrics = convertCommaSeparatedToArray(
155-
overwriteOptions.ignoreMetrics || attr(scriptElement, "ignore-metrics")
155+
overwriteOptions.ignoreMetrics || attr(scriptElement, "ignore-metrics"),
156156
);
157157
/** endif **/
158158

@@ -205,15 +205,15 @@
205205
defaultNamespace;
206206

207207
/** if metadata **/
208-
var metadataObject = window[namespace + "_metadata"];
209208
var appendMetadata = function (metadata, data) {
209+
var metadataObject = window[namespace + "_metadata"];
210210
if (isObject(metadataObject)) metadata = assign(metadata, metadataObject);
211211
var metadataCollectorFunction = window[metadataCollector];
212212
if (!isFunction(metadataCollectorFunction)) return metadata;
213213
try {
214214
return assign(
215215
metadata,
216-
metadataCollectorFunction.call(window, assign(metadata, data))
216+
metadataCollectorFunction.call(window, assign(metadata, data)),
217217
);
218218
} catch (error) {
219219
warnInFunction("metadata", error);
@@ -284,7 +284,7 @@
284284
ignorePage === path ||
285285
new RegExp(
286286
"^" + filterRegex(ignorePage).replace(/\\\*/gi, "(.*)") + "$",
287-
"i"
287+
"i",
288288
).test(path)
289289
)
290290
return trueVar;
@@ -374,7 +374,7 @@
374374
path: loc.pathname,
375375
}),
376376
undefinedVar,
377-
trueVar
377+
trueVar,
378378
);
379379
};
380380

@@ -387,7 +387,7 @@
387387
sendError(event.message);
388388
}
389389
},
390-
falseVar
390+
falseVar,
391391
);
392392
/** endif **/
393393

@@ -438,22 +438,22 @@
438438
/** if ignorepages **/
439439
// Customers can ignore certain pages
440440
var ignorePages = convertCommaSeparatedToArray(
441-
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
441+
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages"),
442442
);
443443
/** endif **/
444444

445445
/** if allowparams **/
446446
// Customers can allow params
447447
var allowParams = convertCommaSeparatedToArray(
448-
overwriteOptions.allowParams || attr(scriptElement, "allow-params")
448+
overwriteOptions.allowParams || attr(scriptElement, "allow-params"),
449449
);
450450
/** endif **/
451451

452452
/** if nonuniquehostnames **/
453453
// Customers can allow params
454454
var nonUniqueHostnames = convertCommaSeparatedToArray(
455455
overwriteOptions.nonUniqueHostnames ||
456-
attr(scriptElement, "non-unique-hostnames")
456+
attr(scriptElement, "non-unique-hostnames"),
457457
);
458458
/** endif **/
459459

@@ -553,12 +553,12 @@
553553
/** if ignorednt **/
554554
if (!collectDnt && doNotTrack in nav && nav[doNotTrack] == "1")
555555
return warn(
556-
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt"
556+
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt",
557557
);
558558
/** else **/
559559
if (doNotTrack in nav && nav[doNotTrack] == "1")
560560
return warn(
561-
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt"
561+
notSendingWhen + doNotTrack + " is enabled. See " + docsUrl + "/dnt",
562562
);
563563
/** endif **/
564564

@@ -573,7 +573,7 @@
573573
locationHostname +
574574
". See " +
575575
docsUrl +
576-
"/overwrite-domain-name"
576+
"/overwrite-domain-name",
577577
);
578578

579579
/////////////////////
@@ -644,7 +644,7 @@
644644
hiddenStart = now();
645645
} else msHidden += now() - hiddenStart;
646646
},
647-
falseVar
647+
falseVar,
648648
);
649649
/** endif **/
650650

@@ -660,15 +660,15 @@
660660
body[offsetHeight] || 0,
661661
documentElement[clientHeight] || 0,
662662
documentElement[scrollHeight] || 0,
663-
documentElement[offsetHeight] || 0
663+
documentElement[offsetHeight] || 0,
664664
);
665665
return Math.min(
666666
100,
667667
Math.round(
668668
(100 * ((documentElement.scrollTop || 0) + documentClientHeight)) /
669669
height /
670-
5
671-
) * 5
670+
5,
671+
) * 5,
672672
);
673673
} catch (error) {
674674
warn(error);
@@ -683,7 +683,7 @@
683683
function () {
684684
if (scrolled < position()) scrolled = position();
685685
},
686-
falseVar
686+
falseVar,
687687
);
688688
});
689689
/** endif **/
@@ -737,7 +737,7 @@
737737
isPushState,
738738
deleteSourceInfo,
739739
sameSite,
740-
metadata
740+
metadata,
741741
) {
742742
if (isPushState) sendOnLeave("" + payload.page_id, trueVar);
743743
if (collectDataOnLeave) payload.page_id = uuid();
@@ -782,7 +782,7 @@
782782
page.viewport_height =
783783
Math.max(
784784
documentElement[clientHeight] || 0,
785-
window.innerHeight || 0
785+
window.innerHeight || 0,
786786
) || null;
787787
}
788788
/** endif **/
@@ -853,7 +853,7 @@
853853
isPushState,
854854
isPushState || userNavigated || !collectMetricByString("r"), // r = referrers
855855
sameSite,
856-
metadata
856+
metadata,
857857
);
858858
};
859859

@@ -920,15 +920,15 @@
920920
function () {
921921
pageview(1);
922922
},
923-
falseVar
923+
falseVar,
924924
);
925925

926926
addEventListenerFunc(
927927
"popstate",
928928
function () {
929929
pageview(1);
930930
},
931-
falseVar
931+
falseVar,
932932
);
933933
}
934934
/** endif **/
@@ -941,7 +941,7 @@
941941
function () {
942942
pageview(1);
943943
},
944-
falseVar
944+
falseVar,
945945
);
946946
}
947947
/** endif **/
@@ -988,7 +988,7 @@
988988
if (validTypes.indexOf(typeof eventOutput) < 0) {
989989
warnInFunction(
990990
eventFunctionName,
991-
event + " returns no string: " + eventOutput
991+
event + " returns no string: " + eventOutput,
992992
);
993993
return callback();
994994
}
@@ -1022,7 +1022,7 @@
10221022
metadata: stringify(metadata),
10231023
/** endif **/
10241024
}),
1025-
callback
1025+
callback,
10261026
);
10271027
}
10281028
};
@@ -1065,5 +1065,5 @@
10651065
"{{baseUrl}}",
10661066
"{{apiUrlPrefix}}",
10671067
"{{scriptName}}",
1068-
"{{namespace}}"
1068+
"{{namespace}}",
10691069
);

test/test-append-metadata.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { expect } = require("chai");
2+
3+
describe("appendMetadata", function () {
4+
it("reloads sa_metadata each call", function () {
5+
const namespace = "sa";
6+
const window = {};
7+
8+
const isObject = (obj) => obj && obj.constructor === Object;
9+
const assign = (target, source) => Object.assign(target, source);
10+
11+
const appendMetadata = function (metadata, data) {
12+
const metadataObject = window[namespace + "_metadata"];
13+
if (isObject(metadataObject)) metadata = assign(metadata, metadataObject);
14+
return metadata;
15+
};
16+
17+
window[namespace + "_metadata"] = { a: 1 };
18+
let metadata = appendMetadata({}, {});
19+
expect(metadata).to.deep.equal({ a: 1 });
20+
21+
window[namespace + "_metadata"] = { b: 2 };
22+
metadata = appendMetadata({}, {});
23+
expect(metadata).to.deep.equal({ b: 2 });
24+
});
25+
});

0 commit comments

Comments
 (0)