📈 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
(feature) Hash & search based routing support (#10)
Andrii Romasiun1977873292d9
diff --git a/src/Lib.ts b/src/Lib.tsindex 3e63ce290..df6f69d01 100644--- a/src/Lib.ts+++ b/src/Lib.ts@@ -74,6 +74,18 @@ export interface PageViewsOptions {/** 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+ */+ hash?: boolean++ /**+ * Set to `true` to enable search-based routing.+ * For example if you have pages like /path?search+ */+ search?: boolean}export const defaultPageActions = {@@ -132,7 +144,10 @@ export class Lib {hbInterval = setInterval(this.heartbeat, 28000)}- const path = getPath()+ const path = getPath({+ hash: options?.hash,+ search: options?.search,+ })this.pageData = {path,@@ -214,7 +229,10 @@ export class Lib {// Tracking path changes. If path changes -> calling this.trackPage methodprivate trackPathChange(): void {if (!this.pageData) return- const newPath = getPath()+ const newPath = getPath({+ hash: this.pageViewsOptions?.hash,+ search: this.pageViewsOptions?.search,+ })const { path } = this.pageDataif (path !== newPath) {diff --git a/src/utils.ts b/src/utils.tsindex f848d444d..35542bc02 100644--- a/src/utils.ts+++ b/src/utils.ts@@ -1,3 +1,8 @@+interface IGetPath {+ hash?: boolean+ search?: boolean+}+const findInSearch = (exp: RegExp): string | undefined => {const res = location.search.match(exp)return (res && res[2]) || undefined@@ -41,7 +46,34 @@ export const getUTMMedium = () => findInSearch(utmMediumRegex)export const getUTMCampaign = () => findInSearch(utmCampaignRegex)-export const getPath = (): string => {- // TODO: Maybe we should also include such data as location.hash or location.search- return location.pathname || ''+/**+ * Function used to track the current page (path) of the application.+ * Will work in cases where the path looks like:+ * - /path+ * - /#/path+ * - /path?search+ * - /path?search#hash+ * - /path#hash?search+ *+ * @param options - Options for the function.+ * @param options.hash - Whether to trigger on hash change.+ * @param options.search - Whether to trigger on search change.+ * @returns The path of the current page.+ */+export const getPath = (options: IGetPath): string => {+ let result = location.pathname || ''++ if (options.hash) {+ const hashIndex = location.hash.indexOf('?')+ const hashString = hashIndex > -1 ? location.hash.substring(0, hashIndex) : location.hash+ result += hashString+ }++ if (options.search) {+ const hashIndex = location.hash.indexOf('?')+ const searchString = location.search || (hashIndex > -1 ? location.hash.substring(hashIndex) : '')+ result += searchString+ }++ return result}