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) `doNotAnonymise` parameter (#7)

Andrii Romasiun109e0b9c568d
diff --git a/.gitignore b/.gitignore
index 84eba26f5..df486907f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -106,3 +106,4 @@ package-lock.json
.DS_Store
pnpm-lock.yaml
+yarn.lock
diff --git a/package.json b/package.json
index 3df03dcfb..205a25529 100644
--- a/package.json
+++ b/package.json
@@ -34,18 +34,18 @@
},
"homepage": "https://swetrix.com/docs",
"dependencies": {
- "@types/node": "^18.16.1",
- "tslib": "^2.5.0"
+ "@types/node": "^20.5.9",
+ "tslib": "^2.6.2"
},
"devDependencies": {
- "@rollup/plugin-commonjs": "^24.1.0",
- "@rollup/plugin-node-resolve": "^15.0.2",
+ "@rollup/plugin-commonjs": "^25.0.4",
+ "@rollup/plugin-node-resolve": "^15.2.1",
"rimraf": "^4.4.1",
"rollup": "^2.79.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-sourcemaps": "^0.6.3",
- "rollup-plugin-typescript2": "^0.34.1",
+ "rollup-plugin-typescript2": "^0.35.0",
"rollup-plugin-uglify": "^6.0.4",
- "typescript": "^5.0.4"
+ "typescript": "^5.2.2"
}
}
diff --git a/src/Lib.ts b/src/Lib.ts
index 2ff2dd741..172e75fae 100644
--- a/src/Lib.ts
+++ b/src/Lib.ts
@@ -58,6 +58,9 @@ export interface PageViewsOptions {
/** 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
@@ -178,7 +181,6 @@ export class Lib {
}
}
-
private heartbeat(): void {
if (!this.pageViewsOptions?.heartbeatOnBackground && document.visibilityState === 'hidden') {
return
@@ -219,11 +221,13 @@ 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) {
- if (this.checkIgnore(this.activePage)) {
+ const shouldIgnore = this.checkIgnore(this.activePage)
+
+ if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
return null
}
- return this.activePage
+ return shouldIgnore ? null : this.activePage
}
// Checking if URL is supported by the browser (for example, IE11 does not support it)
@@ -245,11 +249,13 @@ export class Lib {
return null
}
- if (this.checkIgnore(pathname)) {
+ const shouldIgnore = this.checkIgnore(pathname)
+
+ if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) {
return null
}
- return pathname
+ return shouldIgnore ? null : pathname
} catch {
return null
}
@@ -262,7 +268,9 @@ export class Lib {
if (!this.pageData) return
this.pageData.path = pg
- if (this.checkIgnore(pg)) return
+ const shouldIgnore = this.checkIgnore(pg)
+
+ if (shouldIgnore && this.pageViewsOptions?.doNotAnonymise) return
const perf = this.getPerformanceStats()
@@ -281,7 +289,7 @@ export class Lib {
me: getUTMMedium(),
ca: getUTMCampaign(),
unique,
- pg,
+ pg: shouldIgnore ? null : pg,
perf,
prev,
}