Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

v1.0.1 build

Blue Mouse39488d7bedff
diff --git a/dist/analytics.cjs.js b/dist/analytics.cjs.js
new file mode 100644
index 000000000..87fef1ada
--- /dev/null
+++ b/dist/analytics.cjs.js
@@ -0,0 +1,199 @@
+'use strict';
+
+Object.defineProperty(exports, '__esModule', { value: true });
+
+/*! *****************************************************************************
+Copyright (c) Microsoft Corporation.
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+***************************************************************************** */
+
+var __assign = function() {
+ __assign = Object.assign || function __assign(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+};
+
+var findInSearch = function (exp) {
+ var res = location.search.match(exp);
+ return (res && res[2]) || undefined;
+};
+var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
+var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
+var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
+var isInBrowser = function () {
+ return typeof window !== 'undefined';
+};
+var isLocalhost = function () {
+ return (location === null || location === void 0 ? void 0 : location.hostname) === 'localhost' || (location === null || location === void 0 ? void 0 : location.hostname) === '127.0.0.1' || (location === null || location === void 0 ? void 0 : location.hostname) === '';
+};
+var isAutomated = function () {
+ return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
+};
+var getLocale = function () {
+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
+};
+var getTimezone = function () {
+ try {
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
+ }
+ catch (e) {
+ return;
+ }
+};
+var getReferrer = function () {
+ return document.referrer || undefined;
+};
+var getUTMSource = function () { return findInSearch(utmSourceRegex); };
+var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
+var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
+var getPath = function () {
+ // TODO: Maybe we should also include such data as location.hash or location.search
+ return location.pathname || '';
+};
+
+var host = 'http://localhost:5005/log';
+var Lib = /** @class */ (function () {
+ function Lib(projectID, options) {
+ this.projectID = projectID;
+ this.options = options;
+ this.pageData = null;
+ }
+ // Tracks a custom event
+ Lib.prototype.track = function (event) {
+ if (!this.canTrack()) {
+ return;
+ }
+ var data = __assign({ pid: this.projectID }, event);
+ this.submitCustom(data);
+ };
+ // Tracks page views
+ Lib.prototype.trackPageViews = function (options) {
+ if (!this.canTrack()) {
+ return;
+ }
+ if (this.pageData) {
+ return this.pageData.actions;
+ }
+ var interval = setInterval(this.trackPathChange, 1000);
+ var path = getPath();
+ this.pageData = {
+ path: path,
+ actions: {
+ stop: function () { return clearInterval(interval); }
+ }
+ };
+ this.trackPage(path);
+ };
+ // Tracking path changes. If path changes -> calling this.trackPage method
+ Lib.prototype.trackPathChange = function () {
+ if (!this.pageData)
+ return;
+ var newPath = getPath();
+ var path = this.pageData.path;
+ if (path !== newPath) {
+ this.trackPage(newPath);
+ }
+ };
+ Lib.prototype.trackPage = function (pg) {
+ if (!this.pageData)
+ return;
+ this.pageData.path = pg;
+ var data = {
+ pid: this.projectID,
+ lc: getLocale(),
+ tz: getTimezone(),
+ ref: getReferrer(),
+ so: getUTMSource(),
+ me: getUTMMedium(),
+ ca: getUTMCampaign(),
+ pg: pg,
+ };
+ this.submitData(data);
+ };
+ Lib.prototype.debug = function (message) {
+ var _a;
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
+ console.log('[Analytics]', message);
+ }
+ };
+ Lib.prototype.canTrack = function () {
+ var _a, _b, _c;
+ if (!isInBrowser()) {
+ this.debug('Tracking disabled: script does not run in browser environment.');
+ return false;
+ }
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.respectDNT) && ((_b = window.navigator) === null || _b === void 0 ? void 0 : _b.doNotTrack) === '1') {
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
+ return false;
+ }
+ if (!((_c = this.options) === null || _c === void 0 ? void 0 : _c.debug) && isLocalhost()) {
+ return false;
+ }
+ if (isAutomated()) {
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
+ return false;
+ }
+ return true;
+ };
+ Lib.prototype.submitData = function (body) {
+ return fetch(host, {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ Lib.prototype.submitCustom = function (body) {
+ return fetch(host + "/custom", {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ return Lib;
+}());
+
+exports.LIB_INSTANCE = null;
+// Initialise the tracking library instance (other methods won't work if the library
+// is not initialised)
+function init(pid, options) {
+ if (!exports.LIB_INSTANCE) {
+ exports.LIB_INSTANCE = new Lib(pid, options);
+ }
+ return exports.LIB_INSTANCE;
+}
+// Tracks custom events
+function track(event) {
+ if (!exports.LIB_INSTANCE)
+ return;
+ exports.LIB_INSTANCE.track(event);
+}
+function trackViews(options) {
+ if (!exports.LIB_INSTANCE)
+ return;
+ exports.LIB_INSTANCE.trackPageViews(options);
+}
+
+exports.init = init;
+exports.track = track;
+exports.trackViews = trackViews;
diff --git a/dist/analytics.es5.js b/dist/analytics.es5.js
new file mode 100644
index 000000000..2bbf13bac
--- /dev/null
+++ b/dist/analytics.es5.js
@@ -0,0 +1,193 @@
+/*! *****************************************************************************
+Copyright (c) Microsoft Corporation.
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+***************************************************************************** */
+
+var __assign = function() {
+ __assign = Object.assign || function __assign(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+};
+
+var findInSearch = function (exp) {
+ var res = location.search.match(exp);
+ return (res && res[2]) || undefined;
+};
+var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
+var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
+var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
+var isInBrowser = function () {
+ return typeof window !== 'undefined';
+};
+var isLocalhost = function () {
+ return (location === null || location === void 0 ? void 0 : location.hostname) === 'localhost' || (location === null || location === void 0 ? void 0 : location.hostname) === '127.0.0.1' || (location === null || location === void 0 ? void 0 : location.hostname) === '';
+};
+var isAutomated = function () {
+ return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
+};
+var getLocale = function () {
+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
+};
+var getTimezone = function () {
+ try {
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
+ }
+ catch (e) {
+ return;
+ }
+};
+var getReferrer = function () {
+ return document.referrer || undefined;
+};
+var getUTMSource = function () { return findInSearch(utmSourceRegex); };
+var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
+var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
+var getPath = function () {
+ // TODO: Maybe we should also include such data as location.hash or location.search
+ return location.pathname || '';
+};
+
+var host = 'http://localhost:5005/log';
+var Lib = /** @class */ (function () {
+ function Lib(projectID, options) {
+ this.projectID = projectID;
+ this.options = options;
+ this.pageData = null;
+ }
+ // Tracks a custom event
+ Lib.prototype.track = function (event) {
+ if (!this.canTrack()) {
+ return;
+ }
+ var data = __assign({ pid: this.projectID }, event);
+ this.submitCustom(data);
+ };
+ // Tracks page views
+ Lib.prototype.trackPageViews = function (options) {
+ if (!this.canTrack()) {
+ return;
+ }
+ if (this.pageData) {
+ return this.pageData.actions;
+ }
+ var interval = setInterval(this.trackPathChange, 1000);
+ var path = getPath();
+ this.pageData = {
+ path: path,
+ actions: {
+ stop: function () { return clearInterval(interval); }
+ }
+ };
+ this.trackPage(path);
+ };
+ // Tracking path changes. If path changes -> calling this.trackPage method
+ Lib.prototype.trackPathChange = function () {
+ if (!this.pageData)
+ return;
+ var newPath = getPath();
+ var path = this.pageData.path;
+ if (path !== newPath) {
+ this.trackPage(newPath);
+ }
+ };
+ Lib.prototype.trackPage = function (pg) {
+ if (!this.pageData)
+ return;
+ this.pageData.path = pg;
+ var data = {
+ pid: this.projectID,
+ lc: getLocale(),
+ tz: getTimezone(),
+ ref: getReferrer(),
+ so: getUTMSource(),
+ me: getUTMMedium(),
+ ca: getUTMCampaign(),
+ pg: pg,
+ };
+ this.submitData(data);
+ };
+ Lib.prototype.debug = function (message) {
+ var _a;
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
+ console.log('[Analytics]', message);
+ }
+ };
+ Lib.prototype.canTrack = function () {
+ var _a, _b, _c;
+ if (!isInBrowser()) {
+ this.debug('Tracking disabled: script does not run in browser environment.');
+ return false;
+ }
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.respectDNT) && ((_b = window.navigator) === null || _b === void 0 ? void 0 : _b.doNotTrack) === '1') {
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
+ return false;
+ }
+ if (!((_c = this.options) === null || _c === void 0 ? void 0 : _c.debug) && isLocalhost()) {
+ return false;
+ }
+ if (isAutomated()) {
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
+ return false;
+ }
+ return true;
+ };
+ Lib.prototype.submitData = function (body) {
+ return fetch(host, {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ Lib.prototype.submitCustom = function (body) {
+ return fetch(host + "/custom", {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ return Lib;
+}());
+
+var LIB_INSTANCE = null;
+// Initialise the tracking library instance (other methods won't work if the library
+// is not initialised)
+function init(pid, options) {
+ if (!LIB_INSTANCE) {
+ LIB_INSTANCE = new Lib(pid, options);
+ }
+ return LIB_INSTANCE;
+}
+// Tracks custom events
+function track(event) {
+ if (!LIB_INSTANCE)
+ return;
+ LIB_INSTANCE.track(event);
+}
+function trackViews(options) {
+ if (!LIB_INSTANCE)
+ return;
+ LIB_INSTANCE.trackPageViews(options);
+}
+
+export { LIB_INSTANCE, init, track, trackViews };
diff --git a/dist/analytics.umd.js b/dist/analytics.umd.js
new file mode 100644
index 000000000..648b20771
--- /dev/null
+++ b/dist/analytics.umd.js
@@ -0,0 +1,205 @@
+(function (global, factory) {
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.swetrix = {}));
+}(this, (function (exports) { 'use strict';
+
+ /*! *****************************************************************************
+ Copyright (c) Microsoft Corporation.
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ PERFORMANCE OF THIS SOFTWARE.
+ ***************************************************************************** */
+
+ var __assign = function() {
+ __assign = Object.assign || function __assign(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+ };
+
+ var findInSearch = function (exp) {
+ var res = location.search.match(exp);
+ return (res && res[2]) || undefined;
+ };
+ var utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
+ var utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
+ var utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
+ var isInBrowser = function () {
+ return typeof window !== 'undefined';
+ };
+ var isLocalhost = function () {
+ return (location === null || location === void 0 ? void 0 : location.hostname) === 'localhost' || (location === null || location === void 0 ? void 0 : location.hostname) === '127.0.0.1' || (location === null || location === void 0 ? void 0 : location.hostname) === '';
+ };
+ var isAutomated = function () {
+ return navigator === null || navigator === void 0 ? void 0 : navigator.webdriver;
+ };
+ var getLocale = function () {
+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
+ };
+ var getTimezone = function () {
+ try {
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
+ }
+ catch (e) {
+ return;
+ }
+ };
+ var getReferrer = function () {
+ return document.referrer || undefined;
+ };
+ var getUTMSource = function () { return findInSearch(utmSourceRegex); };
+ var getUTMMedium = function () { return findInSearch(utmMediumRegex); };
+ var getUTMCampaign = function () { return findInSearch(utmCampaignRegex); };
+ var getPath = function () {
+ // TODO: Maybe we should also include such data as location.hash or location.search
+ return location.pathname || '';
+ };
+
+ var host = 'http://localhost:5005/log';
+ var Lib = /** @class */ (function () {
+ function Lib(projectID, options) {
+ this.projectID = projectID;
+ this.options = options;
+ this.pageData = null;
+ }
+ // Tracks a custom event
+ Lib.prototype.track = function (event) {
+ if (!this.canTrack()) {
+ return;
+ }
+ var data = __assign({ pid: this.projectID }, event);
+ this.submitCustom(data);
+ };
+ // Tracks page views
+ Lib.prototype.trackPageViews = function (options) {
+ if (!this.canTrack()) {
+ return;
+ }
+ if (this.pageData) {
+ return this.pageData.actions;
+ }
+ var interval = setInterval(this.trackPathChange, 1000);
+ var path = getPath();
+ this.pageData = {
+ path: path,
+ actions: {
+ stop: function () { return clearInterval(interval); }
+ }
+ };
+ this.trackPage(path);
+ };
+ // Tracking path changes. If path changes -> calling this.trackPage method
+ Lib.prototype.trackPathChange = function () {
+ if (!this.pageData)
+ return;
+ var newPath = getPath();
+ var path = this.pageData.path;
+ if (path !== newPath) {
+ this.trackPage(newPath);
+ }
+ };
+ Lib.prototype.trackPage = function (pg) {
+ if (!this.pageData)
+ return;
+ this.pageData.path = pg;
+ var data = {
+ pid: this.projectID,
+ lc: getLocale(),
+ tz: getTimezone(),
+ ref: getReferrer(),
+ so: getUTMSource(),
+ me: getUTMMedium(),
+ ca: getUTMCampaign(),
+ pg: pg,
+ };
+ this.submitData(data);
+ };
+ Lib.prototype.debug = function (message) {
+ var _a;
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
+ console.log('[Analytics]', message);
+ }
+ };
+ Lib.prototype.canTrack = function () {
+ var _a, _b, _c;
+ if (!isInBrowser()) {
+ this.debug('Tracking disabled: script does not run in browser environment.');
+ return false;
+ }
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.respectDNT) && ((_b = window.navigator) === null || _b === void 0 ? void 0 : _b.doNotTrack) === '1') {
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
+ return false;
+ }
+ if (!((_c = this.options) === null || _c === void 0 ? void 0 : _c.debug) && isLocalhost()) {
+ return false;
+ }
+ if (isAutomated()) {
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
+ return false;
+ }
+ return true;
+ };
+ Lib.prototype.submitData = function (body) {
+ return fetch(host, {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ Lib.prototype.submitCustom = function (body) {
+ return fetch(host + "/custom", {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ };
+ return Lib;
+ }());
+
+ exports.LIB_INSTANCE = null;
+ // Initialise the tracking library instance (other methods won't work if the library
+ // is not initialised)
+ function init(pid, options) {
+ if (!exports.LIB_INSTANCE) {
+ exports.LIB_INSTANCE = new Lib(pid, options);
+ }
+ return exports.LIB_INSTANCE;
+ }
+ // Tracks custom events
+ function track(event) {
+ if (!exports.LIB_INSTANCE)
+ return;
+ exports.LIB_INSTANCE.track(event);
+ }
+ function trackViews(options) {
+ if (!exports.LIB_INSTANCE)
+ return;
+ exports.LIB_INSTANCE.trackPageViews(options);
+ }
+
+ exports.init = init;
+ exports.track = track;
+ exports.trackViews = trackViews;
+
+ Object.defineProperty(exports, '__esModule', { value: true });
+
+})));
diff --git a/dist/esnext/Lib.d.ts b/dist/esnext/Lib.d.ts
new file mode 100644
index 000000000..013fcd2eb
--- /dev/null
+++ b/dist/esnext/Lib.d.ts
@@ -0,0 +1,29 @@
+export interface LibOptions {
+ debug?: boolean;
+ disabled?: boolean;
+ respectDNT?: boolean;
+}
+export interface TrackEventOptions {
+ ev: string;
+ unique?: boolean;
+}
+export interface PageData {
+ path: string;
+ actions: object;
+}
+export interface PageViewsOptions {
+}
+export declare class Lib {
+ private projectID;
+ private options?;
+ private pageData;
+ constructor(projectID: string, options?: LibOptions | undefined);
+ track(event: TrackEventOptions): void;
+ trackPageViews(options?: PageViewsOptions): object | undefined;
+ private trackPathChange;
+ private trackPage;
+ private debug;
+ private canTrack;
+ private submitData;
+ private submitCustom;
+}
diff --git a/dist/esnext/Lib.js b/dist/esnext/Lib.js
new file mode 100644
index 000000000..437b9fee0
--- /dev/null
+++ b/dist/esnext/Lib.js
@@ -0,0 +1,108 @@
+import { isInBrowser, isLocalhost, isAutomated, getLocale, getTimezone, getReferrer, getUTMCampaign, getUTMMedium, getUTMSource, getPath, } from './utils';
+const host = 'http://localhost:5005/log';
+export class Lib {
+ constructor(projectID, options) {
+ this.projectID = projectID;
+ this.options = options;
+ this.pageData = null;
+ }
+ // Tracks a custom event
+ track(event) {
+ if (!this.canTrack()) {
+ return;
+ }
+ const data = {
+ pid: this.projectID,
+ ...event,
+ };
+ this.submitCustom(data);
+ }
+ // Tracks page views
+ trackPageViews(options) {
+ if (!this.canTrack()) {
+ return;
+ }
+ if (this.pageData) {
+ return this.pageData.actions;
+ }
+ const interval = setInterval(this.trackPathChange, 1000);
+ const path = getPath();
+ this.pageData = {
+ path,
+ actions: {
+ stop: () => clearInterval(interval)
+ }
+ };
+ this.trackPage(path);
+ }
+ // Tracking path changes. If path changes -> calling this.trackPage method
+ trackPathChange() {
+ if (!this.pageData)
+ return;
+ const newPath = getPath();
+ const { path } = this.pageData;
+ if (path !== newPath) {
+ this.trackPage(newPath);
+ }
+ }
+ trackPage(pg) {
+ if (!this.pageData)
+ return;
+ this.pageData.path = pg;
+ const data = {
+ pid: this.projectID,
+ lc: getLocale(),
+ tz: getTimezone(),
+ ref: getReferrer(),
+ so: getUTMSource(),
+ me: getUTMMedium(),
+ ca: getUTMCampaign(),
+ pg,
+ };
+ this.submitData(data);
+ }
+ debug(message) {
+ if (this.options?.debug) {
+ console.log('[Analytics]', message);
+ }
+ }
+ canTrack() {
+ if (!isInBrowser()) {
+ this.debug('Tracking disabled: script does not run in browser environment.');
+ return false;
+ }
+ if (this.options?.respectDNT && window.navigator?.doNotTrack === '1') {
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
+ return false;
+ }
+ if (!this.options?.debug && isLocalhost()) {
+ return false;
+ }
+ if (isAutomated()) {
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
+ return false;
+ }
+ return true;
+ }
+ submitData(body) {
+ return fetch(host, {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ }
+ submitCustom(body) {
+ return fetch(`${host}/custom`, {
+ method: 'post',
+ headers: {
+ 'Accept': 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
+ }
+}
+//# sourceMappingURL=Lib.js.map
\ No newline at end of file
diff --git a/dist/esnext/Lib.js.map b/dist/esnext/Lib.js.map
new file mode 100644
index 000000000..112514c25
--- /dev/null
+++ b/dist/esnext/Lib.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"Lib.js","sourceRoot":"","sources":["../../src/Lib.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAC1E,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,GACpD,MAAM,SAAS,CAAA;AA2ChB,MAAM,IAAI,GAAG,2BAA2B,CAAA;AAExC,MAAM,OAAO,G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E,kBAAkB;gBAC5B,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;IACJ,CAAC;IAEO,YAAY,CAAC,IAAY;QAC/B,OAAO,KAAK,CAAC,GAAG,IAAI,SAAS,EAAE;YAC7B,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,QAAQ,EAAE,kBAAkB;gBAC5B,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;IACJ,CAAC;CACF"}
\ No newline at end of file
diff --git a/dist/esnext/index.d.ts b/dist/esnext/index.d.ts
new file mode 100644
index 000000000..87430e5e9
--- /dev/null
+++ b/dist/esnext/index.d.ts
@@ -0,0 +1,5 @@
+import { Lib, LibOptions, TrackEventOptions, PageViewsOptions } from './Lib';
+export declare let LIB_INSTANCE: Lib | null;
+export declare function init(pid: string, options?: LibOptions): Lib;
+export declare function track(event: TrackEventOptions): void;
+export declare function trackViews(options?: PageViewsOptions): void;
diff --git a/dist/esnext/index.js b/dist/esnext/index.js
new file mode 100644
index 000000000..426ba43c4
--- /dev/null
+++ b/dist/esnext/index.js
@@ -0,0 +1,22 @@
+import { Lib } from './Lib';
+export let LIB_INSTANCE = null;
+// Initialise the tracking library instance (other methods won't work if the library
+// is not initialised)
+export function init(pid, options) {
+ if (!LIB_INSTANCE) {
+ LIB_INSTANCE = new Lib(pid, options);
+ }
+ return LIB_INSTANCE;
+}
+// Tracks custom events
+export function track(event) {
+ if (!LIB_INSTANCE)
+ return;
+ LIB_INSTANCE.track(event);
+}
+export function trackViews(options) {
+ if (!LIB_INSTANCE)
+ return;
+ LIB_INSTANCE.trackPageViews(options);
+}
+//# sourceMappingURL=index.js.map
\ No newline at end of file
diff --git a/dist/esnext/index.js.map b/dist/esnext/index.js.map
new file mode 100644
index 000000000..618caa8c1
--- /dev/null
+++ b/dist/esnext/index.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAmD,MAAM,OAAO,CAAA;AAE5E,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C,oFAAoF;AACpF,sBAAsB;AACtB,MAAM,UAAU,IAAI,CAAE,GAAW,EAAE,OAAoB;IACrD,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;KACrC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,KAAK,CAAE,KAAwB;IAC7C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED,MAAM,UAAU,UAAU,CAAE,OAA0B;IACpD,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;AACtC,CAAC"}
\ No newline at end of file
diff --git a/dist/esnext/utils.d.ts b/dist/esnext/utils.d.ts
new file mode 100644
index 000000000..c65fe5217
--- /dev/null
+++ b/dist/esnext/utils.d.ts
@@ -0,0 +1,10 @@
+export declare const isInBrowser: () => boolean;
+export declare const isLocalhost: () => boolean;
+export declare const isAutomated: () => boolean;
+export declare const getLocale: () => string;
+export declare const getTimezone: () => string | undefined;
+export declare const getReferrer: () => string | undefined;
+export declare const getUTMSource: () => string | undefined;
+export declare const getUTMMedium: () => string | undefined;
+export declare const getUTMCampaign: () => string | undefined;
+export declare const getPath: () => string;
diff --git a/dist/esnext/utils.js b/dist/esnext/utils.js
new file mode 100644
index 000000000..f376b120c
--- /dev/null
+++ b/dist/esnext/utils.js
@@ -0,0 +1,38 @@
+const findInSearch = (exp) => {
+ const res = location.search.match(exp);
+ return (res && res[2]) || undefined;
+};
+const utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
+const utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
+const utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
+export const isInBrowser = () => {
+ return typeof window !== 'undefined';
+};
+export const isLocalhost = () => {
+ return location?.hostname === 'localhost' || location?.hostname === '127.0.0.1' || location?.hostname === '';
+};
+export const isAutomated = () => {
+ return navigator?.webdriver;
+};
+export const getLocale = () => {
+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
+};
+export const getTimezone = () => {
+ try {
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
+ }
+ catch (e) {
+ return;
+ }
+};
+export const getReferrer = () => {
+ return document.referrer || undefined;
+};
+export const getUTMSource = () => findInSearch(utmSourceRegex);
+export const getUTMMedium = () => findInSearch(utmMediumRegex);
+export const getUTMCampaign = () => findInSearch(utmCampaignRegex);
+export const getPath = () => {
+ // TODO: Maybe we should also include such data as location.hash or location.search
+ return location.pathname || '';
+};
+//# sourceMappingURL=utils.js.map
\ No newline at end of file
diff --git a/dist/esnext/utils.js.map b/dist/esnext/utils.js.map
new file mode 100644
index 000000000..78db14da6
--- /dev/null
+++ b/dist/esnext/utils.js.map
@@ -0,0 +1 @@
+{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAC,GAAW,EAAsB,EAAE;IACvD,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACtC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,SAAS,CAAA;AACrC,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,sCAAsC,CAAA;AAC7D,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AACtD,MAAM,cAAc,GAAG,2BAA2B,CAAA;AAElD,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IA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IAAI,EAAE,CAAA;AAChC,CAAC,CAAA"}
\ No newline at end of file
diff --git a/dist/swetrix.js b/dist/swetrix.js
new file mode 100644
index 000000000..24e479dfa
--- /dev/null
+++ b/dist/swetrix.js
@@ -0,0 +1 @@
+!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(n){"use strict";function o(t){return(t=location.search.match(t))&&t[2]||void 0}function e(){return location.pathname||""}var i=function(){return(i=Object.assign||function(t){for(var o,n=1,e=arguments.length;n<e;n++)for(var i in o=arguments[n])Object.prototype.hasOwnProperty.call(o,i)&&(t[i]=o[i]);return t}).apply(this,arguments)},a=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,c=/[?&](utm_medium)=([^?&]+)/,s="http://localhost:5005/log",u=(t.prototype.track=function(t){this.canTrack()&&(t=i({pid:this.projectID},t),this.submitCustom(t))},t.prototype.trackPageViews=function(t){if(this.canTrack()){if(this.pageData)return this.pageData.actions;var o=setInterval(this.trackPathChange,1e3),n=e();this.pageData={path:n,actions:{stop:function(){return clearInterval(o)}}},this.trackPage(n)}},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=e(),this.pageData.path!==t&&this.trackPage(t))},t.prototype.trackPage=function(t){this.pageData&&(this.pageData.path=t,t={pid:this.projectID,lc:void 0!==navigator.languages?navigator.languages[0]:navigator.language,tz:function(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){return}}(),ref:document.referrer||void 0,so:o(a),me:o(c),ca:o(r),pg:t},this.submitData(t))},t.prototype.debug=function(t){var o;null!==(o=this.options)&&void 0!==o&&o.debug&&console.log("[Analytics]",t)},t.prototype.canTrack=function(){var t,o;return"undefined"==typeof window?(this.debug("Tracking disabled: script does not run in browser environment."),!1):null!==(t=this.options)&&void 0!==t&&t.respectDNT&&"1"===(null===(o=window.navigator)||void 0===o?void 0:o.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(o=this.options)&&void 0!==o&&o.debug||"localhost"!==(null===location||void 0===location?void 0:location.hostname)&&"127.0.0.1"!==(null===location||void 0===location?void 0:location.hostname)&&""!==(null===location||void 0===location?void 0:location.hostname))&&(null===navigator||void 0===navigator||!navigator.webdriver||(this.debug("Tracking disabled: navigation is automated by WebDriver."),!1))},t.prototype.submitData=function(t){return fetch(s,{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)})},t.prototype.submitCustom=function(t){return fetch(s+"/custom",{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)})},t);function t(t,o){this.projectID=t,this.options=o,this.pageData=null}n.LIB_INSTANCE=null,n.init=function(t,o){return n.LIB_INSTANCE||(n.LIB_INSTANCE=new u(t,o)),n.LIB_INSTANCE},n.track=function(t){n.LIB_INSTANCE&&n.LIB_INSTANCE.track(t)},n.trackViews=function(t){n.LIB_INSTANCE&&n.LIB_INSTANCE.trackPageViews(t)},Object.defineProperty(n,"__esModule",{value:!0})});