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!

(feature) Trackviews callback (#11)

Andrii Romasiun4a116c5d3b25
diff --git a/.prettierrc.js b/.prettierrc.js
new file mode 100644
index 000000000..bd6302ddd
--- /dev/null
+++ b/.prettierrc.js
@@ -0,0 +1,13 @@
+module.exports = {
+ printWidth: 120, // max 120 chars in line, code is easy to read
+ useTabs: false, // use spaces instead of tabs
+ tabWidth: 2, // "visual width" of of the "tab"
+ trailingComma: 'all', // add trailing commas in objects, arrays, etc.
+ semi: false, // Only add semicolons at the beginning of lines that may introduce ASI failures
+ singleQuote: true, // '' for stings instead of ""
+ bracketSpacing: true, // import { some } ... instead of import {some} ...
+ arrowParens: 'always', // braces even for single param in arrow functions (a) => { }
+ jsxSingleQuote: true, // '' for react props
+ bracketSameLine: false, // pretty JSX
+ endOfLine: 'lf', // 'lf' for linux, 'crlf' for windows, we need to use 'lf' for git
+}
diff --git a/dist/esnext/Lib.d.ts b/dist/esnext/Lib.d.ts
index 1bdd3a2fd..a5262c7cd 100644
--- a/dist/esnext/Lib.d.ts
+++ b/dist/esnext/Lib.d.ts
@@ -1,8 +1,8 @@
export interface LibOptions {
/**
- * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.
+ * When set to `true`, localhost events will be sent to server.
*/
- debug?: boolean;
+ devMode?: 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.
@@ -25,6 +25,26 @@ export interface TrackEventOptions {
[key: string]: string;
};
}
+export interface IPageViewPayload {
+ lc: string | undefined;
+ tz: string | undefined;
+ ref: string | undefined;
+ so: string | undefined;
+ me: string | undefined;
+ ca: string | undefined;
+ pg: string;
+ prev: string | null | undefined;
+}
+interface IPerfPayload {
+ dns: number;
+ tls: number;
+ conn: number;
+ response: number;
+ render: number;
+ dom_load: number;
+ page_load: number;
+ ttfb: number;
+}
/**
* The object returned by `trackPageViews()`, used to stop tracking pages.
*/
@@ -44,16 +64,8 @@ export interface PageViewsOptions {
* This param is useful when tracking single-page landing websites.
*/
unique?: boolean;
- /** A list of Regular Expressions or string pathes to ignore. */
- ignore?: Array<string | RegExp>;
- /** Do not send paths from ignore list to API. If set to `false`, the page view information will be sent to the Swetrix API, but the page will be displayed as a 'Redacted page' in the dashboard. */
- doNotAnonymise?: boolean;
- /** 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;
- /** Disable user-flow */
- noUserFlow?: boolean;
/**
* Set to `true` to enable hash-based routing.
* For example if you have pages like /#/path or want to track pages like /path#hash
@@ -64,6 +76,13 @@ export interface PageViewsOptions {
* For example if you have pages like /path?search
*/
search?: boolean;
+ /**
+ * Callback to edit / prevent sending pageviews.
+ *
+ * @param payload - The pageview payload.
+ * @returns The edited payload or `false` to prevent sending the pageview. If `true` is returned, the payload will be sent as-is.
+ */
+ callback?: (payload: IPageViewPayload) => IPageViewPayload | boolean;
}
export declare const defaultPageActions: {
stop(): void;
@@ -78,14 +97,13 @@ export declare class Lib {
constructor(projectID: string, options?: LibOptions | undefined);
track(event: TrackEventOptions): void;
trackPageViews(options?: PageViewsOptions): PageActions;
- getPerformanceStats(): object;
+ getPerformanceStats(): IPerfPayload | {};
private heartbeat;
- private checkIgnore;
private trackPathChange;
private getPreviousPage;
private trackPage;
- submitPageView(pg: null | string, prev: string | null | undefined, unique: boolean, perf: any): void;
- private debug;
+ submitPageView(pg: string, prev: string | null | undefined, unique: boolean, perf: IPerfPayload | {}, evokeCallback?: boolean): void;
private canTrack;
private sendRequest;
}
+export {};
diff --git a/dist/esnext/Lib.js b/dist/esnext/Lib.js
index 2efa6ab32..3c730af0b 100644
--- a/dist/esnext/Lib.js
+++ b/dist/esnext/Lib.js
@@ -19,6 +19,7 @@ export class Lib {
return;
}
const data = {
+ ...event,
pid: this.projectID,
pg: this.activePage,
lc: getLocale(),
@@ -27,7 +28,6 @@ export class Lib {
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- ...event,
};
this.sendRequest('custom', data);
}
@@ -39,14 +39,12 @@ export class Lib {
return this.pageData.actions;
}
this.pageViewsOptions = options;
- let hbInterval, interval;
+ let interval;
if (!options?.unique) {
interval = setInterval(this.trackPathChange, 2000);
}
- if (!options?.noHeartbeat) {
- setTimeout(this.heartbeat, 3000);
- hbInterval = setInterval(this.heartbeat, 28000);
- }
+ setTimeout(this.heartbeat, 3000);
+ const hbInterval = setInterval(this.heartbeat, 28000);
const path = getPath({
hash: options?.hash,
search: options?.search,
@@ -74,23 +72,17 @@ export class Lib {
this.perfStatsCollected = true;
return {
// Network
- // @ts-ignore
- dns: perf.domainLookupEnd - perf.domainLookupStart,
- // @ts-ignore
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
- // @ts-ignore
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
- // @ts-ignore
- response: perf.responseEnd - perf.responseStart,
+ dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
+ tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
+ conn: perf.secureConnectionStart
+ ? perf.secureConnectionStart - perf.connectStart
+ : perf.connectEnd - perf.connectStart, // Connection time
+ response: perf.responseEnd - perf.responseStart, // Response Time (Download)
// Frontend
- // @ts-ignore
- render: perf.domComplete - perf.domContentLoadedEventEnd,
- // @ts-ignore
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
- // @ts-ignore
- page_load: perf.loadEventStart,
+ render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
+ dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
+ page_load: perf.loadEventStart, // Page load time
// Backend
- // @ts-ignore
ttfb: perf.responseStart - perf.requestStart,
};
}
@@ -103,19 +95,6 @@ export class Lib {
};
this.sendRequest('hb', data);
}
- checkIgnore(path) {
- const ignore = this.pageViewsOptions?.ignore;
- if (Array.isArray(ignore)) {
- 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;
- }
- }
- return false;
- }
// Tracking path changes. If path changes -> calling this.trackPage method
trackPathChange() {
if (!this.pageData)
@@ -133,11 +112,7 @@ export class Lib {
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
// That method of getting previous page works for SPA websites
if (this.activePage) {
- const shouldIgnore = this.checkIgnore(this.activePage);
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
- return null;
- }
- return shouldIgnore ? null : this.activePage;
+ return this.activePage;
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
if (typeof URL === 'function') {
@@ -153,11 +128,7 @@ export class Lib {
if (host !== refHost) {
return null;
}
- const shouldIgnore = this.checkIgnore(pathname);
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
- return null;
- }
- return shouldIgnore ? null : pathname;
+ return pathname;
}
catch {
return null;
@@ -169,56 +140,45 @@ export class Lib {
if (!this.pageData)
return;
this.pageData.path = pg;
- const shouldIgnore = this.checkIgnore(pg);
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise)
- return;
const perf = this.getPerformanceStats();
- let prev;
- if (!this.pageViewsOptions?.noUserFlow) {
- prev = this.getPreviousPage();
- }
+ const prev = this.getPreviousPage();
this.activePage = pg;
- this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
+ this.submitPageView(pg, prev, unique, perf, true);
}
- submitPageView(pg, prev, unique, perf) {
- const data = {
+ submitPageView(pg, prev, unique, perf, evokeCallback) {
+ const privateData = {
pid: this.projectID,
+ perf,
+ unique,
+ };
+ const pvPayload = {
lc: getLocale(),
tz: getTimezone(),
ref: getReferrer(),
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- unique,
pg,
- perf,
prev,
};
- this.sendRequest('', data);
- }
- debug(message) {
- if (this.options?.debug) {
- console.log('[Swetrix]', message);
+ if (evokeCallback && this.pageViewsOptions?.callback) {
+ const callbackResult = this.pageViewsOptions.callback(pvPayload);
+ if (callbackResult === false) {
+ return;
+ }
+ if (callbackResult && typeof callbackResult === 'object') {
+ Object.assign(pvPayload, callbackResult);
+ }
}
+ Object.assign(pvPayload, privateData);
+ this.sendRequest('', pvPayload);
}
canTrack() {
- if (this.options?.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 (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.');
+ if (this.options?.disabled ||
+ !isInBrowser() ||
+ (this.options?.respectDNT && window.navigator?.doNotTrack === '1') ||
+ (!this.options?.devMode && isLocalhost()) ||
+ isAutomated()) {
return false;
}
return true;
diff --git a/dist/esnext/Lib.js.map b/dist/esnext/Lib.js.map
index bfe9f3478..2722a1888 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;AAuFhB,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,IAAI,KAAI,CAAC;CACV,CAAA;AAED,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AAEtD,MAAM,OAAO,GAAG;IAM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ist/esnext/index.js
@@ -1,4 +1,4 @@
-import { Lib, defaultPageActions, } 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).
diff --git a/dist/esnext/index.js.map b/dist/esnext/index.js.map
index 0aaf98007..8b4d999d4 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,EACL,GAAG,EAAgE,kBAAkB,GACtF,MAAM,OAAO,CAAA;AAEd,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAC,GAAW,EAAE,OAAoB;IACpD,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,CAAC,KAAwB;IAC5C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,OAA0B;IACnD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO,CAAC,kBAAkB,CAAC,CAAA;YAC3B,OAAM;SACP;QAED,8GAA8G;QAC9G,IAAI,OAAO,QAAQ,KAAK,WAAW,IAAI,QAAQ,CAAC,UAAU,KAAK,UAAU,EAAE;YACzE,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;SAC9C;aAAM;YACL,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,aAAa;gB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AAC;YAC1E,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;QAC/C,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;gBACnC,aAAa;gBACb,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAA;YAC/C,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,IAAa,EAAE,MAAgB;IACzE,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAA;AACtE,CAAC"}
\ No newline at end of file
diff --git a/dist/esnext/utils.js.map b/dist/esnext/utils.js.map
index ea5ff4542..06cb677a9 100644
--- a/dist/esnext/utils.js.map
+++ b/dist/esnext/utils.js.map
@@ -1 +1 @@
-{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAKA,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;IAC9B,OAAO,OAAO,MAAM,KAAK,WAAW,CAAA;AACtC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAA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C,IAAI,EAAE,CAAC;QACjB,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC5C,MAAM,UAAU,GAAG,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAA;QACzF,MAAM,IAAI,UAAU,CAAA;IACtB,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC5C,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QAClG,MAAM,IAAI,YAAY,CAAA;IACxB,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
\ No newline at end of file
diff --git a/dist/swetrix.cjs.js b/dist/swetrix.cjs.js
index d8fe3bb6c..4dd29b721 100644
--- a/dist/swetrix.cjs.js
+++ b/dist/swetrix.cjs.js
@@ -114,7 +114,7 @@ var Lib = /** @class */ (function () {
if (!this.canTrack()) {
return;
}
- var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
+ var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() });
this.sendRequest('custom', data);
};
Lib.prototype.trackPageViews = function (options) {
@@ -125,14 +125,12 @@ var Lib = /** @class */ (function () {
return this.pageData.actions;
}
this.pageViewsOptions = options;
- var hbInterval, interval;
+ var 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);
- }
+ setTimeout(this.heartbeat, 3000);
+ var hbInterval = setInterval(this.heartbeat, 28000);
var path = getPath({
hash: options === null || options === void 0 ? void 0 : options.hash,
search: options === null || options === void 0 ? void 0 : options.search,
@@ -161,23 +159,17 @@ var Lib = /** @class */ (function () {
this.perfStatsCollected = true;
return {
// Network
- // @ts-ignore
- dns: perf.domainLookupEnd - perf.domainLookupStart,
- // @ts-ignore
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
- // @ts-ignore
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
- // @ts-ignore
- response: perf.responseEnd - perf.responseStart,
+ dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
+ tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
+ conn: perf.secureConnectionStart
+ ? perf.secureConnectionStart - perf.connectStart
+ : perf.connectEnd - perf.connectStart, // Connection time
+ response: perf.responseEnd - perf.responseStart, // Response Time (Download)
// Frontend
- // @ts-ignore
- render: perf.domComplete - perf.domContentLoadedEventEnd,
- // @ts-ignore
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
- // @ts-ignore
- page_load: perf.loadEventStart,
+ render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
+ dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
+ page_load: perf.loadEventStart, // Page load time
// Backend
- // @ts-ignore
ttfb: perf.responseStart - perf.requestStart,
};
};
@@ -191,20 +183,6 @@ var Lib = /** @class */ (function () {
};
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 () {
var _a, _b;
@@ -220,15 +198,10 @@ var Lib = /** @class */ (function () {
}
};
Lib.prototype.getPreviousPage = function () {
- var _a, _b;
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
// That method of getting previous page works for SPA websites
if (this.activePage) {
- var shouldIgnore = this.checkIgnore(this.activePage);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : this.activePage;
+ return this.activePage;
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
if (typeof URL === 'function') {
@@ -244,76 +217,60 @@ var Lib = /** @class */ (function () {
if (host !== refHost) {
return null;
}
- var shouldIgnore = this.checkIgnore(pathname);
- if (shouldIgnore && ((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : pathname;
+ return pathname;
}
- catch (_c) {
+ catch (_a) {
return null;
}
}
return null;
};
Lib.prototype.trackPage = function (pg, unique) {
- var _a, _b;
if (unique === void 0) { unique = false; }
if (!this.pageData)
return;
this.pageData.path = pg;
- var shouldIgnore = this.checkIgnore(pg);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise))
- return;
var perf = this.getPerformanceStats();
- var prev;
- if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
- prev = this.getPreviousPage();
- }
+ var prev = this.getPreviousPage();
this.activePage = pg;
- this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
+ this.submitPageView(pg, prev, unique, perf, true);
};
- Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
- var data = {
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf, evokeCallback) {
+ var _a;
+ var privateData = {
pid: this.projectID,
+ perf: perf,
+ unique: unique,
+ };
+ var pvPayload = {
lc: getLocale(),
tz: getTimezone(),
ref: getReferrer(),
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- unique: unique,
pg: pg,
- perf: perf,
prev: prev,
};
- 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);
+ if (evokeCallback && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.callback)) {
+ var callbackResult = this.pageViewsOptions.callback(pvPayload);
+ if (callbackResult === false) {
+ return;
+ }
+ if (callbackResult && typeof callbackResult === 'object') {
+ Object.assign(pvPayload, callbackResult);
+ }
}
+ Object.assign(pvPayload, privateData);
+ this.sendRequest('', pvPayload);
};
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.');
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) ||
+ !isInBrowser() ||
+ (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') ||
+ (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.devMode) && isLocalhost()) ||
+ isAutomated()) {
return false;
}
return true;
diff --git a/dist/swetrix.es5.js b/dist/swetrix.es5.js
index 0e9bc1ea6..1c507f7aa 100644
--- a/dist/swetrix.es5.js
+++ b/dist/swetrix.es5.js
@@ -110,7 +110,7 @@ var Lib = /** @class */ (function () {
if (!this.canTrack()) {
return;
}
- var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
+ var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() });
this.sendRequest('custom', data);
};
Lib.prototype.trackPageViews = function (options) {
@@ -121,14 +121,12 @@ var Lib = /** @class */ (function () {
return this.pageData.actions;
}
this.pageViewsOptions = options;
- var hbInterval, interval;
+ var 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);
- }
+ setTimeout(this.heartbeat, 3000);
+ var hbInterval = setInterval(this.heartbeat, 28000);
var path = getPath({
hash: options === null || options === void 0 ? void 0 : options.hash,
search: options === null || options === void 0 ? void 0 : options.search,
@@ -157,23 +155,17 @@ var Lib = /** @class */ (function () {
this.perfStatsCollected = true;
return {
// Network
- // @ts-ignore
- dns: perf.domainLookupEnd - perf.domainLookupStart,
- // @ts-ignore
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
- // @ts-ignore
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
- // @ts-ignore
- response: perf.responseEnd - perf.responseStart,
+ dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
+ tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
+ conn: perf.secureConnectionStart
+ ? perf.secureConnectionStart - perf.connectStart
+ : perf.connectEnd - perf.connectStart, // Connection time
+ response: perf.responseEnd - perf.responseStart, // Response Time (Download)
// Frontend
- // @ts-ignore
- render: perf.domComplete - perf.domContentLoadedEventEnd,
- // @ts-ignore
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
- // @ts-ignore
- page_load: perf.loadEventStart,
+ render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
+ dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
+ page_load: perf.loadEventStart, // Page load time
// Backend
- // @ts-ignore
ttfb: perf.responseStart - perf.requestStart,
};
};
@@ -187,20 +179,6 @@ var Lib = /** @class */ (function () {
};
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 () {
var _a, _b;
@@ -216,15 +194,10 @@ var Lib = /** @class */ (function () {
}
};
Lib.prototype.getPreviousPage = function () {
- var _a, _b;
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
// That method of getting previous page works for SPA websites
if (this.activePage) {
- var shouldIgnore = this.checkIgnore(this.activePage);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : this.activePage;
+ return this.activePage;
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
if (typeof URL === 'function') {
@@ -240,76 +213,60 @@ var Lib = /** @class */ (function () {
if (host !== refHost) {
return null;
}
- var shouldIgnore = this.checkIgnore(pathname);
- if (shouldIgnore && ((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : pathname;
+ return pathname;
}
- catch (_c) {
+ catch (_a) {
return null;
}
}
return null;
};
Lib.prototype.trackPage = function (pg, unique) {
- var _a, _b;
if (unique === void 0) { unique = false; }
if (!this.pageData)
return;
this.pageData.path = pg;
- var shouldIgnore = this.checkIgnore(pg);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise))
- return;
var perf = this.getPerformanceStats();
- var prev;
- if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
- prev = this.getPreviousPage();
- }
+ var prev = this.getPreviousPage();
this.activePage = pg;
- this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
+ this.submitPageView(pg, prev, unique, perf, true);
};
- Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
- var data = {
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf, evokeCallback) {
+ var _a;
+ var privateData = {
pid: this.projectID,
+ perf: perf,
+ unique: unique,
+ };
+ var pvPayload = {
lc: getLocale(),
tz: getTimezone(),
ref: getReferrer(),
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- unique: unique,
pg: pg,
- perf: perf,
prev: prev,
};
- 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);
+ if (evokeCallback && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.callback)) {
+ var callbackResult = this.pageViewsOptions.callback(pvPayload);
+ if (callbackResult === false) {
+ return;
+ }
+ if (callbackResult && typeof callbackResult === 'object') {
+ Object.assign(pvPayload, callbackResult);
+ }
}
+ Object.assign(pvPayload, privateData);
+ this.sendRequest('', pvPayload);
};
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.');
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) ||
+ !isInBrowser() ||
+ (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') ||
+ (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.devMode) && isLocalhost()) ||
+ isAutomated()) {
return false;
}
return true;
diff --git a/dist/swetrix.js b/dist/swetrix.js
index c811f736c..d969bfd08 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(o){"use strict";function t(t){return(t=location.search.match(t))&&t[2]||void 0}function i(){return void 0!==navigator.languages?navigator.languages[0]:navigator.language}function a(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}function s(){return document.referrer||void 0}function r(){return t(n)}function c(){return t(p)}function u(){return t(h)}function l(t){var e,n=location.pathname||"";return t.hash&&(n+=-1<(e=location.hash.indexOf("?"))?location.hash.substring(0,e):location.hash),t.search&&(e=location.hash.indexOf("?"),n+=location.search||(-1<e?location.hash.substring(e):"")),n}var e=function(){return(e=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},n=/[?&](ref|source|utm_source)=([^?&]+)/,h=/[?&](utm_campaign)=([^?&]+)/,p=/[?&](utm_medium)=([^?&]+)/,d={stop:function(){}},g=(f.prototype.track=function(t){this.canTrack()&&(t=e({pid:this.projectID,pg:this.activePage,lc:i(),tz:a(),ref:s(),so:r(),me:c(),ca:u()},t),this.sendRequest("custom",t))},f.prototype.trackPageViews=function(t){var e,n,o;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)),o=l({hash:null==t?void 0:t.hash,search:null==t?void 0:t.search}),this.pageData={path:o,actions:{stop:function(){clearInterval(n),clearInterval(e)}}},this.trackPage(o,null==t?void 0:t.unique)),this.pageData.actions):d},f.prototype.getPerformanceStats=function(){var t;return this.canTrack()&&!this.perfStatsCollected&&null!=(t=window.performance)&&t.getEntriesByType&&(t=window.performance.getEntriesByType("navigation")[0])?(this.perfStatsCollected=!0,{dns:t.domainLookupEnd-t.domainLookupStart,tls:t.secureConnectionStart?t.requestStart-t.secureConnectionStart:0,conn:t.secureConnectionStart?t.secureConnectionStart-t.connectStart:t.connectEnd-t.connectStart,response:t.responseEnd-t.responseStart,render:t.domComplete-t.domContentLoadedEventEnd,dom_load:t.domContentLoadedEventEnd-t.responseEnd,page_load:t.loadEventStart,ttfb:t.responseStart-t.requestStart}):{}},f.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},f.prototype.checkIgnore=function(t){var e,n=null==(e=this.pageViewsOptions)?void 0:e.ignore;if(Array.isArray(n))for(var o=0;o<n.length;++o){if(n[o]===t)return!0;if(n[o]instanceof RegExp&&n[o].test(t))return!0}return!1},f.prototype.trackPathChange=function(){var t;this.pageData&&(t=l({hash:null==(t=this.pageViewsOptions)?void 0:t.hash,search:null==(t=this.pageViewsOptions)?void 0:t.search}),this.pageData.path!==t)&&this.trackPage(t,!1)},f.prototype.getPreviousPage=function(){var t;if(this.activePage)return(o=this.checkIgnore(this.activePage))&&null!=(e=this.pageViewsOptions)&&e.doNotAnonymise||o?null:this.activePage;if("function"==typeof URL){var e=s();if(!e)return null;var n=location.host;try{var o,i=new URL(e),a=i.host,r=i.pathname;return n!==a?null:(o=this.checkIgnore(r))&&null!=(t=this.pageViewsOptions)&&t.doNotAnonymise||o?null:r}catch(t){}}return null},f.prototype.trackPage=function(t,e){var n,o,i,a;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,(o=this.checkIgnore(t))&&null!=(i=this.pageViewsOptions)&&i.doNotAnonymise||(i=this.getPerformanceStats(),null!=(n=this.pageViewsOptions)&&n.noUserFlow||(a=this.getPreviousPage()),this.activePage=t,this.submitPageView(o?null:t,a,e,i)))},f.prototype.submitPageView=function(t,e,n,o){n={pid:this.projectID,lc:i(),tz:a(),ref:s(),so:r(),me:c(),ca:u(),unique:n,pg:t,perf:o,prev:e};this.sendRequest("",n)},f.prototype.debug=function(t){var e;null!=(e=this.options)&&e.debug&&console.log("[Swetrix]",t)},f.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))},f.prototype.sendRequest=function(t,e){var n=(null==(n=this.options)?void 0:n.apiURL)||"https://api.swetrix.com/log",o=new XMLHttpRequest;o.open("POST","".concat(n,"/").concat(t),!0),o.setRequestHeader("Content-Type","application/json"),o.send(JSON.stringify(e))},f);function f(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}o.LIB_INSTANCE=null,o.init=function(t,e){return o.LIB_INSTANCE||(o.LIB_INSTANCE=new g(t,e)),o.LIB_INSTANCE},o.track=function(t){o.LIB_INSTANCE&&o.LIB_INSTANCE.track(t)},o.trackPageview=function(t,e,n){o.LIB_INSTANCE&&o.LIB_INSTANCE.submitPageView(t,e||null,Boolean(n),{})},o.trackViews=function(e){return new Promise(function(t){o.LIB_INSTANCE?"undefined"==typeof document||"complete"===document.readyState?t(o.LIB_INSTANCE.trackPageViews(e)):window.addEventListener("load",function(){t(o.LIB_INSTANCE.trackPageViews(e))}):t(d)})},Object.defineProperty(o,"__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(a){"use strict";function t(t){return(t=location.search.match(t))&&t[2]||void 0}function i(){return void 0!==navigator.languages?navigator.languages[0]:navigator.language}function r(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch(t){}}function s(){return document.referrer||void 0}function c(){return t(n)}function u(){return t(p)}function l(){return t(h)}function o(t){var e,n=location.pathname||"";return t.hash&&(n+=-1<(e=location.hash.indexOf("?"))?location.hash.substring(0,e):location.hash),t.search&&(e=location.hash.indexOf("?"),n+=location.search||(-1<e?location.hash.substring(e):"")),n}var e=function(){return(e=Object.assign||function(t){for(var e,n=1,a=arguments.length;n<a;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},n=/[?&](ref|source|utm_source)=([^?&]+)/,h=/[?&](utm_campaign)=([^?&]+)/,p=/[?&](utm_medium)=([^?&]+)/,d={stop:function(){}},f=(g.prototype.track=function(t){this.canTrack()&&(t=e(e({},t),{pid:this.projectID,pg:this.activePage,lc:i(),tz:r(),ref:s(),so:c(),me:u(),ca:l()}),this.sendRequest("custom",t))},g.prototype.trackPageViews=function(t){var e,n,a;return this.canTrack()?(this.pageData||(null!=(this.pageViewsOptions=t)&&t.unique||(e=setInterval(this.trackPathChange,2e3)),setTimeout(this.heartbeat,3e3),n=setInterval(this.heartbeat,28e3),a=o({hash:null==t?void 0:t.hash,search:null==t?void 0:t.search}),this.pageData={path:a,actions:{stop:function(){clearInterval(e),clearInterval(n)}}},this.trackPage(a,null==t?void 0:t.unique)),this.pageData.actions):d},g.prototype.getPerformanceStats=function(){var t;return this.canTrack()&&!this.perfStatsCollected&&null!=(t=window.performance)&&t.getEntriesByType&&(t=window.performance.getEntriesByType("navigation")[0])?(this.perfStatsCollected=!0,{dns:t.domainLookupEnd-t.domainLookupStart,tls:t.secureConnectionStart?t.requestStart-t.secureConnectionStart:0,conn:t.secureConnectionStart?t.secureConnectionStart-t.connectStart:t.connectEnd-t.connectStart,response:t.responseEnd-t.responseStart,render:t.domComplete-t.domContentLoadedEventEnd,dom_load:t.domContentLoadedEventEnd-t.responseEnd,page_load:t.loadEventStart,ttfb:t.responseStart-t.requestStart}):{}},g.prototype.heartbeat=function(){var t;(null!=(t=this.pageViewsOptions)&&t.heartbeatOnBackground||"hidden"!==document.visibilityState)&&(t={pid:this.projectID},this.sendRequest("hb",t))},g.prototype.trackPathChange=function(){var t;this.pageData&&(t=o({hash:null==(t=this.pageViewsOptions)?void 0:t.hash,search:null==(t=this.pageViewsOptions)?void 0:t.search}),this.pageData.path!==t)&&this.trackPage(t,!1)},g.prototype.getPreviousPage=function(){if(this.activePage)return this.activePage;if("function"==typeof URL){var t=s();if(!t)return null;var e=location.host;try{var n=new URL(t),a=n.host,o=n.pathname;return e!==a?null:o}catch(t){}}return null},g.prototype.trackPage=function(t,e){var n,a;void 0===e&&(e=!1),this.pageData&&(this.pageData.path=t,n=this.getPerformanceStats(),a=this.getPreviousPage(),this.activePage=t,this.submitPageView(t,a,e,n,!0))},g.prototype.submitPageView=function(t,e,n,a,o){a={pid:this.projectID,perf:a,unique:n},n={lc:i(),tz:r(),ref:s(),so:c(),me:u(),ca:l(),pg:t,prev:e};if(o&&null!=(t=this.pageViewsOptions)&&t.callback){e=this.pageViewsOptions.callback(n);if(!1===e)return;e&&"object"==typeof e&&Object.assign(n,e)}Object.assign(n,a),this.sendRequest("",n)},g.prototype.canTrack=function(){var t;return!(null!=(t=this.options)&&t.disabled||"undefined"==typeof window||null!=(t=this.options)&&t.respectDNT&&"1"===(null==(t=window.navigator)?void 0:t.doNotTrack)||(null==(t=this.options)||!t.devMode)&&("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)},g.prototype.sendRequest=function(t,e){var n=(null==(n=this.options)?void 0:n.apiURL)||"https://api.swetrix.com/log",a=new XMLHttpRequest;a.open("POST","".concat(n,"/").concat(t),!0),a.setRequestHeader("Content-Type","application/json"),a.send(JSON.stringify(e))},g);function g(t,e){this.projectID=t,this.options=e,this.pageData=null,this.pageViewsOptions=null,this.perfStatsCollected=!1,this.activePage=null,this.trackPathChange=this.trackPathChange.bind(this),this.heartbeat=this.heartbeat.bind(this)}a.LIB_INSTANCE=null,a.init=function(t,e){return a.LIB_INSTANCE||(a.LIB_INSTANCE=new f(t,e)),a.LIB_INSTANCE},a.track=function(t){a.LIB_INSTANCE&&a.LIB_INSTANCE.track(t)},a.trackPageview=function(t,e,n){a.LIB_INSTANCE&&a.LIB_INSTANCE.submitPageView(t,e||null,Boolean(n),{})},a.trackViews=function(e){return new Promise(function(t){a.LIB_INSTANCE?"undefined"==typeof document||"complete"===document.readyState?t(a.LIB_INSTANCE.trackPageViews(e)):window.addEventListener("load",function(){t(a.LIB_INSTANCE.trackPageViews(e))}):t(d)})},Object.defineProperty(a,"__esModule",{value:!0})});
diff --git a/dist/swetrix.orig.js b/dist/swetrix.orig.js
index ee952e628..4837f3129 100644
--- a/dist/swetrix.orig.js
+++ b/dist/swetrix.orig.js
@@ -116,7 +116,7 @@
if (!this.canTrack()) {
return;
}
- var data = __assign({ pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() }, event);
+ var data = __assign(__assign({}, event), { pid: this.projectID, pg: this.activePage, lc: getLocale(), tz: getTimezone(), ref: getReferrer(), so: getUTMSource(), me: getUTMMedium(), ca: getUTMCampaign() });
this.sendRequest('custom', data);
};
Lib.prototype.trackPageViews = function (options) {
@@ -127,14 +127,12 @@
return this.pageData.actions;
}
this.pageViewsOptions = options;
- var hbInterval, interval;
+ var 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);
- }
+ setTimeout(this.heartbeat, 3000);
+ var hbInterval = setInterval(this.heartbeat, 28000);
var path = getPath({
hash: options === null || options === void 0 ? void 0 : options.hash,
search: options === null || options === void 0 ? void 0 : options.search,
@@ -163,23 +161,17 @@
this.perfStatsCollected = true;
return {
// Network
- // @ts-ignore
- dns: perf.domainLookupEnd - perf.domainLookupStart,
- // @ts-ignore
- tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0,
- // @ts-ignore
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart,
- // @ts-ignore
- response: perf.responseEnd - perf.responseStart,
+ dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
+ tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
+ conn: perf.secureConnectionStart
+ ? perf.secureConnectionStart - perf.connectStart
+ : perf.connectEnd - perf.connectStart, // Connection time
+ response: perf.responseEnd - perf.responseStart, // Response Time (Download)
// Frontend
- // @ts-ignore
- render: perf.domComplete - perf.domContentLoadedEventEnd,
- // @ts-ignore
- dom_load: perf.domContentLoadedEventEnd - perf.responseEnd,
- // @ts-ignore
- page_load: perf.loadEventStart,
+ render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
+ dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
+ page_load: perf.loadEventStart, // Page load time
// Backend
- // @ts-ignore
ttfb: perf.responseStart - perf.requestStart,
};
};
@@ -193,20 +185,6 @@
};
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 () {
var _a, _b;
@@ -222,15 +200,10 @@
}
};
Lib.prototype.getPreviousPage = function () {
- var _a, _b;
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
// That method of getting previous page works for SPA websites
if (this.activePage) {
- var shouldIgnore = this.checkIgnore(this.activePage);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : this.activePage;
+ return this.activePage;
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
if (typeof URL === 'function') {
@@ -246,76 +219,60 @@
if (host !== refHost) {
return null;
}
- var shouldIgnore = this.checkIgnore(pathname);
- if (shouldIgnore && ((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.doNotAnonymise)) {
- return null;
- }
- return shouldIgnore ? null : pathname;
+ return pathname;
}
- catch (_c) {
+ catch (_a) {
return null;
}
}
return null;
};
Lib.prototype.trackPage = function (pg, unique) {
- var _a, _b;
if (unique === void 0) { unique = false; }
if (!this.pageData)
return;
this.pageData.path = pg;
- var shouldIgnore = this.checkIgnore(pg);
- if (shouldIgnore && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.doNotAnonymise))
- return;
var perf = this.getPerformanceStats();
- var prev;
- if (!((_b = this.pageViewsOptions) === null || _b === void 0 ? void 0 : _b.noUserFlow)) {
- prev = this.getPreviousPage();
- }
+ var prev = this.getPreviousPage();
this.activePage = pg;
- this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf);
+ this.submitPageView(pg, prev, unique, perf, true);
};
- Lib.prototype.submitPageView = function (pg, prev, unique, perf) {
- var data = {
+ Lib.prototype.submitPageView = function (pg, prev, unique, perf, evokeCallback) {
+ var _a;
+ var privateData = {
pid: this.projectID,
+ perf: perf,
+ unique: unique,
+ };
+ var pvPayload = {
lc: getLocale(),
tz: getTimezone(),
ref: getReferrer(),
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- unique: unique,
pg: pg,
- perf: perf,
prev: prev,
};
- 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);
+ if (evokeCallback && ((_a = this.pageViewsOptions) === null || _a === void 0 ? void 0 : _a.callback)) {
+ var callbackResult = this.pageViewsOptions.callback(pvPayload);
+ if (callbackResult === false) {
+ return;
+ }
+ if (callbackResult && typeof callbackResult === 'object') {
+ Object.assign(pvPayload, callbackResult);
+ }
}
+ Object.assign(pvPayload, privateData);
+ this.sendRequest('', pvPayload);
};
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.');
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.disabled) ||
+ !isInBrowser() ||
+ (((_b = this.options) === null || _b === void 0 ? void 0 : _b.respectDNT) && ((_c = window.navigator) === null || _c === void 0 ? void 0 : _c.doNotTrack) === '1') ||
+ (!((_d = this.options) === null || _d === void 0 ? void 0 : _d.devMode) && isLocalhost()) ||
+ isAutomated()) {
return false;
}
return true;
diff --git a/package.json b/package.json
index d2657f153..fbfe4e150 100644
--- a/package.json
+++ b/package.json
@@ -34,11 +34,11 @@
},
"homepage": "https://swetrix.com/docs",
"dependencies": {
- "@types/node": "^20.8.6",
+ "@types/node": "^20.11.8",
"tslib": "^2.6.2"
},
"devDependencies": {
- "@rollup/plugin-commonjs": "^25.0.5",
+ "@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"rimraf": "^4.4.1",
"rollup": "^2.79.1",
@@ -46,6 +46,6 @@
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-typescript2": "^0.36.0",
"rollup-plugin-uglify": "^6.0.4",
- "typescript": "^5.2.2"
+ "typescript": "^5.3.3"
}
}
diff --git a/src/Lib.ts b/src/Lib.ts
index 2c703a2f2..23c041219 100644
--- a/src/Lib.ts
+++ b/src/Lib.ts
@@ -1,13 +1,21 @@
import {
- isInBrowser, isLocalhost, isAutomated, getLocale, getTimezone, getReferrer,
- getUTMCampaign, getUTMMedium, getUTMSource, getPath,
+ isInBrowser,
+ isLocalhost,
+ isAutomated,
+ getLocale,
+ getTimezone,
+ getReferrer,
+ getUTMCampaign,
+ getUTMMedium,
+ getUTMSource,
+ getPath,
} from './utils'
export interface LibOptions {
/**
- * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.
+ * When set to `true`, localhost events will be sent to server.
*/
- debug?: boolean
+ devMode?: boolean
/**
* When set to `true`, the tracking library won't send any data to server.
@@ -37,6 +45,29 @@ export interface TrackEventOptions {
}
}
+// Partial user-editable pageview payload
+export interface IPageViewPayload {
+ lc: string | undefined
+ tz: string | undefined
+ ref: string | undefined
+ so: string | undefined
+ me: string | undefined
+ ca: string | undefined
+ pg: string
+ prev: string | null | undefined
+}
+
+interface IPerfPayload {
+ dns: number
+ tls: number
+ conn: number
+ response: number
+ render: number
+ dom_load: number
+ page_load: number
+ ttfb: number
+}
+
/**
* The object returned by `trackPageViews()`, used to stop tracking pages.
*/
@@ -60,21 +91,9 @@ export interface PageViewsOptions {
*/
unique?: boolean
- /** A list of Regular Expressions or string pathes to ignore. */
- ignore?: Array<string | RegExp>
-
- /** Do not send paths from ignore list to API. If set to `false`, the page view information will be sent to the Swetrix API, but the page will be displayed as a 'Redacted page' in the dashboard. */
- doNotAnonymise?: boolean
-
- /** 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
- /** Disable user-flow */
- noUserFlow?: boolean
-
/**
* Set to `true` to enable hash-based routing.
* For example if you have pages like /#/path or want to track pages like /path#hash
@@ -86,6 +105,14 @@ export interface PageViewsOptions {
* For example if you have pages like /path?search
*/
search?: boolean
+
+ /**
+ * Callback to edit / prevent sending pageviews.
+ *
+ * @param payload - The pageview payload.
+ * @returns The edited payload or `false` to prevent sending the pageview. If `true` is returned, the payload will be sent as-is.
+ */
+ callback?: (payload: IPageViewPayload) => IPageViewPayload | boolean
}
export const defaultPageActions = {
@@ -111,6 +138,7 @@ export class Lib {
}
const data = {
+ ...event,
pid: this.projectID,
pg: this.activePage,
lc: getLocale(),
@@ -119,7 +147,6 @@ export class Lib {
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- ...event,
}
this.sendRequest('custom', data)
}
@@ -134,15 +161,14 @@ export class Lib {
}
this.pageViewsOptions = options
- let hbInterval: NodeJS.Timeout, interval: NodeJS.Timeout
+ let interval: NodeJS.Timeout
+
if (!options?.unique) {
interval = setInterval(this.trackPathChange, 2000)
}
- if (!options?.noHeartbeat) {
- setTimeout(this.heartbeat, 3000)
- hbInterval = setInterval(this.heartbeat, 28000)
- }
+ setTimeout(this.heartbeat, 3000)
+ const hbInterval = setInterval(this.heartbeat, 28000)
const path = getPath({
hash: options?.hash,
@@ -163,12 +189,12 @@ export class Lib {
return this.pageData.actions
}
- getPerformanceStats(): object {
+ getPerformanceStats(): IPerfPayload | {} {
if (!this.canTrack() || this.perfStatsCollected || !window.performance?.getEntriesByType) {
return {}
}
- const perf = window.performance.getEntriesByType('navigation')[0]
+ const perf = window.performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming
if (!perf) {
return {}
@@ -178,25 +204,19 @@ export class Lib {
return {
// Network
- // @ts-ignore
dns: perf.domainLookupEnd - perf.domainLookupStart, // DNS Resolution
- // @ts-ignore
tls: perf.secureConnectionStart ? perf.requestStart - perf.secureConnectionStart : 0, // TLS Setup; checking if secureConnectionStart is not 0 (it's 0 for non-https websites)
- // @ts-ignore
- conn: perf.secureConnectionStart ? perf.secureConnectionStart - perf.connectStart : perf.connectEnd - perf.connectStart, // Connection time
- // @ts-ignore
+ conn: perf.secureConnectionStart
+ ? perf.secureConnectionStart - perf.connectStart
+ : perf.connectEnd - perf.connectStart, // Connection time
response: perf.responseEnd - perf.responseStart, // Response Time (Download)
// Frontend
- // @ts-ignore
render: perf.domComplete - perf.domContentLoadedEventEnd, // Browser rendering the HTML time
- // @ts-ignore
dom_load: perf.domContentLoadedEventEnd - perf.responseEnd, // DOM loading timing
- // @ts-ignore
page_load: perf.loadEventStart, // Page load time
// Backend
- // @ts-ignore
ttfb: perf.responseStart - perf.requestStart,
}
}
@@ -213,19 +233,6 @@ export class Lib {
this.sendRequest('hb', data)
}
- private checkIgnore(path: string): boolean {
- const ignore = this.pageViewsOptions?.ignore
-
- if (Array.isArray(ignore)) {
- 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
- }
- }
- return false
- }
-
// Tracking path changes. If path changes -> calling this.trackPage method
private trackPathChange(): void {
if (!this.pageData) return
@@ -244,13 +251,7 @@ export class Lib {
// Assuming that this function is called in trackPage and this.activePage is not overwritten by new value yet
// That method of getting previous page works for SPA websites
if (this.activePage) {
- const shouldIgnore = this.checkIgnore(this.activePage)
-
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
- return null
- }
-
- return shouldIgnore ? null : this.activePage
+ return this.activePage
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
@@ -272,13 +273,7 @@ export class Lib {
return null
}
- const shouldIgnore = this.checkIgnore(pathname)
-
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
- return null
- }
-
- return shouldIgnore ? null : pathname
+ return pathname
} catch {
return null
}
@@ -291,68 +286,62 @@ export class Lib {
if (!this.pageData) return
this.pageData.path = pg
- const shouldIgnore = this.checkIgnore(pg)
-
- if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) return
-
const perf = this.getPerformanceStats()
- let prev
-
- if (!this.pageViewsOptions?.noUserFlow) {
- prev = this.getPreviousPage()
- }
+ const prev = this.getPreviousPage()
this.activePage = pg
- this.submitPageView(shouldIgnore ? null : pg, prev, unique, perf)
+ this.submitPageView(pg, prev, unique, perf, true)
}
- submitPageView(pg: null | string, prev: string | null | undefined, unique: boolean, perf: any): void {
- const data = {
+ submitPageView(
+ pg: string,
+ prev: string | null | undefined,
+ unique: boolean,
+ perf: IPerfPayload | {},
+ evokeCallback?: boolean,
+ ): void {
+ const privateData = {
pid: this.projectID,
+ perf,
+ unique,
+ }
+ const pvPayload = {
lc: getLocale(),
tz: getTimezone(),
ref: getReferrer(),
so: getUTMSource(),
me: getUTMMedium(),
ca: getUTMCampaign(),
- unique,
pg,
- perf,
prev,
}
- this.sendRequest('', data)
- }
+ if (evokeCallback && this.pageViewsOptions?.callback) {
+ const callbackResult = this.pageViewsOptions.callback(pvPayload)
- private debug(message: string): void {
- if (this.options?.debug) {
- console.log('[Swetrix]', message)
- }
- }
+ if (callbackResult === false) {
+ return
+ }
- private canTrack(): boolean {
- if (this.options?.disabled) {
- this.debug('Tracking disabled: the \'disabled\' setting is set to true.')
- return false
+ if (callbackResult && typeof callbackResult === 'object') {
+ Object.assign(pvPayload, callbackResult)
+ }
}
- if (!isInBrowser()) {
- this.debug('Tracking disabled: script does not run in browser environment.')
- return false
- }
+ Object.assign(pvPayload, privateData)
- 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
- }
+ this.sendRequest('', pvPayload)
+ }
- if (isAutomated()) {
- this.debug('Tracking disabled: navigation is automated by WebDriver.')
+ private canTrack(): boolean {
+ if (
+ this.options?.disabled ||
+ !isInBrowser() ||
+ (this.options?.respectDNT && window.navigator?.doNotTrack === '1') ||
+ (!this.options?.devMode && isLocalhost()) ||
+ isAutomated()
+ ) {
return false
}
diff --git a/src/index.ts b/src/index.ts
index cf185badf..ddaee8568 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,4 @@
-import {
- Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions, defaultPageActions,
-} from './Lib'
+import { Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions, defaultPageActions } from './Lib'
export let LIB_INSTANCE: Lib | null = null
@@ -62,7 +60,7 @@ export function trackViews(options?: PageViewsOptions): Promise<PageActions> {
/**
* This function is used to manually track a page view event.
* It's useful if your application uses esoteric routing which is not supported by Swetrix by default.
- *
+ *
* @param path Path of the page to track (this will be sent to the Swetrix API and displayed in the dashboard).
* @param prev Path of the previous page.
* @param unique If set to `true`, only 1 event with the same ID will be saved per user session.