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 support for previous pages for website with page-reloads

Blue Mousef767dd3092ef
diff --git a/src/Lib.ts b/src/Lib.ts
index dfa57c834..88f675ad7 100644
--- a/src/Lib.ts
+++ b/src/Lib.ts
@@ -212,6 +212,41 @@ export class Lib {
}
}
+ private getPreviousPage(): string | null {
+ // 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) {
+ return this.activePage
+ }
+
+ // Checking if URL is supported by the browser (for example, IE11 does not support it)
+ if (typeof URL === 'function') {
+ // That method of getting previous page works for websites with page reloads
+ const referrer = getReferrer()
+
+ if (!referrer) {
+ return null
+ }
+
+ const { host } = location
+
+ try {
+ const url = new URL(referrer)
+ const { host: refHost, pathname } = url
+
+ if (host !== refHost) {
+ return null
+ }
+
+ return pathname
+ } catch {
+ return null
+ }
+ }
+
+ return null
+ }
+
private trackPage(pg: string, unique: boolean = false): void {
if (!this.pageData) return
this.pageData.path = pg
@@ -231,7 +266,7 @@ export class Lib {
unique,
pg,
perf,
- prev: this.activePage,
+ prev: this.getPreviousPage(),
}
this.activePage = pg