📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Added 'unique' param for pageviews tracking
Blue Mouse52c03149ea2e
diff --git a/dist/analytics.umd.js b/dist/analytics.umd.jsdeleted file mode 100644index 20a9bd7df..000000000--- a/dist/analytics.umd.js+++ /dev/null@@ -1,207 +0,0 @@-(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 = 'https://api.swetrix.com/log';- var Lib = /** @class */ (function () {- function Lib(projectID, options) {- this.projectID = projectID;- this.options = options;- this.pageData = null;- this.trackPathChange = this.trackPathChange.bind(this);- }- // 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, 2000);- var path = getPath();- this.pageData = {- path: path,- actions: {- stop: function () { return clearInterval(interval); }- },- };- this.trackPage(path);- return this.pageData.actions;- };- // 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('[Swetrix]', 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.tsindex 013fcd2eb..262b55180 100644--- a/dist/esnext/Lib.d.ts+++ b/dist/esnext/Lib.d.ts@@ -12,6 +12,7 @@ export interface PageData {actions: object;}export interface PageViewsOptions {+ unique?: boolean;}export declare class Lib {private projectID;diff --git a/dist/esnext/Lib.js b/dist/esnext/Lib.jsindex 6a1e0aebe..d05c2c4ba 100644--- a/dist/esnext/Lib.js+++ b/dist/esnext/Lib.js@@ -26,15 +26,18 @@ export class Lib {if (this.pageData) {return this.pageData.actions;}- const interval = setInterval(this.trackPathChange, 2000);+ let interval;+ if (!options?.unique) {+ interval = setInterval(this.trackPathChange, 2000);+ }const path = getPath();this.pageData = {path,actions: {- stop: () => clearInterval(interval)+ stop: options?.unique ? () => { } : () => clearInterval(interval),},};- this.trackPage(path);+ this.trackPage(path, options?.unique);return this.pageData.actions;}// Tracking path changes. If path changes -> calling this.trackPage method@@ -44,10 +47,10 @@ export class Lib {const newPath = getPath();const { path } = this.pageData;if (path !== newPath) {- this.trackPage(newPath);+ this.trackPage(newPath, false);}}- trackPage(pg) {+ trackPage(pg, unique = false) {if (!this.pageData)return;this.pageData.path = pg;@@ -59,6 +62,7 @@ export class Lib {so: getUTMSource(),me: getUTMMedium(),ca: getUTMCampaign(),+ unique,pg,};this.submitData(data);diff --git a/dist/esnext/Lib.js.map b/dist/esnext/Lib.js.mapindex bc9c5116a..7c4a4a662 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;AA2ChB,MAAM,IAAI,GAAG,6BAA6B,CAAA;AAE1C,MAAM,OAAO,GAAG;IAGd,YAAoB,SAAiB,EAAU,OAAoB;QAA/C,cAAS,GAAT,SAAS,C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AC;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 filediff --git a/dist/analytics.cjs.js b/dist/swetrix.cjs.jssimilarity index 89%rename from dist/analytics.cjs.jsrename to dist/swetrix.cjs.jsindex cef6b448e..32e2bc07e 100644--- a/dist/analytics.cjs.js+++ b/dist/swetrix.cjs.js@@ -90,15 +90,18 @@ var Lib = /** @class */ (function () {if (this.pageData) {return this.pageData.actions;}- var interval = setInterval(this.trackPathChange, 2000);+ var interval;+ if (!(options === null || options === void 0 ? void 0 : options.unique)) {+ interval = setInterval(this.trackPathChange, 2000);+ }var path = getPath();this.pageData = {path: path,actions: {- stop: function () { return clearInterval(interval); }+ stop: (options === null || options === void 0 ? void 0 : options.unique) ? function () { } : function () { return clearInterval(interval); },},};- this.trackPage(path);+ this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);return this.pageData.actions;};// Tracking path changes. If path changes -> calling this.trackPage method@@ -108,10 +111,11 @@ var Lib = /** @class */ (function () {var newPath = getPath();var path = this.pageData.path;if (path !== newPath) {- this.trackPage(newPath);+ this.trackPage(newPath, false);}};- Lib.prototype.trackPage = function (pg) {+ Lib.prototype.trackPage = function (pg, unique) {+ if (unique === void 0) { unique = false; }if (!this.pageData)return;this.pageData.path = pg;@@ -123,6 +127,7 @@ var Lib = /** @class */ (function () {so: getUTMSource(),me: getUTMMedium(),ca: getUTMCampaign(),+ unique: unique,pg: pg,};this.submitData(data);diff --git a/dist/analytics.es5.js b/dist/swetrix.es5.jssimilarity index 89%rename from dist/analytics.es5.jsrename to dist/swetrix.es5.jsindex 2171c5e59..ca3769bf6 100644--- a/dist/analytics.es5.js+++ b/dist/swetrix.es5.js@@ -86,15 +86,18 @@ var Lib = /** @class */ (function () {if (this.pageData) {return this.pageData.actions;}- var interval = setInterval(this.trackPathChange, 2000);+ var interval;+ if (!(options === null || options === void 0 ? void 0 : options.unique)) {+ interval = setInterval(this.trackPathChange, 2000);+ }var path = getPath();this.pageData = {path: path,actions: {- stop: function () { return clearInterval(interval); }+ stop: (options === null || options === void 0 ? void 0 : options.unique) ? function () { } : function () { return clearInterval(interval); },},};- this.trackPage(path);+ this.trackPage(path, options === null || options === void 0 ? void 0 : options.unique);return this.pageData.actions;};// Tracking path changes. If path changes -> calling this.trackPage method@@ -104,10 +107,11 @@ var Lib = /** @class */ (function () {var newPath = getPath();var path = this.pageData.path;if (path !== newPath) {- this.trackPage(newPath);+ this.trackPage(newPath, false);}};- Lib.prototype.trackPage = function (pg) {+ Lib.prototype.trackPage = function (pg, unique) {+ if (unique === void 0) { unique = false; }if (!this.pageData)return;this.pageData.path = pg;@@ -119,6 +123,7 @@ var Lib = /** @class */ (function () {so: getUTMSource(),me: getUTMMedium(),ca: getUTMCampaign(),+ unique: unique,pg: pg,};this.submitData(data);diff --git a/dist/swetrix.js b/dist/swetrix.jsindex 812382160..1f292485f 100644--- a/dist/swetrix.js+++ b/dist/swetrix.js@@ -1 +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)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c="https://api.swetrix.com/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,2e3),n=e();return this.pageData={path:n,actions:{stop:function(){return clearInterval(o)}}},this.trackPage(n),this.pageData.actions}},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(s),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("[Swetrix]",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(c,{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)})},t.prototype.submitCustom=function(t){return fetch(c+"/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,this.trackPathChange=this.trackPathChange.bind(this)}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})});+!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).swetrix={})}(this,function(o){"use strict";function i(t){return(t=location.search.match(t))&&t[2]||void 0}function e(){return location.pathname||""}var n=function(){return(n=Object.assign||function(t){for(var n,o=1,i=arguments.length;o<i;o++)for(var e in n=arguments[o])Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e]);return t}).apply(this,arguments)},a=/[?&](ref|source|utm_source)=([^?&]+)/,r=/[?&](utm_campaign)=([^?&]+)/,s=/[?&](utm_medium)=([^?&]+)/,c="https://api.swetrix.com/log",u=(t.prototype.track=function(t){this.canTrack()&&(t=n({pid:this.projectID},t),this.submitCustom(t))},t.prototype.trackPageViews=function(t){if(this.canTrack()){if(this.pageData)return this.pageData.actions;var n;null!=t&&t.unique||(n=setInterval(this.trackPathChange,2e3));var o=e();return this.pageData={path:o,actions:{stop:null!=t&&t.unique?function(){}:function(){return clearInterval(n)}}},this.trackPage(o,null==t?void 0:t.unique),this.pageData.actions}},t.prototype.trackPathChange=function(){var t;this.pageData&&(t=e(),this.pageData.path!==t&&this.trackPage(t,!1))},t.prototype.trackPage=function(t,n){void 0===n&&(n=!1),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:i(a),me:i(s),ca:i(r),unique:n,pg:t},this.submitData(t))},t.prototype.debug=function(t){var n;null!==(n=this.options)&&void 0!==n&&n.debug&&console.log("[Swetrix]",t)},t.prototype.canTrack=function(){var t,n;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===(n=window.navigator)||void 0===n?void 0:n.doNotTrack)?(this.debug("Tracking disabled: respecting user's 'Do Not Track' preference."),!1):!!(null!==(n=this.options)&&void 0!==n&&n.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(c,{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)})},t.prototype.submitCustom=function(t){return fetch(c+"/custom",{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(t)})},t);function t(t,n){this.projectID=t,this.options=n,this.pageData=null,this.trackPathChange=this.trackPathChange.bind(this)}o.LIB_INSTANCE=null,o.init=function(t,n){return o.LIB_INSTANCE||(o.LIB_INSTANCE=new u(t,n)),o.LIB_INSTANCE},o.track=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.track(t)},o.trackViews=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.trackPageViews(t)},Object.defineProperty(o,"__esModule",{value:!0})});diff --git a/src/Lib.ts b/src/Lib.tsindex a2760fb7d..a2d568f2e 100644--- a/src/Lib.ts+++ b/src/Lib.ts@@ -3,13 +3,6 @@ import {getUTMCampaign, getUTMMedium, getUTMSource, getPath,} from './utils'-/**- * A map of key / value pairs.- */-interface Map<T> {- [key: string]: T-}-export interface LibOptions {// When set to `true`, all tracking logs will be// printed to console and localhost events will be sent to server.@@ -41,7 +34,9 @@ export interface PageData {}export interface PageViewsOptions {-+ // If true, only unique events will be saved+ // This param is useful when tracking single-page landing websites+ unique?: boolean}const host = 'https://api.swetrix.com/log'@@ -75,17 +70,21 @@ export class Lib {return this.pageData.actions}- const interval = setInterval(this.trackPathChange, 2000)+ let interval: NodeJS.Timeout+ if (!options?.unique) {+ interval = setInterval(this.trackPathChange, 2000)+ }+const path = getPath()this.pageData = {path,actions: {- stop: () => clearInterval(interval)+ stop: options?.unique ? () => { } : () => clearInterval(interval),},}- this.trackPage(path)+ this.trackPage(path, options?.unique)return this.pageData.actions}@@ -96,11 +95,11 @@ export class Lib {const { path } = this.pageDataif (path !== newPath) {- this.trackPage(newPath)+ this.trackPage(newPath, false)}}- private trackPage(pg: string) {+ private trackPage(pg: string, unique: boolean = false) {if (!this.pageData) returnthis.pageData.path = pg@@ -112,6 +111,7 @@ export class Lib {so: getUTMSource(),me: getUTMMedium(),ca: getUTMCampaign(),+ unique,pg,}