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!

Add refs and mapping of paths (#406)

Yehor Dremliuha692be7d61f76
diff --git a/app/utils/analytics.ts b/app/utils/analytics.ts
index 1db4eca97..ef5ef00e8 100644
--- a/app/utils/analytics.ts
+++ b/app/utils/analytics.ts
@@ -3,6 +3,52 @@ import { isDevelopment, isSelfhosted } from 'redux/constants'
const SWETRIX_PID = 'STEzHcB1rALV'
+const REFS_TO_IGNORE = [
+ /https:\/\/swetrix.com\/projects\/(?!new$)[^/]+$/i,
+ /https:\/\/swetrix.com\/projects\/settings/i,
+ /https:\/\/swetrix.com\/verify/i,
+ /https:\/\/swetrix.com\/password-reset/i,
+ /https:\/\/swetrix.com\/change-email/i,
+ /https:\/\/swetrix.com\/share/i,
+ /https:\/\/swetrix.com\/captchas\/(?!new$)[^/]+$/i,
+ /https:\/\/swetrix.com\/captchas\/settings/i,
+]
+
+const PATHS_REPLACEMENT_MAP = [
+ {
+ regex: /^\/projects\/(?!new$)[^/]+$/i,
+ replacement: '/projects/[id]',
+ },
+ {
+ regex: /^\/projects\/settings/i,
+ replacement: '/projects/settings/[id]',
+ },
+ {
+ regex: /^\/verify/i,
+ replacement: '/verify/[token]',
+ },
+ {
+ regex: /^\/password-reset/i,
+ replacement: '/password-reset/[token]',
+ },
+ {
+ regex: /^\/change-email/i,
+ replacement: '/change-email/[token]',
+ },
+ {
+ regex: /^\/share/i,
+ replacement: '/share/[token]',
+ },
+ {
+ regex: /^\/captchas\/(?!new$)[^/]+$/i,
+ replacement: '/captchas/[id]',
+ },
+ {
+ regex: /^\/captchas\/settings/i,
+ replacement: '/captchas/settings/[id]',
+ },
+]
+
const checkIgnore = (path: string | undefined | null, ignore: RegExp[]) => {
if (!path) {
return false
@@ -17,32 +63,28 @@ const checkIgnore = (path: string | undefined | null, ignore: RegExp[]) => {
return false
}
-const pathsToIgnore = [
- /^\/projects\/(?!new$)[^/]+$/i,
- /^\/projects\/settings/i,
- /^\/verify/i,
- /^\/password-reset/i,
- /^\/change-email/i,
- /^\/share/i,
- /^\/captchas\/(?!new$)[^/]+$/i,
- /^\/captchas\/settings/i,
-]
-const refsToIgnore = [
- /https:\/\/swetrix.com\/projects\/(?!new$)[^/]+$/i,
- /https:\/\/swetrix.com\/projects\/settings/i,
- /https:\/\/swetrix.com\/verify/i,
- /https:\/\/swetrix.com\/password-reset/i,
- /https:\/\/swetrix.com\/change-email/i,
- /https:\/\/swetrix.com\/share/i,
- /https:\/\/swetrix.com\/captchas\/(?!new$)[^/]+$/i,
- /https:\/\/swetrix.com\/captchas\/settings/i,
-]
+const getNewPath = (path: string | undefined | null) => {
+ if (!path) {
+ return path
+ }
+
+ for (let i = 0; i < PATHS_REPLACEMENT_MAP.length; ++i) {
+ const map = PATHS_REPLACEMENT_MAP[i]
+
+ if (map.regex.test(path)) {
+ return map.replacement
+ }
+ }
+
+ return path
+}
Swetrix.init(SWETRIX_PID, {
devMode: isDevelopment,
})
+
const trackViews = () => {
if (!isSelfhosted) {
Swetrix.trackViews({
@@ -53,15 +95,10 @@ const trackViews = () => {
ref,
}
- if (checkIgnore(pg, pathsToIgnore)) {
- result.pg = null
- }
-
- if (checkIgnore(prev, pathsToIgnore)) {
- result.prev = null
- }
+ result.pg = getNewPath(pg)
+ result.prev = getNewPath(prev)
- if (checkIgnore(ref, refsToIgnore)) {
+ if (checkIgnore(ref, REFS_TO_IGNORE)) {
result.ref = undefined
}