Skip to content

Commit 4bfa39d

Browse files
JeanMeijeradriaandotcom
authored andcommitted
feat: add script options
1 parent 6951e14 commit 4bfa39d

File tree

5 files changed

+136
-19
lines changed

5 files changed

+136
-19
lines changed

dist/index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ declare global {
99
export interface SimpleAnalyticsOptions {
1010
skip?: boolean | (() => boolean) | Promise<boolean>;
1111
domain?: string;
12+
autoCollect?: boolean;
13+
collectDnt?: boolean;
14+
hostname?: string;
15+
mode?: "dash";
16+
ignoreMetrics?: {
17+
referrer?: boolean;
18+
utm?: boolean;
19+
country?: boolean;
20+
session?: boolean;
21+
timeonpage?: boolean;
22+
scrolled?: boolean;
23+
useragent?: boolean;
24+
screensize?: boolean;
25+
viewportsize?: boolean;
26+
language?: boolean;
27+
};
28+
ignorePages?: string[];
29+
allowParams?: string[];
30+
nonUniqueParams?: string[];
31+
strictUtm?: boolean;
1232
}
1333

1434
export declare const saEventKey: InjectionKey<(event: string) => void>;

dist/index.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ Object.defineProperty(exports, '__esModule', { value: true });
55
/* globals document */
66
const saEventKey = Symbol('saEvent');
77

8+
function parseOptions(options) {
9+
const metrics = options.ignoreMetrics
10+
? Object.entries(options.ignoreMetrics)
11+
.filter(([_, value]) => value)
12+
.map(([key]) => `${key}`)
13+
.join(",")
14+
: undefined;
15+
16+
return {
17+
"data-auto-collect": options.autoCollect,
18+
"data-collect-dnt": options.collectDnt,
19+
"data-hostname": options.hostname,
20+
"data-mode": options.mode,
21+
"data-ignore-metrics": metrics === "" ? undefined : metrics,
22+
"data-ignore-pages": options.ignorePages?.join(","),
23+
"data-allow-params": options.allowParams?.join(","),
24+
"data-non-unique-params": options.nonUniqueParams?.join(","),
25+
"data-strict-utm": options.strictUtm,
26+
};
27+
}
28+
829
const isPromise = (subject) =>
930
subject && subject.then && typeof subject.then == "function";
1031

@@ -13,14 +34,18 @@ const warn = (message) => {
1334
console.warn("Simple Analytics: " + message || "Something goes wrong.");
1435
};
1536

16-
const injectScript = (app, domain) => {
37+
const injectScript = (app, domain, options) => {
1738
if (typeof document === "undefined") return warn("No document defined.");
1839
if (document.getElementById("sa-script")) return; // Script already loaded
1940
const el = document.createElement("script");
2041
el.id = "sa-script";
2142
el.type = "text/javascript";
2243
el.async = true;
2344
el.src = "https://" + domain + "/latest.js";
45+
const attributes = parseOptions(options);
46+
Object.entries(attributes).forEach(([key, value]) => {
47+
if (value) el.setAttribute(key, value);
48+
});
2449
document.head.appendChild(el);
2550

2651
// Add a global 'saEvent' method when the script has been loaded
@@ -47,21 +72,21 @@ const handleSkipOrLocalhost = (app) => {
4772
};
4873

4974
var index = {
50-
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com" }) {
51-
if (skip === false) return injectScript(app, domain);
75+
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com", ...options }) {
76+
if (skip === false) return injectScript(app, domain, options);
5277

5378
// If skip is promise, resolve first. With failure always inject script
5479
if (isPromise(skip))
5580
return skip
5681
.then((value) => {
57-
if (value !== true) return injectScript(app, domain);
82+
if (value !== true) return injectScript(app, domain, options);
5883
else return warn("Not sending requests because skip is active.");
5984
})
60-
.catch(injectScript);
85+
.catch(injectScript(app, domain, options));
6186

6287
// If skip function, execute and inject when not skipping
6388
if (typeof skip === "function" && skip() !== true)
64-
return injectScript(app, domain);
89+
return injectScript(app, domain, options);
6590

6691
// Add event catching function to Vue prototype
6792
if (skip) handleSkipOrLocalhost(app);
@@ -72,4 +97,5 @@ var index = {
7297
};
7398

7499
exports.default = index;
100+
exports.parseOptions = parseOptions;
75101
exports.saEventKey = saEventKey;

dist/index.mjs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
/* globals document */
22
const saEventKey = Symbol('saEvent');
33

4+
function parseOptions(options) {
5+
const metrics = options.ignoreMetrics
6+
? Object.entries(options.ignoreMetrics)
7+
.filter(([_, value]) => value)
8+
.map(([key]) => `${key}`)
9+
.join(",")
10+
: undefined;
11+
12+
return {
13+
"data-auto-collect": options.autoCollect,
14+
"data-collect-dnt": options.collectDnt,
15+
"data-hostname": options.hostname,
16+
"data-mode": options.mode,
17+
"data-ignore-metrics": metrics === "" ? undefined : metrics,
18+
"data-ignore-pages": options.ignorePages?.join(","),
19+
"data-allow-params": options.allowParams?.join(","),
20+
"data-non-unique-params": options.nonUniqueParams?.join(","),
21+
"data-strict-utm": options.strictUtm,
22+
};
23+
}
24+
425
const isPromise = (subject) =>
526
subject && subject.then && typeof subject.then == "function";
627

@@ -9,14 +30,18 @@ const warn = (message) => {
930
console.warn("Simple Analytics: " + message || "Something goes wrong.");
1031
};
1132

12-
const injectScript = (app, domain) => {
33+
const injectScript = (app, domain, options) => {
1334
if (typeof document === "undefined") return warn("No document defined.");
1435
if (document.getElementById("sa-script")) return; // Script already loaded
1536
const el = document.createElement("script");
1637
el.id = "sa-script";
1738
el.type = "text/javascript";
1839
el.async = true;
1940
el.src = "https://" + domain + "/latest.js";
41+
const attributes = parseOptions(options);
42+
Object.entries(attributes).forEach(([key, value]) => {
43+
if (value) el.setAttribute(key, value);
44+
});
2045
document.head.appendChild(el);
2146

2247
// Add a global 'saEvent' method when the script has been loaded
@@ -43,21 +68,21 @@ const handleSkipOrLocalhost = (app) => {
4368
};
4469

4570
var index = {
46-
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com" }) {
47-
if (skip === false) return injectScript(app, domain);
71+
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com", ...options }) {
72+
if (skip === false) return injectScript(app, domain, options);
4873

4974
// If skip is promise, resolve first. With failure always inject script
5075
if (isPromise(skip))
5176
return skip
5277
.then((value) => {
53-
if (value !== true) return injectScript(app, domain);
78+
if (value !== true) return injectScript(app, domain, options);
5479
else return warn("Not sending requests because skip is active.");
5580
})
56-
.catch(injectScript);
81+
.catch(injectScript(app, domain, options));
5782

5883
// If skip function, execute and inject when not skipping
5984
if (typeof skip === "function" && skip() !== true)
60-
return injectScript(app, domain);
85+
return injectScript(app, domain, options);
6186

6287
// Add event catching function to Vue prototype
6388
if (skip) handleSkipOrLocalhost(app);
@@ -67,4 +92,4 @@ var index = {
6792
},
6893
};
6994

70-
export { index as default, saEventKey };
95+
export { index as default, parseOptions, saEventKey };

src/index.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
/* globals document */
22
export const saEventKey = Symbol('saEvent');
33

4+
export function parseOptions(options) {
5+
const metrics = options.ignoreMetrics
6+
? Object.entries(options.ignoreMetrics)
7+
.filter(([_, value]) => value)
8+
.map(([key]) => `${key}`)
9+
.join(",")
10+
: undefined;
11+
12+
return {
13+
"data-auto-collect": options.autoCollect,
14+
"data-collect-dnt": options.collectDnt,
15+
"data-hostname": options.hostname,
16+
"data-mode": options.mode,
17+
"data-ignore-metrics": metrics === "" ? undefined : metrics,
18+
"data-ignore-pages": options.ignorePages?.join(","),
19+
"data-allow-params": options.allowParams?.join(","),
20+
"data-non-unique-params": options.nonUniqueParams?.join(","),
21+
"data-strict-utm": options.strictUtm,
22+
};
23+
}
24+
425
const isPromise = (subject) =>
526
subject && subject.then && typeof subject.then == "function";
627

@@ -9,14 +30,19 @@ const warn = (message) => {
930
console.warn("Simple Analytics: " + message || "Something goes wrong.");
1031
};
1132

12-
const injectScript = (app, domain) => {
33+
const injectScript = (app, domain, options) => {
1334
if (typeof document === "undefined") return warn("No document defined.");
1435
if (document.getElementById("sa-script")) return; // Script already loaded
1536
const el = document.createElement("script");
1637
el.id = "sa-script";
1738
el.type = "text/javascript";
1839
el.async = true;
1940
el.src = "https://" + domain + "/latest.js";
41+
const attributes = parseOptions(options);
42+
for (const key in attributes) {
43+
const value = attributes[key];
44+
if (value) el.setAttribute(key, value);
45+
}
2046
document.head.appendChild(el);
2147

2248
// Add a global 'saEvent' method when the script has been loaded
@@ -43,21 +69,21 @@ const handleSkipOrLocalhost = (app) => {
4369
};
4470

4571
export default {
46-
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com" }) {
47-
if (skip === false) return injectScript(app, domain);
72+
install(app, { skip = false, domain = "scripts.simpleanalyticscdn.com", ...options }) {
73+
if (skip === false) return injectScript(app, domain, options);
4874

4975
// If skip is promise, resolve first. With failure always inject script
5076
if (isPromise(skip))
5177
return skip
5278
.then((value) => {
53-
if (value !== true) return injectScript(app, domain);
79+
if (value !== true) return injectScript(app, domain, options);
5480
else return warn("Not sending requests because skip is active.");
5581
})
56-
.catch(injectScript);
82+
.catch(injectScript(app, domain, options));
5783

5884
// If skip function, execute and inject when not skipping
5985
if (typeof skip === "function" && skip() !== true)
60-
return injectScript(app, domain);
86+
return injectScript(app, domain, options);
6187

6288
// Add event catching function to Vue prototype
6389
if (skip) handleSkipOrLocalhost(app);

types/index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ declare global {
99
export interface SimpleAnalyticsOptions {
1010
skip?: boolean | (() => boolean) | Promise<boolean>;
1111
domain?: string;
12+
autoCollect?: boolean;
13+
collectDnt?: boolean;
14+
hostname?: string;
15+
mode?: "dash";
16+
ignoreMetrics?: {
17+
referrer?: boolean;
18+
utm?: boolean;
19+
country?: boolean;
20+
session?: boolean;
21+
timeonpage?: boolean;
22+
scrolled?: boolean;
23+
useragent?: boolean;
24+
screensize?: boolean;
25+
viewportsize?: boolean;
26+
language?: boolean;
27+
};
28+
ignorePages?: string[];
29+
allowParams?: string[];
30+
nonUniqueParams?: string[];
31+
strictUtm?: boolean;
1232
}
1333

1434
export declare const saEventKey: InjectionKey<(event: string) => void>;

0 commit comments

Comments
 (0)