Skip to content

Commit aee4926

Browse files
adriaandotcomAdriaan
authored andcommitted
Make sure to allow arrays and strings for events and fix double events
1 parent 82d4290 commit aee4926

31 files changed

+82
-58
lines changed

dist/latest/cloudflare.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-04-13; bb7c) */
1+
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-04-20; 9833) */
22
/* eslint-env browser */
33

44
(function (window, overwriteOptions, baseUrl, apiUrlPrefix, version, saGlobal) {
@@ -19,10 +19,10 @@
1919
var https = "https:";
2020
var pageviewsText = "pageview";
2121
var errorText = "error";
22+
var slash = "/";
2223
var protocol = https + "//";
2324
var con = window.console;
2425
var doNotTrack = "doNotTrack";
25-
var slash = "/";
2626
var nav = window.navigator;
2727
var loc = window.location;
2828
var locationHostname = loc.hostname;
@@ -118,6 +118,10 @@
118118
return typeof func == "function";
119119
};
120120

121+
var isString = function (string) {
122+
return typeof string == "string";
123+
};
124+
121125
var assign = function () {
122126
var to = {};
123127
var arg = arguments;
@@ -159,7 +163,7 @@
159163

160164
// Prepend a slash when it's missing
161165
var ignorePage =
162-
ignorePageRaw[0] == "/" ? ignorePageRaw : "/" + ignorePageRaw;
166+
ignorePageRaw[0] == slash ? ignorePageRaw : slash + ignorePageRaw;
163167

164168
try {
165169
if (
@@ -181,6 +185,7 @@
181185
// Send data via image
182186
var sendData = function (data, callback) {
183187
data = assign(payload, data);
188+
184189
var image = new Image();
185190
if (callback) {
186191
image.onerror = callback;
@@ -200,7 +205,9 @@
200205
encodeURIComponentFunc(data[key])
201206
);
202207
})
203-
.join("&");
208+
.join("&") +
209+
"&time=" +
210+
Date.now();
204211
};
205212

206213
/////////////////////
@@ -292,7 +299,7 @@
292299
// Make sure ignore pages is an array
293300
var ignorePages = Array.isArray(ignorePagesRaw)
294301
? ignorePagesRaw
295-
: typeof ignorePagesRaw == "string" && ignorePagesRaw.length
302+
: isString(ignorePagesRaw) && ignorePagesRaw.length
296303
? ignorePagesRaw.split(/, ?/)
297304
: [];
298305

@@ -531,7 +538,7 @@
531538

532539
// Check if referrer is the same as current real hostname (not the defined hostname!)
533540
var sameSite = referrer
534-
? doc.referrer.split("/")[2] == locationHostname
541+
? doc.referrer.split(slash)[2] == locationHostname
535542
: false;
536543

537544
// We set unique variable based on pushstate or back navigation, if no match we check the referrer
@@ -663,7 +670,7 @@
663670

664671
event = ("" + event).replace(/[^a-z0-9]+/gi, "_").replace(/(^_|_$)/g, "");
665672

666-
if (event)
673+
if (event) {
667674
sendData(
668675
assign(source, bot ? { bot: true } : {}, {
669676
type: "event",
@@ -673,6 +680,7 @@
673680
}),
674681
callback
675682
);
683+
}
676684
};
677685

678686
var defaultEventFunc = function (event, callback) {
@@ -691,7 +699,11 @@
691699
window[functionName] = defaultEventFunc;
692700

693701
// Post events from the queue of the user defined function
694-
for (var event in queue) sendEvent(queue[event]);
702+
for (var event in queue) {
703+
Array.isArray(queue[event])
704+
? sendEvent.apply(null, queue[event])
705+
: sendEvent(queue[event]);
706+
}
695707
} catch (e) {
696708
sendError(e);
697709
}

dist/latest/custom/app.js

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

dist/latest/custom/app.js.map

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

0 commit comments

Comments
 (0)