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!

Build a new release

Blue Mouse3af92bf12bc4
diff --git a/dist/esnext/Lib.d.ts b/dist/esnext/Lib.d.ts
index d2624a939..a87590645 100644
--- a/dist/esnext/Lib.d.ts
+++ b/dist/esnext/Lib.d.ts
@@ -1,23 +1,55 @@
export interface LibOptions {
+ /**
+ * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.
+ */
debug?: boolean;
+ /**
+ * When set to `true`, the tracking library won't send any data to server.
+ * Useful for development purposes when this value is set based on `.env` var.
+ */
disabled?: boolean;
+ /**
+ * By setting this flag to `true`, we will not collect ANY kind of data about the user with the DNT setting.
+ */
respectDNT?: boolean;
+ /** Set a custom URL of the API server (for selfhosted variants of Swetrix). */
apiURL?: string;
}
export interface TrackEventOptions {
+ /** The custom event name. */
ev: string;
+ /** If set to `true`, only 1 event with the same ID will be saved per user session. */
unique?: boolean;
}
+/**
+ * The object returned by `trackPageViews()`, used to stop tracking pages.
+ */
+export interface PageActions {
+ /** Stops the tracking of pages. */
+ stop: () => void;
+}
export interface PageData {
+ /** Current URL path. */
path: string;
- actions: object;
+ /** The object returned by `trackPageViews()`, used to stop tracking pages. */
+ actions: PageActions;
}
export interface PageViewsOptions {
+ /**
+ * If set to `true`, only unique events will be saved.
+ * This param is useful when tracking single-page landing websites.
+ */
unique?: boolean;
- ignore?: Array<any>;
+ /** A list of Regular Expressions or string pathes to ignore. */
+ ignore?: Array<string | RegExp>;
+ /** Do not send Heartbeat requests to the server. */
noHeartbeat?: boolean;
+ /** Send Heartbeat requests when the website tab is not active in the browser. */
heartbeatOnBackground?: boolean;
}
+export declare const defaultPageActions: {
+ stop(): void;
+};
export declare class Lib {
private projectID;
private options?;
@@ -25,7 +57,7 @@ export declare class Lib {
private pageViewsOptions;
constructor(projectID: string, options?: LibOptions | undefined);
track(event: TrackEventOptions): void;
- trackPageViews(options?: PageViewsOptions): void | object;
+ trackPageViews(options?: PageViewsOptions): PageActions;
private heartbeat;
private checkIgnore;
private trackPathChange;
diff --git a/dist/esnext/Lib.js b/dist/esnext/Lib.js
index 0cd0c967b..f0181a4e4 100644
--- a/dist/esnext/Lib.js
+++ b/dist/esnext/Lib.js
@@ -1,4 +1,7 @@
import { isInBrowser, isLocalhost, isAutomated, getLocale, getTimezone, getReferrer, getUTMCampaign, getUTMMedium, getUTMSource, getPath, } from './utils';
+export const defaultPageActions = {
+ stop() { },
+};
const DEFAULT_API_HOST = 'https://api.swetrix.com/log';
export class Lib {
constructor(projectID, options) {
@@ -21,7 +24,7 @@ export class Lib {
}
trackPageViews(options) {
if (!this.canTrack()) {
- return;
+ return defaultPageActions;
}
if (this.pageData) {
return this.pageData.actions;
@@ -63,6 +66,7 @@ export class Lib {
for (let i = 0; i < ignore.length; ++i) {
if (ignore[i] === path)
return true;
+ // @ts-ignore
if (ignore[i] instanceof RegExp && ignore[i].test(path))
return true;
}
diff --git a/dist/esnext/Lib.js.map b/dist/esnext/Lib.js.map
index 7c5f272f1..81c657aba 100644
--- a/dist/esnext/Lib.js.map
+++ b/dist/esnext/Lib.js.map
@@ -1 +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;AAkDhB,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AAEtD,MAAM,OAAO,GAAG;IAId,YAAoB,SAAiB,EAAU,OAAoB;QAA/C,cAAS,GAAT,SAAS,CAAQ;QAAU,YAAO,GAAP,OAAO,CAAa;QAH3D,aAAQ,GAAoB,IAAI,CAAA;QAChC,qBAAgB,GAAwC,IAAI,CAAA;QAGlE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eA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OAAO,EAAE,MAAM,IAAI,gBAAgB,CAAA;QACrD,MAAM,GAAG,GAAG,IAAI,cAAc,EAAE,CAAA;QAChC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,IAAI,IAAI,EAAE,EAAE,IAAI,CAAC,CAAA;QACzC,GAAG,CAAC,gBAAgB,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;QACxD,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;IAChC,CAAC;CACF"}
\ No newline at end of file
diff --git a/dist/esnext/index.d.ts b/dist/esnext/index.d.ts
index 87430e5e9..7f322c5b8 100644
--- a/dist/esnext/index.d.ts
+++ b/dist/esnext/index.d.ts
@@ -1,5 +1,27 @@
-import { Lib, LibOptions, TrackEventOptions, PageViewsOptions } from './Lib';
+import { Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions } from './Lib';
export declare let LIB_INSTANCE: Lib | null;
+/**
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ *
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
+ * @param {LibOptions} options Options related to the tracking.
+ * @returns {Lib} Instance of the Swetrix.js.
+ */
export declare function init(pid: string, options?: LibOptions): Lib;
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {TrackEventOptions} event The options related to the custom event.
+ */
export declare function track(event: TrackEventOptions): void;
-export declare function trackViews(options?: PageViewsOptions): void;
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {PageViewsOptions} options The options related to the custom event.
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
+ */
+export declare function trackViews(options?: PageViewsOptions): PageActions;
diff --git a/dist/esnext/index.js b/dist/esnext/index.js
index 426ba43c4..5423addad 100644
--- a/dist/esnext/index.js
+++ b/dist/esnext/index.js
@@ -1,22 +1,41 @@
-import { Lib } from './Lib';
+import { Lib, defaultPageActions, } from './Lib';
export let LIB_INSTANCE = null;
-// Initialise the tracking library instance (other methods won't work if the library
-// is not initialised)
+/**
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ *
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
+ * @param {LibOptions} options Options related to the tracking.
+ * @returns {Lib} Instance of the Swetrix.js.
+ */
export function init(pid, options) {
if (!LIB_INSTANCE) {
LIB_INSTANCE = new Lib(pid, options);
}
return LIB_INSTANCE;
}
-// Tracks custom events
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {TrackEventOptions} event The options related to the custom event.
+ */
export function track(event) {
if (!LIB_INSTANCE)
return;
LIB_INSTANCE.track(event);
}
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {PageViewsOptions} options The options related to the custom event.
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
+ */
export function trackViews(options) {
if (!LIB_INSTANCE)
- return;
- LIB_INSTANCE.trackPageViews(options);
+ return defaultPageActions;
+ 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
index 618caa8c1..7c5c07543 100644
--- a/dist/esnext/index.js.map
+++ b/dist/esnext/index.js.map
@@ -1 +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
+{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAAgE,kBAAkB,GACtF,MAAM,OAAO,CAAA;AAEd,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C;;;;;;GAMG;AACH,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;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAE,KAAwB;IAC7C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAE,OAA0B;IACpD,IAAI,CAAC,YAAY;QAAE,OAAO,kBAAkB,CAAA;IAE5C,OAAO,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;AAC7C,CAAC"}
\ No newline at end of file
diff --git a/dist/swetrix.cjs.js b/dist/swetrix.cjs.js
index f59698905..3dbc190a0 100644
--- a/dist/swetrix.cjs.js
+++ b/dist/swetrix.cjs.js
@@ -66,6 +66,9 @@ var getPath = function () {
return location.pathname || '';
};
+var defaultPageActions = {
+ stop: function () { },
+};
var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
var Lib = /** @class */ (function () {
function Lib(projectID, options) {
@@ -85,7 +88,7 @@ var Lib = /** @class */ (function () {
};
Lib.prototype.trackPageViews = function (options) {
if (!this.canTrack()) {
- return;
+ return defaultPageActions;
}
if (this.pageData) {
return this.pageData.actions;
@@ -129,6 +132,7 @@ var Lib = /** @class */ (function () {
for (var i = 0; i < ignore.length; ++i) {
if (ignore[i] === path)
return true;
+ // @ts-ignore
if (ignore[i] instanceof RegExp && ignore[i].test(path))
return true;
}
@@ -206,24 +210,43 @@ var Lib = /** @class */ (function () {
}());
exports.LIB_INSTANCE = null;
-// Initialise the tracking library instance (other methods won't work if the library
-// is not initialised)
+/**
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ *
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
+ * @param {LibOptions} options Options related to the tracking.
+ * @returns {Lib} Instance of the Swetrix.js.
+ */
function init(pid, options) {
if (!exports.LIB_INSTANCE) {
exports.LIB_INSTANCE = new Lib(pid, options);
}
return exports.LIB_INSTANCE;
}
-// Tracks custom events
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {TrackEventOptions} event The options related to the custom event.
+ */
function track(event) {
if (!exports.LIB_INSTANCE)
return;
exports.LIB_INSTANCE.track(event);
}
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {PageViewsOptions} options The options related to the custom event.
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
+ */
function trackViews(options) {
if (!exports.LIB_INSTANCE)
- return;
- exports.LIB_INSTANCE.trackPageViews(options);
+ return defaultPageActions;
+ return exports.LIB_INSTANCE.trackPageViews(options);
}
exports.init = init;
diff --git a/dist/swetrix.es5.js b/dist/swetrix.es5.js
index 1131c7c6e..7d346a124 100644
--- a/dist/swetrix.es5.js
+++ b/dist/swetrix.es5.js
@@ -62,6 +62,9 @@ var getPath = function () {
return location.pathname || '';
};
+var defaultPageActions = {
+ stop: function () { },
+};
var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
var Lib = /** @class */ (function () {
function Lib(projectID, options) {
@@ -81,7 +84,7 @@ var Lib = /** @class */ (function () {
};
Lib.prototype.trackPageViews = function (options) {
if (!this.canTrack()) {
- return;
+ return defaultPageActions;
}
if (this.pageData) {
return this.pageData.actions;
@@ -125,6 +128,7 @@ var Lib = /** @class */ (function () {
for (var i = 0; i < ignore.length; ++i) {
if (ignore[i] === path)
return true;
+ // @ts-ignore
if (ignore[i] instanceof RegExp && ignore[i].test(path))
return true;
}
@@ -202,24 +206,43 @@ var Lib = /** @class */ (function () {
}());
var LIB_INSTANCE = null;
-// Initialise the tracking library instance (other methods won't work if the library
-// is not initialised)
+/**
+ * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ *
+ * @param {string} pid The Project ID to link the instance of Swetrix.js to.
+ * @param {LibOptions} options Options related to the tracking.
+ * @returns {Lib} Instance of the Swetrix.js.
+ */
function init(pid, options) {
if (!LIB_INSTANCE) {
LIB_INSTANCE = new Lib(pid, options);
}
return LIB_INSTANCE;
}
-// Tracks custom events
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {TrackEventOptions} event The options related to the custom event.
+ */
function track(event) {
if (!LIB_INSTANCE)
return;
LIB_INSTANCE.track(event);
}
+/**
+ * With this function you are able to track any custom events you want.
+ * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
+ * The total number of track calls and their conversion rate will be saved.
+ *
+ * @param {PageViewsOptions} options The options related to the custom event.
+ * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
+ */
function trackViews(options) {
if (!LIB_INSTANCE)
- return;
- LIB_INSTANCE.trackPageViews(options);
+ return defaultPageActions;
+ return LIB_INSTANCE.trackPageViews(options);
}
export { LIB_INSTANCE, init, track, trackViews };
diff --git a/dist/swetrix.js b/dist/swetrix.js
index b70e5c365..71772b3bc 100644
--- a/dist/swetrix.js
+++ b/dist/swetrix.js
@@ -1 +1 @@
-!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(i){"use strict";function n(t){return(t=location.search.match(t))&&t[2]||void 0}function a(){return location.pathname||""}var e=function(){return(e=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var a in e=arguments[i])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t}).apply(this,arguments)},o=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c=(t.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID},t),this.sendRequest("custom",t))},t.prototype.trackPageViews=function(t){if(this.canTrack()){if(this.pageData)return this.pageData.actions;var e,i;null!=(this.pageViewsOptions=t)&&t.unique||(i=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3));var n=a();return this.pageData={path:n,actions:{stop:function(){clearInterval(i),clearInterval(e)}}},this.trackPage(n,null==t?void 0:t.unique),this.pageData.actions}},t.prototype.heartbeat=function(){var t;(null!==(t=this.pageViewsOptions)&&void 0!==t&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},t.prototype.checkIgnore=function(t){var e,i=null===(e=this.pageViewsOptions)||void 0===e?void 0:e.ignore;if(Array.isArray(i))for(var n=0;n<i.length;++n){if(i[n]===t)return!0;if(i[n]instanceof RegExp&&i[n].test(t))return!0}return!1},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=a(),this.pageData.path!==t&&this.trackPage(t,!1))},t.prototype.trackPage=function(t,e){void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,this.checkIgnore(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:n(o),me:n(s),ca:n(r),unique:e,pg:t},this.sendRequest("",t)))},t.prototype.debug=function(t){var e;null!==(e=this.options)&&void 0!==e&&e.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t,e;return null!==(t=this.options)&&void 0!==t&&t.disabled?(this.debug("Tracking disabled: the 'disabled' setting is set to true."),!1):"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===(e=window.navigator)||void 0===e?void 0:e.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(e=this.options)&&void 0!==e&&e.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.sendRequest=function(t,e){var i=(null===(n=this.options)||void 0===n?void 0:n.apiURL)||"https://api.swetrix.com/log",n=new XMLHttpRequest;n.open("POST","".concat(i,"/").concat(t),!0),n.setRequestHeader("Content-Type","application/json"),n.send(JSON.stringify(e))},t);function t(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}i.LIB_INSTANCE=null,i.init=function(t,e){return i.LIB_INSTANCE||(i.LIB_INSTANCE=new c(t,e)),i.LIB_INSTANCE},i.track=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.track(t)},i.trackViews=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.trackPageViews(t)},Object.defineProperty(i,"__esModule",{value:!0})});
+!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(i){"use strict";function n(t){return(t=location.search.match(t))&&t[2]||void 0}function o(){return location.pathname||""}var e=function(){return(e=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var o in e=arguments[i])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},a=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c={stop:function(){}},u=(t.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID},t),this.sendRequest("custom",t))},t.prototype.trackPageViews=function(t){if(!this.canTrack())return c;if(this.pageData)return this.pageData.actions;var e,i;null!=(this.pageViewsOptions=t)&&t.unique||(i=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3));var n=o();return this.pageData={path:n,actions:{stop:function(){clearInterval(i),clearInterval(e)}}},this.trackPage(n,null==t?void 0:t.unique),this.pageData.actions},t.prototype.heartbeat=function(){var t;(null!==(t=this.pageViewsOptions)&&void 0!==t&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},t.prototype.checkIgnore=function(t){var e,i=null===(e=this.pageViewsOptions)||void 0===e?void 0:e.ignore;if(Array.isArray(i))for(var n=0;n<i.length;++n){if(i[n]===t)return!0;if(i[n]instanceof RegExp&&i[n].test(t))return!0}return!1},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=o(),this.pageData.path!==t&&this.trackPage(t,!1))},t.prototype.trackPage=function(t,e){void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,this.checkIgnore(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:n(a),me:n(s),ca:n(r),unique:e,pg:t},this.sendRequest("",t)))},t.prototype.debug=function(t){var e;null!==(e=this.options)&&void 0!==e&&e.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t,e;return null!==(t=this.options)&&void 0!==t&&t.disabled?(this.debug("Tracking disabled: the 'disabled' setting is set to true."),!1):"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===(e=window.navigator)||void 0===e?void 0:e.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(e=this.options)&&void 0!==e&&e.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.sendRequest=function(t,e){var i=(null===(n=this.options)||void 0===n?void 0:n.apiURL)||"https://api.swetrix.com/log",n=new XMLHttpRequest;n.open("POST","".concat(i,"/").concat(t),!0),n.setRequestHeader("Content-Type","application/json"),n.send(JSON.stringify(e))},t);function t(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}i.LIB_INSTANCE=null,i.init=function(t,e){return i.LIB_INSTANCE||(i.LIB_INSTANCE=new u(t,e)),i.LIB_INSTANCE},i.track=function(t){i.LIB_INSTANCE&&i.LIB_INSTANCE.track(t)},i.trackViews=function(t){return i.LIB_INSTANCE?i.LIB_INSTANCE.trackPageViews(t):c},Object.defineProperty(i,"__esModule",{value:!0})});