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!

Added 'ignore' PageViews param

Blue Mousece0383ca3317
diff --git a/src/Lib.ts b/src/Lib.ts
index a2d568f2e..265c0acf2 100644
--- a/src/Lib.ts
+++ b/src/Lib.ts
@@ -37,18 +37,21 @@ export interface PageViewsOptions {
// If true, only unique events will be saved
// This param is useful when tracking single-page landing websites
unique?: boolean
+
+ // A list of Regular Expressions or string pathes to ignore
+ ignore?: Array<any>
}
const host = 'https://api.swetrix.com/log'
export class Lib {
private pageData: PageData | null = null
+ private pageViewsOptions: PageViewsOptions | null | undefined = null
constructor(private projectID: string, private options?: LibOptions) {
this.trackPathChange = this.trackPathChange.bind(this)
}
- // Tracks a custom event
track(event: TrackEventOptions) {
if (!this.canTrack()) {
return
@@ -61,15 +64,16 @@ export class Lib {
this.submitCustom(data)
}
- // Tracks page views
trackPageViews(options?: PageViewsOptions) {
if (!this.canTrack()) {
return
}
+
if (this.pageData) {
return this.pageData.actions
}
+ this.pageViewsOptions = options
let interval: NodeJS.Timeout
if (!options?.unique) {
interval = setInterval(this.trackPathChange, 2000)
@@ -88,6 +92,18 @@ export class Lib {
return this.pageData.actions
}
+ 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
+ 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() {
if (!this.pageData) return
@@ -103,6 +119,8 @@ export class Lib {
if (!this.pageData) return
this.pageData.path = pg
+ if (this.checkIgnore(pg)) return
+
const data = {
pid: this.projectID,
lc: getLocale(),