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!

Simpler session replay tracking

Blue Mousececef480c2fc
diff --git a/web/app/App.tsx b/web/app/App.tsx
index 4f6ea2521..b5b6d0d11 100644
--- a/web/app/App.tsx
+++ b/web/app/App.tsx
@@ -2,37 +2,26 @@ import _endsWith from 'lodash/endsWith'
import _includes from 'lodash/includes'
import _startsWith from 'lodash/startsWith'
import { useEffect } from 'react'
-import { useLocation, Outlet, useNavigation } from 'react-router'
+import { useLocation, Outlet } from 'react-router'
import 'dayjs/locale/uk'
import { Toaster } from 'sonner'
import Footer from '~/components/Footer'
import Header from '~/components/Header'
import { stripLangFromPath } from '~/lib/constants'
-import {
- stopSessionReplayIfPrivatePath,
- trackSessionReplay,
-} from '~/utils/analytics'
+import { trackSessionReplay } from '~/utils/analytics'
import routesPath from '~/utils/routes'
import { useTheme } from './providers/ThemeProvider'
const App = () => {
const { pathname: rawPathname } = useLocation()
- const navigation = useNavigation()
const pathname = stripLangFromPath(rawPathname)
- const pendingPathname = navigation.location?.pathname
const { theme } = useTheme()
useEffect(() => {
- trackSessionReplay(rawPathname)
- }, [rawPathname])
-
- useEffect(() => {
- if (pendingPathname) {
- stopSessionReplayIfPrivatePath(pendingPathname)
- }
- }, [pendingPathname])
+ trackSessionReplay()
+ }, [])
const isOnboardingPage = pathname === routesPath.onboarding
const isProjectViewPage =
diff --git a/web/app/utils/analytics.ts b/web/app/utils/analytics.ts
index 5b09a0074..4eb9acf7d 100644
--- a/web/app/utils/analytics.ts
+++ b/web/app/utils/analytics.ts
@@ -11,13 +11,7 @@ import {
type TrackEventOptions,
} from 'swetrix'
-import {
- isBrowser,
- isDevelopment,
- isSelfhosted,
- stripLangFromPath,
-} from '~/lib/constants'
-import routes from '~/utils/routes'
+import { isBrowser, isDevelopment, isSelfhosted } from '~/lib/constants'
export const SWETRIX_PID = 'STEzHcB1rALV'
const SWETRIX_API_PROXY_PATH = '/_internal_data_inngest_proxy'
@@ -27,35 +21,6 @@ type SessionReplayActions = Awaited<ReturnType<typeof startSessionReplay>>
let sessionReplayActions: SessionReplayActions | null = null
let sessionReplayStart: Promise<SessionReplayActions | null> | null = null
-let sessionReplayStop: Promise<void> | null = null
-let sessionReplayShouldRun = false
-let sessionReplayPrivatePathVersion = 0
-let sessionReplayRestartAfterStop = false
-
-const SESSION_REPLAY_PUBLIC_PATHS = new Set<string>([
- routes.main,
- routes.performance,
- routes.errorTracking,
- routes.captchaLanding,
- routes.captchaDemo,
- routes.forMarketers,
- routes.forStartups,
- routes.forSmallBusinesses,
- routes.gaAlternative,
- routes.blog,
- routes.privacy,
- routes.cookiePolicy,
- routes.dpa,
- routes.security,
- routes.terms,
- routes.imprint,
- routes.dataPolicy,
- routes.open,
- routes.contact,
- routes.bookACall,
-])
-
-const SESSION_REPLAY_PUBLIC_PREFIXES = [`${routes.blog}/`, '/comparison/']
const REFS_TO_IGNORE = [
/https:\/\/swetrix.com\/(?:[^/]+\/)?projects\/(?!new$)[^/]+$/i,
@@ -155,77 +120,10 @@ const getNewPath = (path: string | undefined | null) => {
return path
}
-const getSessionReplayPath = (pathname: string) => {
- const path = stripLangFromPath(pathname)
-
- if (path.length > 1 && path.endsWith('/')) {
- return path.slice(0, -1)
- }
-
- return path
-}
-
-const canTrackSessionReplay = (pathname: string) => {
- const path = getSessionReplayPath(pathname)
-
- return (
- SESSION_REPLAY_PUBLIC_PATHS.has(path) ||
- SESSION_REPLAY_PUBLIC_PREFIXES.some((prefix) => path.startsWith(prefix))
- )
-}
-
-const stopSessionReplayActions = async (actions: SessionReplayActions) => {
- try {
- await actions.stop()
- } catch (reason) {
- console.error('Failed to stop Swetrix session replay:', reason)
- }
-}
-
-const queueSessionReplayStop = (actions: SessionReplayActions) => {
- const stop = stopSessionReplayActions(actions).finally(() => {
- if (sessionReplayStop === stop) {
- sessionReplayStop = null
- }
- })
-
- sessionReplayStop = stop
-
- return stop
-}
-
-const stopActiveSessionReplay = () => {
- const actions = sessionReplayActions
- sessionReplayActions = null
-
- if (actions) {
- void queueSessionReplayStop(actions)
- }
-}
-
-const markSessionReplayPrivate = () => {
- sessionReplayShouldRun = false
- sessionReplayPrivatePathVersion += 1
- stopActiveSessionReplay()
-}
-
-const startSessionReplayOnPublicPath = async (privatePathVersion: number) => {
- if (sessionReplayStop) {
- await sessionReplayStop
- }
-
- if (
- !sessionReplayShouldRun ||
- privatePathVersion !== sessionReplayPrivatePathVersion ||
- !canTrackSessionReplay(window.location.pathname)
- ) {
- return null
- }
-
- return startSessionReplay({
+const startSessionReplayTracking = async () =>
+ startSessionReplay({
privacy: 'normal',
})
-}
init(SWETRIX_PID, {
disabled: isDevelopment,
@@ -256,16 +154,8 @@ export const trackViews = () => {
})
}
-export const trackSessionReplay = (pathname: string) => {
+export const trackSessionReplay = () => {
if (isSelfhosted || !isBrowser || isDevelopment || isIframe) {
- markSessionReplayPrivate()
- return
- }
-
- sessionReplayShouldRun = canTrackSessionReplay(pathname)
-
- if (!sessionReplayShouldRun) {
- markSessionReplayPrivate()
return
}
@@ -273,34 +163,12 @@ export const trackSessionReplay = (pathname: string) => {
return
}
- const privatePathVersion = sessionReplayPrivatePathVersion
-
- sessionReplayStart = startSessionReplayOnPublicPath(privatePathVersion)
+ sessionReplayStart = startSessionReplayTracking()
.then(async (actions) => {
if (!actions) {
return null
}
- const isPublicPath = canTrackSessionReplay(window.location.pathname)
-
- if (
- !sessionReplayShouldRun ||
- !isPublicPath ||
- privatePathVersion !== sessionReplayPrivatePathVersion
- ) {
- await queueSessionReplayStop(actions)
-
- if (
- sessionReplayShouldRun &&
- isPublicPath &&
- privatePathVersion !== sessionReplayPrivatePathVersion
- ) {
- sessionReplayRestartAfterStop = true
- }
-
- return null
- }
-
sessionReplayActions = actions
return actions
@@ -311,28 +179,9 @@ export const trackSessionReplay = (pathname: string) => {
})
.finally(() => {
sessionReplayStart = null
-
- if (sessionReplayRestartAfterStop) {
- sessionReplayRestartAfterStop = false
- trackSessionReplay(window.location.pathname)
- }
})
}
-export const stopSessionReplayIfPrivatePath = (pathname: string) => {
- if (
- isSelfhosted ||
- !isBrowser ||
- isDevelopment ||
- isIframe ||
- canTrackSessionReplay(pathname)
- ) {
- return
- }
-
- markSessionReplayPrivate()
-}
-
export const trackErrors = () => {
if (isSelfhosted || !isBrowser || isDevelopment || isIframe) {
return