📈 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
Guard analytics against junk traffic
Blue Mouse6eff883181fb
diff --git a/web/app/root.tsx b/web/app/root.tsxindex 6ad0389cf..0dd3adf57 100644--- a/web/app/root.tsx+++ b/web/app/root.tsx@@ -42,7 +42,11 @@ import {import mainCss from '~/styles/index.css?url'import tailwindCss from '~/styles/tailwind.css?url'import { trackErrors, trackError } from '~/utils/analytics'-import { isBlogPostPath, trackPageview } from '~/utils/analytics.server'+import {+ isBlogPostPath,+ isTrackablePagePath,+ trackPageview,+} from '~/utils/analytics.server'import { getCookie } from '~/utils/cookie'import { detectTheme, isWWW } from '~/utils/server'import { createHeadersWithCookies, hasAuthTokens } from '~/utils/session.server'@@ -221,7 +225,10 @@ export async function loader({ request }: LoaderFunctionArgs) {return redirect(httpToHttps, 301)}- if (!isBlogPostPath(urlObject.pathname)) {+ if (+ !isBlogPostPath(urlObject.pathname) &&+ isTrackablePagePath(urlObject.pathname)+ ) {void trackPageview(request, { pg: urlObject.pathname })}diff --git a/web/app/utils/analytics.server.ts b/web/app/utils/analytics.server.tsindex 8f8c2a3a4..2b8cd1e11 100644--- a/web/app/utils/analytics.server.ts+++ b/web/app/utils/analytics.server.ts@@ -23,6 +23,58 @@ const REFS_TO_IGNORE = []const BLOG_PAGE_REGEX = /^\/blog\/.+$/+const COMPARISON_PAGE_REGEX = /^\/comparison\/[^/]+\/?$/++const TRACKABLE_STATIC_PATHS = new Set([+ '/',+ '/dashboard',+ '/login',+ '/signup',+ '/onboarding',+ '/projects',+ '/open',+ '/performance',+ '/error-tracking',+ '/contact',+ '/socialised',+ '/google-analytics-alternative',+ '/for-startups',+ '/for-small-businesses',+ '/for-marketers',+ '/user-settings',+ '/billing',+ '/blog',+ '/tools',+ '/tools/ip-lookup',+ '/tools/ctr-calculator',+ '/tools/roi-calculator',+ '/tools/sitemap-validator',+ '/tools/utm-generator',+ '/captcha',+ '/captcha/demo',+ '/organisations',+ '/data-policy',+ '/cookie-policy',+ '/privacy',+ '/terms',+ '/imprint',+])++const TRACKABLE_DYNAMIC_PATHS = [+ /^\/projects\/(?!new$)[^/]+$/i,+ /^\/projects\/[^/]+\/subscribers\/invite$/i,+ /^\/projects\/settings\/[^/]+$/i,+ /^\/verify\/[^/]+$/i,+ /^\/password-reset\/[^/]+$/i,+ /^\/change-email\/[^/]+$/i,+ /^\/share\/[^/]+$/i,+ /^\/reports-unsubscribe\/[^/]+$/i,+ /^\/3rd-party-unsubscribe\/[^/]+$/i,+ /^\/ref\/[^/]+$/i,+ /^\/ref\/[^/]+\/[^/]+$/i,+ /^\/organisations\/[^/]+$/i,+ /^\/organisation\/invite\/[^/]+$/i,+]const EXPERIMENT_FETCH_TIMEOUT_MS = 5_000@@ -58,6 +110,38 @@ const getNewPath = (path: string | undefined) => {export const isBlogPostPath = (path: string) => BLOG_PAGE_REGEX.test(path)+const normalizePath = (path: string) => {+ if (path.length > 1 && path.endsWith('/')) {+ return path.slice(0, -1)+ }++ return path+}++export const isTrackablePagePath = (path: string | undefined) => {+ if (!path) {+ return false+ }++ const normalizedPath = normalizePath(path)++ if (+ TRACKABLE_STATIC_PATHS.has(normalizedPath) ||+ isBlogPostPath(normalizedPath) ||+ COMPARISON_PAGE_REGEX.test(normalizedPath)+ ) {+ return true+ }++ for (let i = 0; i < TRACKABLE_DYNAMIC_PATHS.length; ++i) {+ if (TRACKABLE_DYNAMIC_PATHS[i].test(normalizedPath)) {+ return true+ }+ }++ return false+}+export const trackPageview = async (request: Request,options: TrackPageViewOptions = {},@@ -70,7 +154,13 @@ export const trackPageview = async (const ip = getClientIP(request) || '127.0.0.1'const userAgent = request.headers.get('user-agent') || ''const url = new URL(request.url)- const pg = getNewPath(options.pg || url.pathname)+ const rawPath = options.pg || url.pathname++ if (!isTrackablePagePath(rawPath)) {+ return+ }++ const pg = getNewPath(rawPath)const ref = options.ref || request.headers.get('referer') || undefinedawait swetrix.trackPageView(ip, userAgent, {