Skip to content

Commit f575d2f

Browse files
Merge pull request #30 from simpleanalytics/v9-fixes
V9 fixes
2 parents 7ef3bf1 + 8655aa6 commit f575d2f

35 files changed

+349
-247
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/cloudflare.js

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2022-07-25; 72db; v9) */
1+
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2022-07-26; 2f79; v9) */
22
/* eslint-env browser */
33

44
(function (
@@ -8,7 +8,8 @@
88
apiUrlPrefix,
99
version,
1010
defaultNamespace,
11-
sendError
11+
sendError,
12+
warn
1213
) {
1314
try {
1415
/////////////////////
@@ -71,8 +72,21 @@
7172
//
7273

7374
// A simple log function so the user knows why a request is not being send
74-
var warn = function (message) {
75-
if (con && con.warn) con.warn("Simple Analytics:", message);
75+
warn = function () {
76+
// 1. Convert args to a normal array
77+
var args = [].slice.call(arguments);
78+
79+
// 2. Prepend log prefix
80+
args.unshift("Simple Analytics: ");
81+
82+
// 3. Pass along arguments to console.warn
83+
// Function.prototype.bind.call is needed for Internet Explorer
84+
var log = Function.prototype.bind.call(con.warn, con);
85+
log.apply(con, args);
86+
};
87+
88+
var warnInFunction = function (name, error) {
89+
warn("Error in your " + name + " function:", error);
7690
};
7791

7892
var hasProp = function (obj, prop) {
@@ -176,10 +190,10 @@
176190
try {
177191
return assign(
178192
metadata,
179-
metadataCollectorFunction.call(window, assign(data, metadata))
193+
metadataCollectorFunction.call(window, assign(metadata, data))
180194
);
181195
} catch (error) {
182-
warn(errorText + " in your metadata function: " + error);
196+
warnInFunction("metadata", error);
183197
}
184198
};
185199

@@ -231,18 +245,14 @@
231245
var ignorePage =
232246
ignorePageRaw[0] == slash ? ignorePageRaw : slash + ignorePageRaw;
233247

234-
try {
235-
if (
236-
ignorePage === path ||
237-
new RegExp(
238-
"^" + filterRegex(ignorePage).replace(/\\\*/gi, "(.*)") + "$",
239-
"i"
240-
).test(path)
241-
)
242-
return trueVar;
243-
} catch (error) {
244-
return falseVar;
245-
}
248+
if (
249+
ignorePage === path ||
250+
new RegExp(
251+
"^" + filterRegex(ignorePage).replace(/\\\*/gi, "(.*)") + "$",
252+
"i"
253+
).test(path)
254+
)
255+
return trueVar;
246256
}
247257
return falseVar;
248258
};
@@ -293,20 +303,36 @@
293303
Date.now();
294304
};
295305

306+
// Customers can overwrite their hostname, here we check for that
307+
var overwrittenHostname =
308+
overwriteOptions.hostname || attr(scriptElement, "hostname");
309+
var definedHostname = overwrittenHostname || locationHostname;
310+
311+
var basePayload = {
312+
version: version,
313+
hostname: definedHostname,
314+
};
315+
296316
/////////////////////
297317
// ERROR FUNCTIONS
298318
//
299319

300320
// Send errors
301321
// no var because it's scoped outside of the try/catch
302322
sendError = function (errorOrMessage) {
303-
errorOrMessage = errorOrMessage.message || errorOrMessage;
323+
errorOrMessage = errorOrMessage.stack
324+
? errorOrMessage + " " + errorOrMessage.stack
325+
: errorOrMessage;
304326
warn(errorOrMessage);
305-
sendData({
306-
type: errorText,
307-
error: errorOrMessage,
308-
url: definedHostname + loc.pathname,
309-
});
327+
sendData(
328+
assign(basePayload, {
329+
type: errorText,
330+
error: errorOrMessage,
331+
path: loc.pathname,
332+
}),
333+
undefinedVar,
334+
trueVar
335+
);
310336
};
311337

312338
// We listen for the error events and only send errors that are
@@ -343,11 +369,6 @@
343369
attr(scriptElement, "skip-dnt") == trueText ||
344370
attr(scriptElement, "collect-dnt") == trueText;
345371

346-
// Customers can overwrite their hostname, here we check for that
347-
var overwrittenHostname =
348-
overwriteOptions.hostname || attr(scriptElement, "hostname");
349-
var definedHostname = overwrittenHostname || locationHostname;
350-
351372
// Some customers want to collect page views manually
352373
var autoCollect = !(
353374
attr(scriptElement, "auto-collect") == "false" ||
@@ -361,11 +382,9 @@
361382
namespace + "_" + eventText;
362383

363384
// Customers can ignore certain pages
364-
var ignorePages =
365-
["/path*lala"] ||
366-
convertCommaSeparatedToArray(
367-
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
368-
);
385+
var ignorePages = convertCommaSeparatedToArray(
386+
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
387+
);
369388

370389
// Customers can allow params
371390
var allowParams = convertCommaSeparatedToArray(
@@ -394,8 +413,8 @@
394413
timezone = collectMetricByString("c")
395414
? Intl.DateTimeFormat().resolvedOptions().timeZone
396415
: undefinedVar;
397-
} catch (e) {
398-
/* Do nothing */
416+
} catch (error) {
417+
warn(error);
399418
}
400419

401420
/////////////////////
@@ -416,11 +435,6 @@
416435
var collectDataOnLeave =
417436
collectMetricByString("t") || collectMetricByString("scro");
418437

419-
var basePayload = {
420-
version: version,
421-
hostname: definedHostname,
422-
};
423-
424438
if (bot) basePayload.bot = trueVar;
425439

426440
var payload = assign(basePayload, {
@@ -560,6 +574,7 @@
560574
) * 5
561575
);
562576
} catch (error) {
577+
warn(error);
563578
return 0;
564579
}
565580
};
@@ -586,13 +601,17 @@
586601
// https://github.com/simpleanalytics/roadmap/issues/462
587602
try {
588603
path = overwrite || decodeURIComponentFunc(loc.pathname);
589-
} catch (e) {
590-
// Do nothing
604+
} catch (error) {
605+
warn(error);
591606
}
592607

