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!

publishing unminified UMD builds

Blue Mouse09bf00d0a4cd
diff --git a/dist/swetrix.js b/dist/swetrix.js
index 71772b3bc..22811e180 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 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})});
+!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(n){"use strict";function i(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,n=1,i=arguments.length;n<i;n++)for(var a in e=arguments[n])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={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){var e,n,i;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(n=setInterval(this.trackPathChange,2e3)),null!=t&&t.noHeartbeat||(setTimeout(this.heartbeat,3e3),e=setInterval(this.heartbeat,28e3)),i=a(),this.pageData={path:i,actions:{stop:function(){clearInterval(n),clearInterval(e)}}},this.trackPage(i,null==t?void 0:t.unique)),this.pageData.actions):c},t.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},t.prototype.checkIgnore=function(t){var e,n=null==(e=this.pageViewsOptions)?void 0:e.ignore;if(Array.isArray(n))for(var i=0;i<n.length;++i){if(n[i]===t)return!0;if(n[i]instanceof RegExp&&n[i].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)||(e={pid:this.projectID,lc:void 0!==navigator.languages?navigator.languages[0]:navigator.language,tz:function(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}(),ref:document.referrer||void 0,so:i(o),me:i(s),ca:i(r),unique:e,pg:t},this.sendRequest("",e)))},t.prototype.debug=function(t){var e;null!=(e=this.options)&&e.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t;return null!=(t=this.options)&&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)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!(!(null!=(t=this.options)&&t.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 n=(null==(n=this.options)?void 0:n.apiURL)||"https://api.swetrix.com/log",i=new XMLHttpRequest;i.open("POST","".concat(n,"/").concat(t),!0),i.setRequestHeader("Content-Type","application/json"),i.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)}n.LIB_INSTANCE=null,n.init=function(t,e){return n.LIB_INSTANCE||(n.LIB_INSTANCE=new u(t,e)),n.LIB_INSTANCE},n.track=function(t){n.LIB_INSTANCE&&n.LIB_INSTANCE.track(t)},n.trackViews=function(t){return n.LIB_INSTANCE?n.LIB_INSTANCE.trackPageViews(t):c},Object.defineProperty(n,"__esModule",{value:!0})});
diff --git a/dist/swetrix.orig.js b/dist/swetrix.orig.js
new file mode 100644
index 000000000..800fabe2e
--- /dev/null
+++ b/dist/swetrix.orig.js
@@ -0,0 +1,260 @@
+(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 defaultPageActions = {
+ stop: function () { },
+ };
+ var DEFAULT_API_HOST = 'https://api.swetrix.com/log';
+ var Lib = /** @class */ (function () {
+ function Lib(projectID, options) {
+ this.projectID = projectID;
+ this.options = options;
+ this.pageData = null;
+ this.pageViewsOptions = null;
+ this.trackPathChange = this.trackPathChange.bind(this);
+ this.heartbeat = this.heartbeat.bind(this);
+ }
+ Lib.prototype.track = function (event) {
+ if (!this.canTrack()) {
+ return;
+ }
+ var data = __assign({ pid: this.projectID }, event);
+ this.sendRequest('custom', data);
+ };
+ Lib.prototype.trackPageViews = function (options) {
+ if (!this.canTrack()) {
+ return defaultPageActions;
+ }
+ if (this.pageData) {
+ return this.pageData.actions;
+ }
+ this.pageViewsOptions = options;
+ var hbInterval, interval;
+ if (!(options === null || options === void 0 ? void 0 : options.unique)) {
+ interval = setInterval(this.trackPathChange, 2000);
+ }
+ if (!(options === null || options === void 0 ? void 0 : options.noHeartbeat)) {
+ setTimeout(this.heartbeat, 3000);
+ hbInterval = setInterval(this.heartbeat, 28000);
+ }
+ var path = getPath();
+ this.pageData = {
+ path: path,
+ actions: {
+ stop: function () {
+ clearInterval(interval);
+ clearInterval(hbInterval);
+ },
+ },
+ };
+ this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);
+ return this.pageData.actions;
+ };
+ Lib.prototype.heartbeat = function () {
+ var _a;
+ if (!((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.heartbeatOnBackground) && document.visibilityState === 'hidden') {
+ return;
+ }
+ var data = {
+ pid: this.projectID,
+ };
+ this.sendRequest('hb', data);
+ };
+ Lib.prototype.checkIgnore = function (path) {
+ var _a;
+ var ignore = (_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.ignore;
+ if (Array.isArray(ignore)) {
+ 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;
+ }
+ }
+ return false;
+ };
+ // 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, false);
+ }
+ };
+ Lib.prototype.trackPage = function (pg, unique) {
+ if (unique === void 0) { unique = false; }
+ if (!this.pageData)
+ return;
+ this.pageData.path = pg;
+ if (this.checkIgnore(pg))
+ return;
+ var data = {
+ pid: this.projectID,
+ lc: getLocale(),
+ tz: getTimezone(),
+ ref: getReferrer(),
+ so: getUTMSource(),
+ me: getUTMMedium(),
+ ca: getUTMCampaign(),
+ unique: unique,
+ pg: pg,
+ };
+ this.sendRequest('', data);
+ };
+ Lib.prototype.debug = function (message) {
+ var _a;
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.debug) {
+ console.log('[Swetrix]', message);
+ }
+ };
+ Lib.prototype.canTrack = function () {
+ var _a, _b, _c, _d;
+ if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) {
+ this.debug('Tracking disabled: the \'disabled\' setting is set to true.');
+ return false;
+ }
+ if (!isInBrowser()) {
+ this.debug('Tracking disabled: script does not run in browser environment.');
+ return false;
+ }
+ if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') {
+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.');
+ return false;
+ }
+ if (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.debug) && isLocalhost()) {
+ return false;
+ }
+ if (isAutomated()) {
+ this.debug('Tracking disabled: navigation is automated by WebDriver.');
+ return false;
+ }
+ return true;
+ };
+ Lib.prototype.sendRequest = function (path, body) {
+ var _a;
+ var host = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.apiURL) || DEFAULT_API_HOST;
+ var req = new XMLHttpRequest();
+ req.open('POST', "".concat(host, "/").concat(path), true);
+ req.setRequestHeader('Content-Type', 'application/json');
+ req.send(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).
+ *
+ * @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;
+ }
+ /**
+ * 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 defaultPageActions;
+ return exports.LIB_INSTANCE.trackPageViews(options);
+ }
+
+ exports.init = init;
+ exports.track = track;
+ exports.trackViews = trackViews;
+
+ Object.defineProperty(exports, '__esModule', { value: true });
+
+}));
diff --git a/package.json b/package.json
index f9ed7aeb1..5c2829de0 100644
--- a/package.json
+++ b/package.json
@@ -5,6 +5,7 @@
"main": "dist/swetrix.cjs.js",
"module": "dist/swetrix.es5.js",
"browser": "dist/swetrix.js",
+ "origbrowser": "dist/swetrix.orig.js",
"esnext": "dist/esnext/index.js",
"typings": "dist/esnext/index.d.ts",
"scripts": {
@@ -33,7 +34,8 @@
},
"homepage": "https://swetrix.com/docs",
"dependencies": {
- "@types/node": "^14.18.0"
+ "@types/node": "^14.18.0",
+ "tslib": "^2.4.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
diff --git a/rollup.config.js b/rollup.config.js
index 1a5d757e5..f5d1e471d 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -11,6 +11,7 @@ export default [
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' },
+ { file: pkg.origbrowser, format: 'umd', name: 'swetrix' },
],
plugins: [
typescript(), nodeResolve(), commonjs(), sourceMaps(),