Skip to content

Commit 18c6bba

Browse files
committed
Throw error when minify detects ES6 and give hello.js a version
1 parent 3d76399 commit 18c6bba

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

dist/latest/cloudflare.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-12-30; 26ba; v8) */
1+
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-12-30; 6aec; v8) */
22
/* eslint-env browser */
33

44
(function (window, overwriteOptions, baseUrl, apiUrlPrefix, version, saGlobal) {
@@ -52,6 +52,7 @@
5252
var platformText = "platform";
5353
var platformVersionText = "platformVersion";
5454
var docsUrl = "https://docs.simpleanalytics.com";
55+
var allowParams;
5556
var isBotAgent =
5657
/(bot|spider|crawl)/i.test(userAgent) && !/(cubot)/i.test(userAgent);
5758
var screen = window.screen;
@@ -117,17 +118,18 @@
117118
return !!value === value;
118119
};
119120

120-
var getParams = function (regex) {
121+
var getParams = function (regex, returnArray) {
121122
// From the search we grab the utm_source and ref and save only that
122123
var matches = loc.search.match(
123124
new RegExp("[?&](" + regex + ")=([^?&]+)", "gi")
124125
);
125126
var match = matches
126127
? matches.map(function (m) {
127-
return m.split("=")[1];
128+
return m.split(/[?&=]/).slice(-2);
128129
})
129130
: [];
130-
if (match && match[0]) return match[0];
131+
132+
if (match[0]) return returnArray ? match[0] : match[0][1];
131133
};
132134

133135
// Ignore pages specified in data-ignore-pages
@@ -161,6 +163,17 @@
161163
var sendData = function (data, callback) {
162164
data = assign(payload, page, data);
163165

166+
if (allowParams)
167+
data.params = stringify(
168+
allowParams
169+
.map(function (param) {
170+
var params = getParams(param, true);
171+
if (!params) return;
172+
return { key: params[0], value: params[1] };
173+
})
174+
.filter(Boolean)
175+
);
176+
164177
var image = new Image();
165178
if (callback) {
166179
image.onerror = callback;
@@ -264,24 +277,32 @@
264277
overwriteOptions.autoCollect === false
265278
);
266279

280+
var convertCommaSeparatedToArray = function (csv) {
281+
return Array.isArray(csv)
282+
? csv
283+
: isString(csv) && csv.length
284+
? csv.split(/, ?/)
285+
: [];
286+
};
287+
267288
// Event function name
268289
var functionName =
269290
overwriteOptions.saGlobal || attr(scriptElement, "sa-global") || saGlobal;
270291

271292
// Customers can ignore certain pages
272-
var ignorePagesRaw =
273-
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages");
293+
var ignorePages = convertCommaSeparatedToArray(
294+
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages")
295+
);
274296

275-
// Make sure ignore pages is an array
276-
var ignorePages = Array.isArray(ignorePagesRaw)
277-
? ignorePagesRaw
278-
: isString(ignorePagesRaw) && ignorePagesRaw.length
279-
? ignorePagesRaw.split(/, ?/)
280-
: [];
297+
// Customers can allow params
298+
allowParams = convertCommaSeparatedToArray(
299+
overwriteOptions.allowParams || attr(scriptElement, "allow-params")
300+
);
281301

282-
// Customers can ignore certain pages
283-
var ignorePagesRaw =
284-
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages");
302+
// By default we allow source, medium in the URLs. With strictUtm enabled
303+
// we only allow it with the utm_ prefix: utm_source, utm_medium, ...
304+
var strictUtm =
305+
overwriteOptions.strictUtm || attr(scriptElement, "strict-utm");
285306

286307
/////////////////////
287308
// PAYLOAD FOR BOTH PAGE VIEWS AND EVENTS
@@ -366,10 +387,10 @@
366387
.replace(/^https?:\/\/((m|l|w{2,3}([0-9]+)?)\.)?([^?#]+)(.*)$/, "$4")
367388
.replace(/^([^/]+)$/, "$1") || undefinedVar;
368389

369-
// The prefix utm_ is optional
370-
var utmRegexPrefix = "(utm_)?";
390+
// The prefix utm_ is optional with strictUtm disabled
391+
var utmRegexPrefix = "(utm_)" + (strictUtm ? "" : "?");
371392
var source = {
372-
source: getParams(utmRegexPrefix + "source|ref"),
393+
source: getParams(utmRegexPrefix + "source" + (strictUtm ? "" : "|ref")),
373394
medium: getParams(utmRegexPrefix + "medium"),
374395
campaign: getParams(utmRegexPrefix + "campaign"),
375396
term: getParams(utmRegexPrefix + "term"),

minify.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const MINIFY_OPTIONS = {
2525
};
2626

2727
const fillTemplate = (template, { overwriteOptions = null } = {}) => {
28+
if (!template)
29+
throw new Error("Parsing of the JavaScript failed, please use ES5 only.");
2830
return template
2931
.replace(
3032
/\{\{\s?nginxHost\s?\}\}/gi,
@@ -69,6 +71,7 @@ const DEFAULTS = {
6971
saGlobal: "sa_event",
7072
url: "docs.simpleanalytics.com/script",
7173
scriptName: "script",
74+
allowparams: true,
7275
};
7376

7477
const LIGHT = {
@@ -86,6 +89,7 @@ const LIGHT = {
8689
errorhandling: false,
8790
warnings: false,
8891
ignorednt: false,
92+
allowparams: false,
8993
};
9094

9195
const templates = [
@@ -95,7 +99,7 @@ const templates = [
9599
output: `hello.js`,
96100
variables: {
97101
...DEFAULTS,
98-
scriptName: "cdn_hello",
102+
scriptName: `cdn_hello_${VERSION}`,
99103
sri: false,
100104
baseUrl: "simpleanalyticscdn.com",
101105
apiUrlPrefix: "queue.",
@@ -202,7 +206,7 @@ const templates = [
202206
{
203207
type: "js",
204208
input: `${__dirname}/src/auto-events.js`,
205-
output: `custom/auto-events.js`,
209+
output: "custom/auto-events.js",
206210
variables: {
207211
version: VERSION,
208212
sri: true,

0 commit comments

Comments
 (0)