593608
var pathOverwriterFunction = window[pathOverwriter];
594609
if (isFunction(pathOverwriterFunction)) {
595-
path = pathOverwriterFunction.call(window, path);
610+
try {
611+
path = pathOverwriterFunction.call(window, { path: path }) || path;
612+
} catch (error) {
613+
warnInFunction("path", error);
614+
}
596615
}
597616

598617
// Ignore pages specified in data-ignore-pages
@@ -673,7 +692,7 @@
673692
try {
674693
performaceEntryType = perf.getEntriesByType(navigationText)[0].type;
675694
} catch (error) {
676-
// Do nothing
695+
warn(error);
677696
}
678697

679698
var userNavigated = performaceEntryType
@@ -812,22 +831,27 @@
812831

813832
var eventIsFunction = isFunction(event);
814833
var callback = isFunction(callbackRaw) ? callbackRaw : function () {};
834+
var eventType = typeof event;
815835

816-
if (validTypes.indexOf(typeof event) < 0 && !eventIsFunction) {
817-
warn(eventText + " isn't a string: " + event);
836+
if (validTypes.indexOf(eventType) < 0 && !eventIsFunction) {
837+
warnInFunction(eventFunctionName, eventText + " can't be " + eventType);
818838
return callback();
819839
}
820840

821841
try {
822842
if (eventIsFunction) {
823-
event = event();
824-
if (validTypes.indexOf(typeof event) < 0) {
825-
warn(eventText + " function output isn't a string: " + event);
843+
var eventOutput = event();
844+
if (validTypes.indexOf(typeof eventOutput) < 0) {
845+
warnInFunction(
846+
eventFunctionName,
847+
event + " returns no string: " + eventOutput
848+
);
826849
return callback();
827850
}
851+
event = eventOutput;
828852
}
829853
} catch (error) {
830-
warn(errorText + " in your event function: " + error);
854+
warnInFunction(eventFunctionName, error);
831855
return callback();
832856
}
833857

0 commit comments

Comments
 (0)