Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement session replay feature and enhance analytics script (#424)

Bill Yangf69af70c42d1
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 57edeb70a..ebd413364 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -16,7 +16,11 @@
"Bash(npm run:*)",
"Bash(mv:*)",
"Bash(chmod:*)",
- "Bash(rg:*)"
+ "Bash(rg:*)",
+ "Bash(sed:*)",
+ "Bash(cat:*)",
+ "Bash(find:*)",
+ "Bash(clickhouse-client:*)"
]
},
"enableAllProjectMcpServers": false
diff --git a/Caddyfile b/Caddyfile
index b47fc80c0..95cdea683 100644
--- a/Caddyfile
+++ b/Caddyfile
@@ -1,17 +1,15 @@
# Caddyfile
-# Use the domain name passed from docker-compose environment
{$DOMAIN_NAME} {
# Enable compression
encode zstd gzip
+ request_body max_size 10MB
handle /api/* {
reverse_proxy backend:3001
}
-
# Proxy all other requests to the client service
handle {
reverse_proxy client:3002
}
-
-}
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/client/package-lock.json b/client/package-lock.json
index 75755d66b..7aca3be3a 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -21,6 +21,7 @@
"@radix-ui/react-label": "2.1.6",
"@radix-ui/react-navigation-menu": "1.2.12",
"@radix-ui/react-popover": "1.1.13",
+ "@radix-ui/react-scroll-area": "^1.2.9",
"@radix-ui/react-select": "2.2.4",
"@radix-ui/react-separator": "1.1.6",
"@radix-ui/react-slider": "1.3.4",
@@ -64,6 +65,8 @@
"react-leaflet": "5.0.0",
"react-scan": "^0.3.4",
"react-select": "5.10.1",
+ "rrweb": "^2.0.0-alpha.11",
+ "rrweb-player": "^2.0.0-alpha.11",
"sonner": "2.0.3",
"tailwind-merge": "2.6.0",
"tailwindcss-animate": "1.0.7",
@@ -4919,6 +4922,7 @@
"version": "1.2.9",
"resolved": "https://registry.npmjs.org/@radix-ui/react-scroll-area/-/react-scroll-area-1.2.9.tgz",
"integrity": "sha512-YSjEfBXnhUELsO2VzjdtYYD4CfQjvao+lhhrX5XsHD7/cyUNzljF1FHEbgTPN7LH2MClfwRMIsYlqTYpKTTe2A==",
+ "license": "MIT",
"dependencies": {
"@radix-ui/number": "1.1.1",
"@radix-ui/primitive": "1.1.2",
@@ -5957,6 +5961,44 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
+ "node_modules/@rrweb/packer": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/@rrweb/packer/-/packer-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-rEXltE/gnflEv/NatVNPIp4/6EtVlPLSwzIbd4WPSMtQGvwZe5OfAAk/Y88HkbAwb1nHfdA3PtFGn2Epik4inQ==",
+ "license": "MIT",
+ "dependencies": {
+ "@rrweb/types": "^2.0.0-alpha.18",
+ "fflate": "^0.4.4"
+ }
+ },
+ "node_modules/@rrweb/packer/node_modules/fflate": {
+ "version": "0.4.8",
+ "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz",
+ "integrity": "sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==",
+ "license": "MIT"
+ },
+ "node_modules/@rrweb/replay": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/@rrweb/replay/-/replay-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-EGCdydiQHCEbh1j+EF7OupwRHD1/EnAYYF4ENBz4CReoH+jGsLKu0LT2962CWPnQttMvpxwzhmD5gFGf63Gm0g==",
+ "license": "MIT",
+ "dependencies": {
+ "@rrweb/types": "^2.0.0-alpha.18",
+ "rrweb": "^2.0.0-alpha.18"
+ }
+ },
+ "node_modules/@rrweb/types": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/@rrweb/types/-/types-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-iMH3amHthJZ9x3gGmBPmdfim7wLGygC2GciIkw2A6SO8giSn8PHYtRT8OKNH4V+k3SZ6RSnYHcTQxBA7pSWZ3Q==",
+ "license": "MIT"
+ },
+ "node_modules/@rrweb/utils": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/@rrweb/utils/-/utils-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-qV8azQYo9RuwW4NGRtOiQfTBdHNL1B0Q//uRLMbCSjbaKqJYd88Js17Bdskj65a0Vgp2dwTLPIZ0gK47dfjfaA==",
+ "license": "MIT"
+ },
"node_modules/@rybbit/shared": {
"resolved": "../shared",
"link": true
@@ -6092,6 +6134,12 @@
"url": "https://github.com/sponsors/tannerlinsley"
}
},
+ "node_modules/@tsconfig/svelte": {
+ "version": "1.0.13",
+ "resolved": "https://registry.npmjs.org/@tsconfig/svelte/-/svelte-1.0.13.tgz",
+ "integrity": "sha512-5lYJP45Xllo4yE/RUBccBT32eBlRDbqN8r1/MIvQbKxW3aFqaYPCNgm8D5V20X4ShHcwvYWNlKg3liDh1MlBoA==",
+ "license": "MIT"
+ },
"node_modules/@turf/boolean-point-in-polygon": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-7.2.0.tgz",
@@ -6157,6 +6205,12 @@
"integrity": "sha512-CefEn/J336TBDp7NX8JqzlDtCBOsm8M3r1Li0gEOt0HOMHF1XemNyrx9lSHjsafcb1yYWybU0N8ZAXuyCaND0w==",
"license": "MIT"
},
+ "node_modules/@types/css-font-loading-module": {
+ "version": "0.0.7",
+ "resolved": "https://registry.npmjs.org/@types/css-font-loading-module/-/css-font-loading-module-0.0.7.tgz",
+ "integrity": "sha512-nl09VhutdjINdWyXxHWN/w9zlNCfr60JUqJbd24YXUuCwgeL0TpFSdElCwb6cxfB6ybE19Gjj4g0jsgkXxKv1Q==",
+ "license": "MIT"
+ },
"node_modules/@types/d3": {
"version": "7.4.3",
"resolved": "https://registry.npmjs.org/@types/d3/-/d3-7.4.3.tgz",
@@ -6555,6 +6609,12 @@
"integrity": "sha512-8B/tdfRFKdrnejqmvq95ogp8tf52oZ51p3f4QD5m5Paey/qlX4Rhhy5Y8tgFMi7Ms70HzcMMw3EQjH/jdhTwlA==",
"license": "BSD-3-Clause"
},
+ "node_modules/@xstate/fsm": {
+ "version": "1.6.5",
+ "resolved": "https://registry.npmjs.org/@xstate/fsm/-/fsm-1.6.5.tgz",
+ "integrity": "sha512-b5o1I6aLNeYlU/3CPlj/Z91ybk1gUsKT+5NAJI+2W4UjvS5KLG28K9v5UvNoFVjHV8PajVZ00RH3vnjyQO7ZAw==",
+ "license": "MIT"
+ },
"node_modules/accessor-fn": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/accessor-fn/-/accessor-fn-1.5.3.tgz",
@@ -6699,6 +6759,15 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/base64-arraybuffer": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz",
+ "integrity": "sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 0.6.0"
+ }
+ },
"node_modules/better-auth": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/better-auth/-/better-auth-1.2.7.tgz",
@@ -8677,6 +8746,12 @@
"node": ">=16 || 14 >=14.17"
}
},
+ "node_modules/mitt": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.1.tgz",
+ "integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==",
+ "license": "MIT"
+ },
"node_modules/motion-dom": {
"version": "12.11.0",
"resolved": "https://registry.npmjs.org/motion-dom/-/motion-dom-12.11.0.tgz",
@@ -9093,7 +9168,6 @@
"version": "8.5.3",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.3.tgz",
"integrity": "sha512-dle9A3yYxlBSrt8Fu+IpjGT8SY8hN0mlaA6GY8t0P5PjIOZemULz/E2Bnm/2dcUOena75OTNkHI76uZBNUUq3A==",
- "dev": true,
"funding": [
{
"type": "opencollective",
@@ -10357,6 +10431,51 @@
"integrity": "sha512-OXMmJ3zRk2xeXFGfA3K+EOPHC5u7RDFG7lIOx0X1pdnhUkI8MdVrbV+sNsD80ElpUZ+MRHdyxPnFthq9VHs8uQ==",
"license": "MIT"
},
+ "node_modules/rrdom": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/rrdom/-/rrdom-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-fSFzFFxbqAViITyYVA4Z0o5G6p1nEqEr/N8vdgSKie9Rn0FJxDSNJgjV0yiCIzcDs0QR+hpvgFhpbdZ6JIr5Nw==",
+ "license": "MIT",
+ "dependencies": {
+ "rrweb-snapshot": "^2.0.0-alpha.18"
+ }
+ },
+ "node_modules/rrweb": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/rrweb/-/rrweb-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-1mjZcB+LVoGSx1+i9E2ZdAP90fS3MghYVix2wvGlZvrgRuLCbTCCOZMztFCkKpgp7/EeCdYM4nIHJkKX5J1Nmg==",
+ "license": "MIT",
+ "dependencies": {
+ "@rrweb/types": "^2.0.0-alpha.18",
+ "@rrweb/utils": "^2.0.0-alpha.18",
+ "@types/css-font-loading-module": "0.0.7",
+ "@xstate/fsm": "^1.4.0",
+ "base64-arraybuffer": "^1.0.1",
+ "mitt": "^3.0.0",
+ "rrdom": "^2.0.0-alpha.18",
+ "rrweb-snapshot": "^2.0.0-alpha.18"
+ }
+ },
+ "node_modules/rrweb-player": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/rrweb-player/-/rrweb-player-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-v31QgtjR9Ei3JnyvaN/2THj6a/i+dG49LzHiUtHcfTIFi7ecVExmMq+QDctVxUsuwdPPTtEcslpQh2tbVbi/ow==",
+ "license": "MIT",
+ "dependencies": {
+ "@rrweb/packer": "^2.0.0-alpha.18",
+ "@rrweb/replay": "^2.0.0-alpha.18",
+ "@tsconfig/svelte": "^1.0.0"
+ }
+ },
+ "node_modules/rrweb-snapshot": {
+ "version": "2.0.0-alpha.18",
+ "resolved": "https://registry.npmjs.org/rrweb-snapshot/-/rrweb-snapshot-2.0.0-alpha.18.tgz",
+ "integrity": "sha512-hBHZL/NfgQX6wO1D9mpwqFu1NJPpim+moIcKhFEjVTZVRUfCln+LOugRc4teVTCISYHN8Cw5e2iNTWCSm+SkoA==",
+ "license": "MIT",
+ "dependencies": {
+ "postcss": "^8.4.38"
+ }
+ },
"node_modules/run-parallel": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
diff --git a/client/package.json b/client/package.json
index 3bc3b4785..57f6862f3 100644
--- a/client/package.json
+++ b/client/package.json
@@ -23,6 +23,7 @@
"@radix-ui/react-label": "2.1.6",
"@radix-ui/react-navigation-menu": "1.2.12",
"@radix-ui/react-popover": "1.1.13",
+ "@radix-ui/react-scroll-area": "^1.2.9",
"@radix-ui/react-select": "2.2.4",
"@radix-ui/react-separator": "1.1.6",
"@radix-ui/react-slider": "1.3.4",
@@ -66,6 +67,8 @@
"react-leaflet": "5.0.0",
"react-scan": "^0.3.4",
"react-select": "5.10.1",
+ "rrweb": "^2.0.0-alpha.11",
+ "rrweb-player": "^2.0.0-alpha.11",
"sonner": "2.0.3",
"tailwind-merge": "2.6.0",
"tailwindcss-animate": "1.0.7",
diff --git a/client/src/api/analytics/sessionReplay/useGetSessionReplayEvents.ts b/client/src/api/analytics/sessionReplay/useGetSessionReplayEvents.ts
new file mode 100644
index 000000000..ddddf5c0c
--- /dev/null
+++ b/client/src/api/analytics/sessionReplay/useGetSessionReplayEvents.ts
@@ -0,0 +1,56 @@
+import { useQuery } from "@tanstack/react-query";
+import { authedFetch } from "../../utils";
+
+export interface SessionReplayEvent {
+ timestamp: number;
+ type: string | number;
+ data: any;
+}
+
+export interface SessionReplayMetadata {
+ session_id: string;
+ user_id: string;
+ start_time: Date;
+ end_time?: Date;
+ duration_ms?: number;
+ event_count: number;
+ compressed_size_bytes: number;
+ page_url: string;
+ user_agent: string;
+ country: string;
+ region: string;
+ city: string;
+ lat: number;
+ lon: number;
+ browser: string;
+ browser_version: string;
+ operating_system: string;
+ operating_system_version: string;
+ language: string;
+ screen_width: number;
+ screen_height: number;
+ device_type: string;
+ channel: string;
+ hostname: string;
+ referrer: string;
+ has_replay_data: boolean;
+ created_at: Date;
+}
+
+export interface GetSessionReplayEventsResponse {
+ events: SessionReplayEvent[];
+ metadata: SessionReplayMetadata;
+}
+
+export function useGetSessionReplayEvents(siteId: number, sessionId: string) {
+ return useQuery({
+ queryKey: ["session-replay-events", siteId, sessionId],
+ queryFn: () => {
+ return authedFetch<GetSessionReplayEventsResponse>(
+ `/session-replay/${sessionId}/${siteId}`
+ );
+ },
+ enabled: !!siteId && !!sessionId,
+ staleTime: 1000 * 60 * 10, // 10 minutes
+ });
+}
diff --git a/client/src/api/analytics/sessionReplay/useGetSessionReplays.ts b/client/src/api/analytics/sessionReplay/useGetSessionReplays.ts
new file mode 100644
index 000000000..380b93158
--- /dev/null
+++ b/client/src/api/analytics/sessionReplay/useGetSessionReplays.ts
@@ -0,0 +1,66 @@
+import { useInfiniteQuery } from "@tanstack/react-query";
+import { useStore } from "../../../lib/store";
+import { authedFetch, getQueryParams } from "../../utils";
+
+export interface SessionReplayListItem {
+ session_id: string;
+ user_id: string;
+ start_time: string;
+ end_time?: string;
+ duration_ms?: number;
+ page_url: string;
+ event_count: number;
+ country: string;
+ region: string;
+ city: string;
+ browser: string;
+ browser_version: string;
+ operating_system: string;
+ operating_system_version: string;
+ device_type: string;
+ screen_width: number;
+ screen_height: number;
+}
+
+export interface SessionReplayListResponse {
+ data: SessionReplayListItem[];
+ totalCount: number;
+}
+
+type UseGetSessionReplaysOptions = {
+ limit?: number;
+};
+
+export function useGetSessionReplays({
+ limit = 20,
+}: UseGetSessionReplaysOptions = {}) {
+ const { time, site, filters } = useStore();
+
+ return useInfiniteQuery({
+ queryKey: ["session-replays", site, time, filters, limit],
+ queryFn: async ({ pageParam = 0 }) => {
+ const queryParams = {
+ ...getQueryParams(time),
+ limit,
+ offset: pageParam,
+ filters,
+ };
+
+ const response = await authedFetch<SessionReplayListResponse>(
+ `/session-replay/list/${site}`,
+ queryParams
+ );
+ return response;
+ },
+ initialPageParam: 0,
+ getNextPageParam: (lastPage, allPages) => {
+ const totalFetched = allPages.reduce(
+ (acc, page) => acc + (page.data?.length || 0),
+ 0
+ );
+ return lastPage.data?.length === limit ? totalFetched : undefined;
+ },
+ staleTime: 1000 * 60 * 5, // 5 minutes
+ refetchOnWindowFocus: false,
+ });
+}
\ No newline at end of file
diff --git a/client/src/app/[site]/components/Sidebar/Sidebar.tsx b/client/src/app/[site]/components/Sidebar/Sidebar.tsx
index 03a4475c5..8b9dbc82a 100644
--- a/client/src/app/[site]/components/Sidebar/Sidebar.tsx
+++ b/client/src/app/[site]/components/Sidebar/Sidebar.tsx
@@ -13,6 +13,7 @@ import {
Split,
User,
File,
+ Play,
} from "lucide-react";
import Link from "next/link";
import { usePathname } from "next/navigation";
@@ -61,6 +62,14 @@ export function Sidebar() {
href={getTabPath("realtime")}
icon={<Earth className="w-4 h-4" />}
/>
+ <div className="hidden md:block">
+ <SidebarLink
+ label="Replay"
+ active={isActiveTab("replay")}
+ href={getTabPath("replay")}
+ icon={<Play className="w-4 h-4" />}
+ />
+ </div>
{IS_CLOUD && (
<SidebarLink
label="Pages"
diff --git a/client/src/app/[site]/layout.tsx b/client/src/app/[site]/layout.tsx
index dad96785a..aa7454645 100644
--- a/client/src/app/[site]/layout.tsx
+++ b/client/src/app/[site]/layout.tsx
@@ -55,9 +55,9 @@ export default function SiteLayout({
{/* <div className="px-4 py-2 max-w-[1400px] mx-auto w-full mb-4"> */}
<Header />
<div>{children}</div>
- {!pathname.includes("/map") && !pathname.includes("/realtime") && (
- <Footer />
- )}
+ {!pathname.includes("/map") &&
+ !pathname.includes("/realtime") &&
+ !pathname.includes("/replay") && <Footer />}
</div>
</div>
</div>
diff --git a/client/src/app/[site]/replay/components/EnableSessionReplay.tsx b/client/src/app/[site]/replay/components/EnableSessionReplay.tsx
new file mode 100644
index 000000000..bba7f6d7f
--- /dev/null
+++ b/client/src/app/[site]/replay/components/EnableSessionReplay.tsx
@@ -0,0 +1,66 @@
+"use client";
+
+import { PlayCircle, ArrowRight } from "lucide-react";
+import Link from "next/link";
+import {
+ Alert,
+ AlertDescription,
+ AlertTitle,
+} from "../../../../components/ui/alert";
+import { Button } from "../../../../components/ui/button";
+import { useGetSessionReplays } from "../../../../api/analytics/sessionReplay/useGetSessionReplays";
+
+export function EnableSessionReplay() {
+ const {
+ data: replayData,
+ isLoading,
+ isError,
+ } = useGetSessionReplays({ limit: 1 });
+
+ // Don't show banner while loading or if there's an error
+ if (isLoading || isError) return null;
+
+ const replays = replayData?.pages[0]?.data ?? [];
+
+ // Only show banner if there are no replays tracked
+ if (replays.length > 0) return null;
+
+ return (
+ <Alert className="p-4 bg-amber-50/50 border-amber-200/50 dark:bg-amber-900/10 dark:border-amber-800/50">
+ <div className="flex items-start space-x-3">
+ <PlayCircle className="h-5 w-5 mt-0.5 text-amber-500/80" />
+ <div className="flex-1">
+ <AlertTitle className="text-base font-semibold mb-1 text-amber-700/90 dark:text-amber-400/90">
+ Enable Session Replay
+ </AlertTitle>
+ <AlertDescription className="text-sm text-amber-700/80 dark:text-amber-400/80 mb-3">
+ Add{" "}
+ <code className="bg-amber-100/70 dark:bg-amber-800/50 px-1 py-0.5 rounded text-xs">
+ data-session-replay="true"
+ </code>{" "}
+ to your script tag to record user sessions for debugging and
+ analysis.
+ </AlertDescription>
+
+ <div className="space-y-2">
+ <div className="flex flex-col space-y-2 sm:flex-row sm:space-y-0 sm:space-x-2 items-start sm:items-center">
+ <Button
+ asChild
+ variant="outline"
+ size="sm"
+ className="border-amber-300/70 text-amber-700/90 hover:bg-amber-100/70 dark:border-amber-600/70 dark:text-amber-400/90 dark:hover:bg-amber-800/50"
+ >
+ <Link
+ href="https://rybbit.io/docs/script#session-replay"
+ target="_blank"
+ >
+ View Documentation <ArrowRight className="ml-1 h-3 w-3" />
+ </Link>
+ </Button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </Alert>
+ );
+}
diff --git a/client/src/app/[site]/replay/components/ReplayBreadcrumbs.tsx b/client/src/app/[site]/replay/components/ReplayBreadcrumbs.tsx
new file mode 100644
index 000000000..45945cdbf
--- /dev/null
+++ b/client/src/app/[site]/replay/components/ReplayBreadcrumbs.tsx
@@ -0,0 +1,273 @@
+import {
+ Camera,
+ Eye,
+ FileText,
+ Globe,
+ Loader2,
+ MousePointer,
+ Puzzle,
+ ScrollText,
+ Sparkles,
+} from "lucide-react";
+import { Duration } from "luxon";
+import { useParams } from "next/navigation";
+import { useMemo } from "react";
+import { useGetSessionReplayEvents } from "../../../../api/analytics/sessionReplay/useGetSessionReplayEvents";
+import { ScrollArea } from "../../../../components/ui/scroll-area";
+import { cn } from "../../../../lib/utils";
+import { useReplayStore } from "./replayStore";
+
+// Event type mapping based on rrweb event types
+const EVENT_TYPE_INFO = {
+ "0": { name: "DOMContentLoaded", icon: FileText, color: "text-blue-400" },
+ "1": { name: "Load", icon: Loader2, color: "text-green-400" },
+ "2": { name: "Full Snapshot", icon: Camera, color: "text-purple-400" },
+ "3": { name: "Incremental", icon: MousePointer, color: "text-yellow-400" },
+ "4": { name: "Meta", icon: Eye, color: "text-cyan-400" },
+ "5": { name: "Custom", icon: Sparkles, color: "text-pink-400" },
+ "6": { name: "Plugin", icon: Puzzle, color: "text-indigo-400" },
+};
+
+// Incremental snapshot types (type 3 subtypes)
+const INCREMENTAL_TYPES = {
+ 0: "Mutation",
+ 1: "Mouse Move",
+ 2: "Mouse Interaction",
+ 3: "Scroll",
+ 4: "Viewport Resize",
+ 5: "Input",
+ 6: "Touch Move",
+ 7: "Media Interaction",
+ 8: "Style Sheet Rule",
+ 9: "Canvas Mutation",
+ 10: "Font",
+ 11: "Log",
+ 12: "Drag",
+ 13: "Style Declaration",
+ 14: "Selection",
+ 15: "Adopted Style Sheet",
+};
+
+export function ReplayBreadcrumbs() {
+ const params = useParams();
+ const siteId = Number(params.site);
+ const { sessionId, player, setCurrentTime } = useReplayStore();
+
+ const { data, isLoading, error } = useGetSessionReplayEvents(
+ siteId,
+ sessionId
+ );
+
+ // Group consecutive events of the same type
+ const groupedEvents = useMemo(() => {
+ if (!data?.events) return [];
+
+ const groups: Array<{
+ events: any[];
+ type: string;
+ subType?: number;
+ startTime: number;
+ endTime: number;
+ count: number;
+ }> = [];
+
+ let currentGroup: (typeof groups)[0] | null = null;
+
+ data.events.forEach((event) => {
+ const eventTypeStr = String(event.type);
+ const subType = eventTypeStr === "3" ? event.data?.source : undefined;
+
+ // Check if this event should be grouped with the previous one
+ const shouldGroup =
+ currentGroup &&
+ currentGroup.type === eventTypeStr &&
+ currentGroup.subType === subType &&
+ // Only group incremental events (type 3) and certain subtypes
+ eventTypeStr === "3" &&
+ (subType === 0 || // Mutation
+ subType === 1 || // Mouse move
+ subType === 2 || // Mouse interaction (click, etc.)
+ subType === 3 || // Scroll
+ subType === 5); // Input
+
+ if (shouldGroup && currentGroup) {
+ // Add to current group
+ currentGroup.events.push(event);
+ currentGroup.endTime = event.timestamp;
+ currentGroup.count++;
+ } else {
+ // Start a new group
+ if (currentGroup) {
+ groups.push(currentGroup);
+ }
+ currentGroup = {
+ events: [event],
+ type: eventTypeStr,
+ subType,
+ startTime: event.timestamp,
+ endTime: event.timestamp,
+ count: 1,
+ };
+ }
+ });
+
+ // Don't forget the last group
+ if (currentGroup) {
+ groups.push(currentGroup);
+ }
+
+ return groups;
+ }, [data?.events]);
+
+ const firstTimestamp = data?.events[0]?.timestamp;
+
+ const getTime = (timestamp: number) => {
+ return timestamp - (firstTimestamp ?? 0);
+ };
+
+ const handleEventClick = (timestamp: number) => {
+ if (!player || !firstTimestamp) return;
+
+ const timeInMs = timestamp - firstTimestamp;
+ const timeInSeconds = timeInMs / 1000;
+
+ // Seek to the specific time
+ player.goto(timeInMs);
+ setCurrentTime(timeInSeconds);
+ };
+
+ const getEventDescription = (event: any) => {
+ const eventTypeStr = String(event.type);
+ const eventInfo = EVENT_TYPE_INFO[
+ eventTypeStr as keyof typeof EVENT_TYPE_INFO
+ ] || {
+ name: `Unknown (${eventTypeStr})`,
+ icon: Globe,
+ color: "text-gray-400",
+ };
+
+ // For incremental snapshots, get more detail
+ if (eventTypeStr === "3" && event.data?.source !== undefined) {
+ const incrementalType =
+ INCREMENTAL_TYPES[
+ event.data.source as keyof typeof INCREMENTAL_TYPES
+ ] || "Unknown";
+ return `${incrementalType}`;
+ }
+
+ // For meta events, show URL if available
+ if (eventTypeStr === "4" && event.data?.href) {
+ const url = event.data.href;
+ try {
+ const urlObj = new URL(url);
+ return urlObj.pathname;
+ } catch {
+ return url;
+ }
+ }
+
+ return eventInfo.name;
+ };
+
+ const getEventIcon = (event: any) => {
+ const eventTypeStr = String(event.type);
+ const eventInfo =
+ EVENT_TYPE_INFO[eventTypeStr as keyof typeof EVENT_TYPE_INFO];
+
+ // Special icons for specific incremental snapshot types
+ if (eventTypeStr === "3" && event.data?.source !== undefined) {
+ if (event.data.source === 3) return ScrollText; // Scroll
+ if (event.data.source === 1 || event.data.source === 2)
+ return MousePointer; // Mouse
+ }
+
+ return eventInfo?.icon || Globe;
+ };
+
+ const getEventColor = (event: any) => {
+ const eventTypeStr = String(event.type);
+ const eventInfo =
+ EVENT_TYPE_INFO[eventTypeStr as keyof typeof EVENT_TYPE_INFO];
+ return eventInfo?.color || "text-gray-400";
+ };
+
+ if (isLoading || !data?.events) {
+ return (
+ <div className="rounded-lg border border-neutral-800 p-4 flex items-center justify-center h-[calc(100vh-120px)]">
+ <Loader2 className="w-6 h-6 animate-spin text-neutral-400" />
+ </div>
+ );
+ }
+
+ const getGroupDescription = (group: (typeof groupedEvents)[0]) => {
+ const firstEvent = group.events[0];
+ const baseDescription = getEventDescription(firstEvent);
+
+ if (group.count > 1) {
+ return `${baseDescription} (×${group.count})`;
+ }
+
+ return baseDescription;
+ };
+
+ const handleGroupClick = (group: (typeof groupedEvents)[0]) => {
+ // Jump to the middle of the group
+ const middleIndex = Math.floor(group.events.length / 2);
+ const middleEvent = group.events[middleIndex];
+ handleEventClick(middleEvent.timestamp);
+ };
+
+ return (
+ <div className="rounded-lg border border-neutral-800 flex flex-col">
+ <div className="p-2 border-b border-neutral-800 bg-neutral-900 text-xs text-neutral-400">
+ {data.events.length} events captured ({groupedEvents.length} groups)
+ </div>
+ <ScrollArea className="h-[calc(100vh-156px)]">
+ <div className="flex flex-col">
+ {groupedEvents.map((group, index) => {
+ const firstEvent = group.events[0];
+ const Icon = getEventIcon(firstEvent);
+ const color = getEventColor(firstEvent);
+ const description = getGroupDescription(group);
+ const startTimeMs = getTime(group.startTime);
+ const endTimeMs = getTime(group.endTime);
+ const durationMs = endTimeMs - startTimeMs;
+
+ return (
+ <div
+ key={`${group.startTime}-${index}`}
+ className={cn(
+ "p-2 border-b border-neutral-800 bg-neutral-900",
+ "hover:bg-neutral-800/80 transition-colors cursor-pointer",
+ "flex items-center gap-2 group"
+ )}
+ onClick={() => handleGroupClick(group)}
+ >
+ <div className="text-xs text-neutral-400 w-10">
+ {Duration.fromMillis(startTimeMs).toFormat("mm:ss")}
+ </div>
+ <Icon className={cn("w-4 h-4 flex-shrink-0", color)} />
+ <div className="flex-1 min-w-0">
+ <div className="text-xs text-neutral-200 font-medium truncate">
+ {description}
+ </div>
+ {group.count > 1 && durationMs > 0 && (
+ <div className="text-xs text-neutral-500 mt-0.5">
+ {Duration.fromMillis(durationMs).toFormat("s.SSS")}s
+ duration
+ </div>
+ )}
+ </div>
+ {group.count > 5 && (
+ <div className="text-xs text-neutral-500 bg-neutral-800 px-1.5 py-0.5 rounded">
+ {group.count}
+ </div>
+ )}
+ </div>
+ );
+ })}
+ </div>
+ </ScrollArea>
+ </div>
+ );
+}
diff --git a/client/src/app/[site]/replay/components/ReplayCard.tsx b/client/src/app/[site]/replay/components/ReplayCard.tsx
new file mode 100644
index 000000000..7c26065ee
--- /dev/null
+++ b/client/src/app/[site]/replay/components/ReplayCard.tsx
@@ -0,0 +1,139 @@
+import { Clock, MousePointerClick } from "lucide-react";
+import { DateTime } from "luxon";
+import Link from "next/link";
+import {
+ BrowserTooltipIcon,
+ CountryFlagTooltipIcon,
+ DeviceTypeTooltipIcon,
+ OperatingSystemTooltipIcon,
+} from "../../../../components/TooltipIcons/TooltipIcons";
+import { Badge } from "../../../../components/ui/badge";
+import { Skeleton } from "../../../../components/ui/skeleton";
+import { cn, formatter } from "../../../../lib/utils";
+import { useReplayStore } from "./replayStore";
+
+interface SessionReplayListItem {
+ session_id: string;
+ user_id: string;
+ start_time: string;
+ end_time?: string;
+ duration_ms?: number;
+ page_url: string;
+ event_count: number;
+ country: string;
+ region: string;
+ city: string;
+ browser: string;
+ browser_version: string;
+ operating_system: string;
+ operating_system_version: string;
+ device_type: string;
+ screen_width: number;
+ screen_height: number;
+}
+
+export function ReplayCard({ replay }: { replay: SessionReplayListItem }) {
+ const { sessionId, setSessionId } = useReplayStore();
+ const startTime = DateTime.fromSQL(replay.start_time, {
+ zone: "utc",
+ }).toLocal();
+ const duration = replay.duration_ms
+ ? Math.ceil(replay.duration_ms / 1000)
+ : null;
+
+ const formatDuration = (seconds: number) => {
+ const minutes = Math.floor(seconds / 60);
+ const remainingSeconds = seconds % 60;
+ return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`;
+ };
+
+ return (
+ <div
+ className={cn(
+ "bg-neutral-900 border-b border-neutral-800 p-3 hover:bg-neutral-800/80 transition-colors cursor-pointer w-[200px]",
+ // "bg-neutral-900 border border-neutral-800 rounded-lg p-3 hover:bg-neutral-800/50 transition-colors cursor-pointer",
+ sessionId === replay.session_id && "bg-neutral-800/80"
+ )}
+ onClick={() => {
+ setSessionId(replay.session_id);
+ }}
+ >
+ <div className="flex items-center gap-2 mb-1">
+ {/* <div className="text-xs text-neutral-500">
+ {replay.user_id.slice(0, 10)}...
+ </div> */}
+ <div className="text-xs text-neutral-400">
+ {startTime.toRelative()}
+ </div>
+ {duration && (
+ <div className="flex items-center gap-1 text-neutral-400 text-xs">
+ <Clock className="w-3 h-3" />
+ {formatDuration(duration)}
+ </div>
+ )}
+ </div>
+
+ <div className="text-xs text-neutral-200 truncate mb-2">
+ {replay.page_url
+ .replace("https://", "")
+ .replace("http://", "")
+ .replace("www.", "")}
+ </div>
+
+ <div className="flex items-center gap-2 text-sm text-neutral-400">
+ <CountryFlagTooltipIcon
+ country={replay.country}
+ city={replay.city}
+ region={replay.region}
+ />
+ <BrowserTooltipIcon
+ browser={replay.browser}
+ browser_version={replay.browser_version}
+ />
+ <OperatingSystemTooltipIcon
+ operating_system={replay.operating_system}
+ operating_system_version={replay.operating_system_version}
+ />
+ <DeviceTypeTooltipIcon
+ device_type={replay.device_type}
+ screen_width={replay.screen_width}
+ screen_height={replay.screen_height}
+ />
+
+ <Badge
+ variant="outline"
+ className="flex items-center gap-1 bg-neutral-800 text-gray-300"
+ >
+ <MousePointerClick className="w-4 h-4 text-amber-500" />
+ <span>{formatter(replay.event_count)}</span>
+ </Badge>
+ </div>
+ </div>
+ );
+}
+
+export function ReplayCardSkeleton() {
+ return (
+ <div className="bg-neutral-900 border-b border-neutral-800 p-3 hover:bg-neutral-800/80 transition-colors">
+ {/* Time and duration row */}
+ <div className="flex items-center gap-2 mb-1">
+ <Skeleton className="h-3 w-16" />
+ <Skeleton className="h-3 w-12" />
+ </div>
+
+ {/* URL row */}
+ <div className="mb-2">
+ <Skeleton className="h-3 w-40" />
+ </div>
+
+ {/* Icons and event count row */}
+ <div className="flex items-center gap-2">
+ <Skeleton className="h-4 w-4 rounded" />
+ <Skeleton className="h-4 w-4 rounded" />
+ <Skeleton className="h-4 w-4 rounded" />
+ <Skeleton className="h-4 w-4 rounded" />
+ <Skeleton className="h-6 w-12 rounded" />
+ </div>
+ </div>
+ );
+}
diff --git a/client/src/app/[site]/replay/components/ReplayList.tsx b/client/src/app/[site]/replay/components/ReplayList.tsx
new file mode 100644
index 000000000..c67c9e855
--- /dev/null
+++ b/client/src/app/[site]/replay/components/ReplayList.tsx
@@ -0,0 +1,109 @@
+import { useEffect, useMemo } from "react";
+import { useIntersectionObserver } from "@uidotdev/usehooks";
+import { Loader2 } from "lucide-react";
+import { NothingFound } from "../../../../components/NothingFound";
+import { ReplayCard, ReplayCardSkeleton } from "./ReplayCard";
+import {
+ useGetSessionReplays,
+ SessionReplayListItem,
+} from "../../../../api/analytics/sessionReplay/useGetSessionReplays";
+import { useReplayStore } from "./replayStore";
+import { ScrollArea } from "../../../../components/ui/scroll-area";
+
+export function ReplayList() {
+ const { sessionId, setSessionId } = useReplayStore();
+
+ const {
+ data,
+ isLoading,
+ error,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ } = useGetSessionReplays();
+
+ // Use the intersection observer hook for infinite scroll
+ const [ref, entry] = useIntersectionObserver({
+ threshold: 0,
+ root: null,
+ rootMargin: "0px 0px 200px 0px", // Load more when user is 200px from the bottom
+ });
+
+ const flattenedData = useMemo(() => {
+ if (!data) return [];
+ return data.pages.flatMap((page) => page.data || []);
+ }, [data]);
+
+ useEffect(() => {
+ if (flattenedData.length > 0 && !sessionId) {
+ setSessionId(flattenedData[0].session_id);
+ }
+ }, [flattenedData]);
+
+ // Fetch next page when intersection observer detects the target is visible
+ useEffect(() => {
+ if (
+ entry?.isIntersecting &&
+ hasNextPage &&
+ !isFetchingNextPage &&
+ !isLoading
+ ) {
+ fetchNextPage();
+ }
+ }, [
+ entry?.isIntersecting,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ isLoading,
+ ]);
+
+ if (error) {
+ return (
+ <div className="text-red-500 p-4">Error: {(error as Error).message}</div>
+ );
+ }
+
+ return (
+ <div className="rounded-lg border border-neutral-800 flex flex-col">
+ <ScrollArea className="h-[calc(100vh-123px)]">
+ {isLoading ? (
+ Array.from({ length: 20 }).map((_, index) => (
+ <ReplayCardSkeleton key={`loading-${index}`} />
+ ))
+ ) : flattenedData.length === 0 ? (
+ <NothingFound
+ title={"No session replays found"}
+ description={"Try a different date range or filter"}
+ />
+ ) : (
+ <>
+ {flattenedData.map((replay: SessionReplayListItem, index) => (
+ <ReplayCard
+ key={`${replay.session_id}-${index}`}
+ replay={replay}
+ />
+ ))}
+
+ {/* Infinite scroll anchor and loading indicator */}
+ <div ref={ref} className="py-3 flex justify-center">
+ {isFetchingNextPage && (
+ <div className="flex items-center gap-2 text-neutral-400 text-xs">
+ <Loader2 className="h-4 w-4 animate-spin" />
+ Loading more replays...
+ </div>
+ )}
+ {!hasNextPage &&
+ !isFetchingNextPage &&
+ flattenedData.length > 0 && (
+ <div className="text-neutral-500 text-xs">
+ All replays loaded
+ </div>
+ )}
+ </div>
+ </>
+ )}
+ </ScrollArea>
+ </div>
+ );
+}
diff --git a/client/src/app/[site]/replay/components/ReplayPlayer.tsx b/client/src/app/[site]/replay/components/ReplayPlayer.tsx
new file mode 100644
index 000000000..742b02944
--- /dev/null
+++ b/client/src/app/[site]/replay/components/ReplayPlayer.tsx
@@ -0,0 +1,380 @@
+import { Pause, Play } from "lucide-react";
+import { useParams } from "next/navigation";
+import { useEffect, useRef, useState } from "react";
+import rrwebPlayer from "rrweb-player";
+import "rrweb-player/dist/style.css";
+import { useGetSessionReplayEvents } from "../../../../api/analytics/sessionReplay/useGetSessionReplayEvents";
+import { ThreeDotLoader } from "../../../../components/Loaders";
+import { ActivitySlider } from "../../../../components/ui/activity-slider";
+import { Button } from "../../../../components/ui/button";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "../../../../components/ui/select";
+import { useReplayStore } from "./replayStore";
+
+export function ReplayPlayer({
+ width,
+ height,
+}: {
+ width: number;
+ height: number;
+}) {
+ const params = useParams();
+ const siteId = Number(params.site);
+ const {
+ sessionId,
+ player,
+ setPlayer,
+ isPlaying,
+ setIsPlaying,
+ currentTime,
+ setCurrentTime,
+ duration,
+ setDuration,
+ playbackSpeed,
+ setPlaybackSpeed,
+ activityPeriods,
+ setActivityPeriods,
+ resetPlayerState,
+ } = useReplayStore();
+
+ const playerContainerRef = useRef<HTMLDivElement>(null);
+
+ // State for play/pause overlay animation
+ const [showPlayPauseOverlay, setShowPlayPauseOverlay] = useState(false);
+ const [overlayIcon, setOverlayIcon] = useState<"play" | "pause">("play");
+ const overlayTimeoutRef = useRef<number | undefined>(undefined);
+
+ const { data, isLoading, error } = useGetSessionReplayEvents(
+ siteId,
+ sessionId
+ );
+
+ // Reset player state when session changes
+ useEffect(() => {
+ resetPlayerState();
+ }, [sessionId, resetPlayerState]);
+
+ // Cleanup overlay timeout on unmount
+ useEffect(() => {
+ return () => {
+ if (overlayTimeoutRef.current) {
+ clearTimeout(overlayTimeoutRef.current);
+ }
+ };
+ }, []);
+
+ useEffect(() => {
+ if (data?.events && playerContainerRef.current) {
+ // Clear any existing content first
+ playerContainerRef.current.innerHTML = "";
+
+ let newPlayer: any = null;
+
+ try {
+ // Initialize rrweb player
+ newPlayer = new rrwebPlayer({
+ target: playerContainerRef.current,
+ props: {
+ events: data.events as any, // Cast to any to handle type compatibility with rrweb
+ width: width,
+ // subtract for the custom controls
+ height: height - 67,
+ autoPlay: false,
+ showController: false, // We'll use custom controls
+ },
+ });
+
+ setPlayer(newPlayer);
+
+ // Set up event listeners
+ newPlayer.addEventListener("ui-update-current-time", (event: any) => {
+ setCurrentTime(event.payload);
+ });
+
+ newPlayer.addEventListener("ui-update-player-state", (event: any) => {
+ setIsPlaying(event.payload === "playing");
+ });
+
+ newPlayer.addEventListener("ui-update-duration", (event: any) => {
+ setDuration(event.payload);
+ });
+
+ // Get the initial duration from the player
+ setTimeout(() => {
+ const playerDuration = newPlayer.getMetaData().totalTime;
+ if (playerDuration) {
+ setDuration(playerDuration);
+ }
+ }, 100);
+
+ // Calculate activity periods after we have duration
+ setTimeout(() => {
+ if (!data.events || data.events.length === 0) return;
+
+ const totalDuration = newPlayer.getMetaData().totalTime || 0;
+
+ // Filter for user interaction events (mouse moves, clicks, etc.)
+ const interactionEvents = data.events.filter((event) => {
+ const eventType = parseInt(event.type.toString());
+ // Type 3 = IncrementalSnapshot (includes mouse moves, clicks, etc.)
+ return eventType === 3;
+ });
+
+ const periods: { start: number; end: number }[] = [];
+ const inactivityThreshold = 5000; // 5 seconds of no interaction = inactive
+ const firstEventTime = data.events[0].timestamp;
+
+ for (let i = 0; i < interactionEvents.length; i++) {
+ const currentEvent = interactionEvents[i];
+ const nextEvent = interactionEvents[i + 1];
+
+ const currentTime = currentEvent.timestamp - firstEventTime;
+ const nextTime = nextEvent
+ ? nextEvent.timestamp - firstEventTime
+ : totalDuration;
+
+ if (nextTime - currentTime <= inactivityThreshold) {
+ periods.push({
+ start: currentTime,
+ end: nextTime,
+ });
+ }
+ }
+
+ setActivityPeriods(periods);
+ }, 150); // Run after duration is set
+ } catch (error) {
+ console.error("Failed to initialize rrweb player:", error);
+ return;
+ }
+
+ return () => {
+ // Proper cleanup
+ if (newPlayer) {
+ newPlayer.pause();
+ }
+ if (playerContainerRef.current) {
+ playerContainerRef.current.innerHTML = "";
+ }
+ setPlayer(null);
+ };
+ }
+ }, [data]);
+
+ const handlePlayPause = () => {
+ if (!player) return;
+
+ const newPlayingState = !isPlaying;
+
+ if (isPlaying) {
+ player.pause();
+ } else {
+ player.play();
+ }
+ setIsPlaying(newPlayingState);
+
+ // Show overlay animation
+ setOverlayIcon(newPlayingState ? "pause" : "play");
+ setShowPlayPauseOverlay(true);
+
+ // Clear existing timeout
+ if (overlayTimeoutRef.current) {
+ clearTimeout(overlayTimeoutRef.current);
+ }
+
+ // Hide overlay after animation
+ overlayTimeoutRef.current = window.setTimeout(() => {
+ setShowPlayPauseOverlay(false);
+ }, 800);
+ };
+
+ const handleSkipBack = () => {
+ if (!player) return;
+ const newTime = Math.max(0, currentTime - 10000); // Skip back 10 seconds
+ player.goto(newTime);
+ };
+
+ const handleSkipForward = () => {
+ if (!player) return;
+ const newTime = Math.min(duration, currentTime + 10000); // Skip forward 10 seconds
+ player.goto(newTime);
+ };
+
+ const handleSliderChange = (value: number[]) => {
+ if (!player || !duration) return;
+
+ // Pause the player when user scrubs manually
+ player.pause();
+ setIsPlaying(false);
+
+ const newTime = (value[0] / 100) * duration;
+ player.goto(newTime);
+ setCurrentTime(newTime);
+ };
+
+ const handleSpeedChange = (speed: string) => {
+ if (!player) return;
+ setPlaybackSpeed(speed);
+ player.setSpeed(parseFloat(speed));
+ };
+
+ const handleFullscreen = () => {
+ if (playerContainerRef.current) {
+ if (document.fullscreenElement) {
+ document.exitFullscreen();
+ } else {
+ playerContainerRef.current.requestFullscreen();
+ }
+ }
+ };
+
+ const formatTime = (ms: number) => {
+ const totalSeconds = Math.floor(ms / 1000);
+ const minutes = Math.floor(totalSeconds / 60);
+ const seconds = totalSeconds % 60;
+ return `${minutes}:${seconds.toString().padStart(2, "0")}`;
+ };
+
+ // Add keyboard shortcuts
+ useEffect(() => {
+ const handleKeyDown = (event: KeyboardEvent) => {
+ // Only handle hotkeys when the player exists and focus is not on an input/textarea
+ if (
+ !player ||
+ event.target instanceof HTMLInputElement ||
+ event.target instanceof HTMLTextAreaElement
+ ) {
+ return;
+ }
+
+ switch (event.key) {
+ case "ArrowLeft":
+ event.preventDefault();
+ handleSkipBack();
+ break;
+ case "ArrowRight":
+ event.preventDefault();
+ handleSkipForward();
+ break;
+ case " ":
+ event.preventDefault();
+ handlePlayPause();
+ break;
+ }
+ };
+
+ document.addEventListener("keydown", handleKeyDown);
+
+ return () => {
+ document.removeEventListener("keydown", handleKeyDown);
+ };
+ }, [player, handleSkipBack, handleSkipForward, handlePlayPause]);
+
+ if (error) {
+ return (
+ <div className="flex flex-col items-center justify-center h-full p-8">
+ <div className="text-red-500 mb-4">
+ Error loading replay: {(error as Error).message}
+ </div>
+ </div>
+ );
+ }
+
+ if (isLoading || !data) {
+ return (
+ <div className="bg-black h-full flex items-center justify-center">
+ <ThreeDotLoader className="w-full" />
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex flex-col bg-neutral-950">
+ {/* Player Container */}
+ <div className="flex-1 flex items-center justify-center overflow-hidden relative">
+ <div
+ ref={playerContainerRef}
+ className="w-full bg-black rounded-lg shadow-2xl [&_.rr-player]:!bg-black cursor-pointer"
+ style={{
+ position: "relative",
+ }}
+ onClick={handlePlayPause}
+ />
+
+ {/* Play/Pause Overlay Animation */}
+ {showPlayPauseOverlay && (
+ <div className="absolute inset-0 flex items-center justify-center pointer-events-none">
+ <div className="bg-black/60 rounded-full p-6 animate-in fade-in zoom-in-50 duration-200">
+ {overlayIcon === "play" ? (
+ <Play className="w-12 h-12 text-white" fill="currentColor" />
+ ) : (
+ <Pause className="w-12 h-12 text-white" fill="currentColor" />
+ )}
+ </div>
+ </div>
+ )}
+ </div>
+
+ {/* Custom Controls */}
+ <div className="border border-neutral-800 p-2 pb-3 bg-neutral-900 rounded-b-lg pt-6">
+ <div className="flex items-center">
+ <Button
+ variant="ghost"
+ size="smIcon"
+ onClick={handlePlayPause}
+ disabled={!player}
+ >
+ {isPlaying ? (
+ <Pause className="w-4 h-4" fill="currentColor" />
+ ) : (
+ <Play className="w-4 h-4" fill="currentColor" />
+ )}
+ </Button>
+ <div className="flex-1 mx-2 -mt-8">
+ <ActivitySlider
+ value={[duration > 0 ? (currentTime / duration) * 100 : 0]}
+ onValueChange={handleSliderChange}
+ max={100}
+ step={0.1}
+ activityPeriods={activityPeriods}
+ duration={duration}
+ events={data?.events || []}
+ className="w-full"
+ />
+ </div>
+ <div className="text-xs text-neutral-300 w-20 text-center">
+ {formatTime(currentTime)} / {formatTime(duration)}
+ </div>
+
+ <Select value={playbackSpeed} onValueChange={handleSpeedChange}>
+ <SelectTrigger size="sm" className="w-14 mx-2">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent size="sm">
+ <SelectItem value="0.25" size="sm">
+ 0.25x
+ </SelectItem>
+ <SelectItem value="0.5" size="sm">
+ 0.5x
+ </SelectItem>
+ <SelectItem value="1" size="sm">
+ 1x
+ </SelectItem>
+ <SelectItem value="2" size="sm">
+ 2x
+ </SelectItem>
+ <SelectItem value="4" size="sm">
+ 4x
+ </SelectItem>
+ </SelectContent>
+ </Select>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/client/src/app/[site]/replay/components/replayStore.ts b/client/src/app/[site]/replay/components/replayStore.ts
new file mode 100644
index 000000000..8edfaaf1f
--- /dev/null
+++ b/client/src/app/[site]/replay/components/replayStore.ts
@@ -0,0 +1,69 @@
+import { create } from "zustand";
+
+interface ActivityPeriod {
+ start: number;
+ end: number;
+}
+
+export const useReplayStore = create<{
+ // Session selection
+ sessionId: string;
+ setSessionId: (sessionId: string) => void;
+
+ // Player state
+ player: any;
+ setPlayer: (player: any) => void;
+
+ // Playback state
+ isPlaying: boolean;
+ setIsPlaying: (isPlaying: boolean) => void;
+
+ currentTime: number;
+ setCurrentTime: (currentTime: number) => void;
+
+ duration: number;
+ setDuration: (duration: number) => void;
+
+ playbackSpeed: string;
+ setPlaybackSpeed: (speed: string) => void;
+
+ activityPeriods: ActivityPeriod[];
+ setActivityPeriods: (periods: ActivityPeriod[]) => void;
+
+ // Reset all player state when session changes
+ resetPlayerState: () => void;
+}>((set) => ({
+ // Session selection
+ sessionId: "",
+ setSessionId: (sessionId) => set({ sessionId }),
+
+ // Player state
+ player: null,
+ setPlayer: (player) => set({ player }),
+
+ // Playback state
+ isPlaying: false,
+ setIsPlaying: (isPlaying) => set({ isPlaying }),
+
+ currentTime: 0,
+ setCurrentTime: (currentTime) => set({ currentTime }),
+
+ duration: 0,
+ setDuration: (duration) => set({ duration }),
+
+ playbackSpeed: "1",
+ setPlaybackSpeed: (playbackSpeed) => set({ playbackSpeed }),
+
+ activityPeriods: [],
+ setActivityPeriods: (activityPeriods) => set({ activityPeriods }),
+
+ // Reset all player state when session changes
+ resetPlayerState: () => set({
+ player: null,
+ isPlaying: false,
+ currentTime: 0,
+ duration: 0,
+ playbackSpeed: "1",
+ activityPeriods: [],
+ }),
+}));
diff --git a/client/src/app/[site]/replay/page.tsx b/client/src/app/[site]/replay/page.tsx
new file mode 100644
index 000000000..1b5d756ce
--- /dev/null
+++ b/client/src/app/[site]/replay/page.tsx
@@ -0,0 +1,50 @@
+"use client";
+
+import { useMeasure } from "@uidotdev/usehooks";
+import { useGetSessionReplays } from "../../../api/analytics/sessionReplay/useGetSessionReplays";
+import { DisabledOverlay } from "../../../components/DisabledOverlay";
+import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
+import { SESSION_REPLAY_PAGE_FILTERS } from "../../../lib/store";
+import { SubHeader } from "../components/SubHeader/SubHeader";
+import { EnableSessionReplay } from "./components/EnableSessionReplay";
+import { ReplayList } from "./components/ReplayList";
+import { ReplayPlayer } from "./components/ReplayPlayer";
+import { NothingFound } from "../../../components/NothingFound";
+import { ReplayBreadcrumbs } from "./components/ReplayBreadcrumbs";
+
+export default function SessionReplayPage() {
+ useSetPageTitle("Rybbit · Session Replay");
+
+ const { data, isLoading } = useGetSessionReplays();
+
+ const hasNoReplays = !isLoading && !data?.pages[0].data?.length;
+
+ const [ref, { height: resolvedHeight, width: resolvedWidth }] = useMeasure();
+
+ return (
+ <DisabledOverlay message="Replay">
+ <div className="p-2 md:p-4 max-w-[2000px] mx-auto flex flex-col gap-1 overflow-hidden h-full">
+ <SubHeader availableFilters={SESSION_REPLAY_PAGE_FILTERS} />
+ <EnableSessionReplay />
+ {hasNoReplays ? (
+ <NothingFound
+ title={"No session replays found"}
+ description={
+ "Replays will appear here once session replay is enabled."
+ }
+ />
+ ) : (
+ <div className="grid grid-cols-[200px_1fr_300px] gap-3">
+ <ReplayList />
+ <div ref={ref} className="w-[calc(min(100vw, 2000px)-780px)]">
+ {resolvedWidth && resolvedHeight && (
+ <ReplayPlayer width={resolvedWidth} height={resolvedHeight} />
+ )}
+ </div>
+ <ReplayBreadcrumbs />
+ </div>
+ )}
+ </div>
+ </DisabledOverlay>
+ );
+}
diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsx
index 4569ced9f..4b30a84d7 100644
--- a/client/src/components/Sessions/SessionCard.tsx
+++ b/client/src/components/Sessions/SessionCard.tsx
@@ -9,22 +9,21 @@ import {
ChevronDown,
ChevronRight,
FileText,
- Laptop,
MousePointerClick,
- Smartphone,
} from "lucide-react";
-import { DateTime, Duration } from "luxon";
+import { DateTime } from "luxon";
import { memo, useState } from "react";
import { GetSessionsResponse } from "../../api/analytics/userSessions";
-import { Browser } from "../../app/[site]/components/shared/icons/Browser";
-import { CountryFlag } from "../../app/[site]/components/shared/icons/CountryFlag";
-import { OperatingSystem } from "../../app/[site]/components/shared/icons/OperatingSystem";
-import { cn, formatter, getCountryName } from "../../lib/utils";
-import { formatDuration } from "../../lib/dateTimeUtils";
+import { formatDuration, hour12, userLocale } from "../../lib/dateTimeUtils";
+import { cn, formatter } from "../../lib/utils";
+import {
+ BrowserTooltipIcon,
+ CountryFlagTooltipIcon,
+ DeviceTypeTooltipIcon,
+ OperatingSystemTooltipIcon,
+} from "../TooltipIcons/TooltipIcons";
import { Badge } from "../ui/badge";
import { SessionDetails } from "./SessionDetails";
-import { userLocale, hour12 } from "../../lib/dateTimeUtils";
-import { useGetRegionName } from "../../lib/geo";
interface SessionCardProps {
session: GetSessionsResponse[number];
@@ -32,17 +31,6 @@ interface SessionCardProps {
onClick?: () => void;
}
-// DeviceIcon component for displaying mobile/desktop icons
-function DeviceIcon({ deviceType }: { deviceType: string }) {
- const type = deviceType.toLowerCase();
-
- if (type.includes("mobile") || type.includes("tablet")) {
- return <Smartphone className="w-4 h-4" />;
- }
-
- return <Laptop className="w-4 h-4" />;
-}
-
// Function to truncate path for display
function truncatePath(path: string, maxLength: number = 32) {
if (!path) return "-";
@@ -53,8 +41,6 @@ function truncatePath(path: string, maxLength: number = 32) {
}
export function SessionCard({ session, onClick, userId }: SessionCardProps) {
- const { getRegionName } = useGetRegionName();
-
const [expanded, setExpanded] = useState(false);
// Calculate session duration in minutes
@@ -74,20 +60,6 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
}
};
- const getFullLocation = (session: GetSessionsResponse[number]) => {
- let location = "";
- if (session.city) {
- location += `${session.city}, `;
- }
- if (getRegionName(session.region)) {
- location += `${getRegionName(session.region)}, `;
- }
- if (session.country) {
- location += getCountryName(session.country);
- }
- return location;
- };
-
return (
<div className="mb-3 rounded-lg bg-neutral-900 border border-neutral-800 overflow-hidden">
<div className="p-3 cursor-pointer" onClick={handleCardClick}>
@@ -100,68 +72,26 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
{/* Icons section */}
<div className="flex space-x-2 items-center md:ml-3">
- {/* Country */}
{session.country && (
- <Tooltip>
- <TooltipTrigger asChild>
- <div className="flex items-center">
- <CountryFlag country={session.country} />
- </div>
- </TooltipTrigger>
- <TooltipContent>
- <p>{getFullLocation(session)}</p>
- </TooltipContent>
- </Tooltip>
+ <CountryFlagTooltipIcon
+ country={session.country}
+ city={session.city}
+ region={session.region}
+ />
)}
-
- {/* Browser */}
- <Tooltip>
- <TooltipTrigger asChild>
- <div className="flex-shrink-0">
- <Browser browser={session.browser || "Unknown"} />
- </div>
- </TooltipTrigger>
- <TooltipContent>
- <p>
- {session.browser || "Unknown browser"}
- {session.browser_version && ` ${session.browser_version}`}
- </p>
- </TooltipContent>
- </Tooltip>
-
- {/* OS */}
- <Tooltip>
- <TooltipTrigger asChild>
- <div className="flex-shrink-0">
- <OperatingSystem os={session.operating_system || ""} />
- </div>
- </TooltipTrigger>
- <TooltipContent>
- <p>
- {session.operating_system || "Unknown OS"}
- {session.operating_system_version &&
- ` ${session.operating_system_version}`}
- </p>
- </TooltipContent>
- </Tooltip>
-
- {/* Device Type */}
- <Tooltip>
- <TooltipTrigger asChild>
- <div>
- <DeviceIcon deviceType={session.device_type || ""} />
- </div>
- </TooltipTrigger>
- <TooltipContent>
- <p>
- {session.device_type || "Unknown device"}
- {session.screen_width &&
- session.screen_height &&
- ` ${session.screen_width}x${session.screen_height}`}
- </p>
- </TooltipContent>
- </Tooltip>
-
+ <BrowserTooltipIcon
+ browser={session.browser || "Unknown"}
+ browser_version={session.browser_version}
+ />
+ <OperatingSystemTooltipIcon
+ operating_system={session.operating_system || ""}
+ operating_system_version={session.operating_system_version}
+ />
+ <DeviceTypeTooltipIcon
+ device_type={session.device_type || ""}
+ screen_width={session.screen_width}
+ screen_height={session.screen_height}
+ />
<Tooltip>
<TooltipTrigger asChild>
<Badge
diff --git a/client/src/components/SiteSettings/ScriptBuilder.tsx b/client/src/components/SiteSettings/ScriptBuilder.tsx
index e6227021a..43051b15e 100644
--- a/client/src/components/SiteSettings/ScriptBuilder.tsx
+++ b/client/src/components/SiteSettings/ScriptBuilder.tsx
@@ -19,6 +19,7 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
const [trackQuery, setTrackQuery] = useState(true);
const [trackOutbound, setTrackOutbound] = useState(true);
const [trackErrors, setTrackErrors] = useState(false);
+ const [sessionReplay, setSessionReplay] = useState(false);
const [webVitals, setWebVitals] = useState(false);
const [skipPatterns, setSkipPatterns] = useState<string[]>([]);
const [skipPatternsText, setSkipPatternsText] = useState("");
@@ -116,6 +117,11 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
? `
data-track-errors="true"`
: ""
+ }${
+ sessionReplay
+ ? `
+ data-session-replay="true"`
+ : ""
}${
skipPatterns.length > 0
? `
@@ -218,24 +224,25 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
</div>
</div>
- {/* Track Errors Option */}
+ {/* Session Replay Option */}
<div className="space-y-2">
<div className="flex items-center justify-between">
<div>
<Label
- htmlFor="trackErrors"
+ htmlFor="sessionReplay"
className="text-sm font-medium text-foreground block"
>
- Track JavaScript errors
+ Enable session replay
</Label>
<p className="text-xs text-muted-foreground mt-1">
- Automatically capture and track JavaScript errors on your site
+ Record user interactions and DOM changes for debugging and UX
+ analysis
</p>
</div>
<Switch
- id="trackErrors"
- checked={trackErrors}
- onCheckedChange={setTrackErrors}
+ id="sessionReplay"
+ checked={sessionReplay}
+ onCheckedChange={setSessionReplay}
/>
</div>
</div>
@@ -285,6 +292,28 @@ export function ScriptBuilder({ siteId }: ScriptBuilderProps) {
</div>
</div>
+ {/* Track Errors Option */}
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <div>
+ <Label
+ htmlFor="trackErrors"
+ className="text-sm font-medium text-foreground block"
+ >
+ Track JavaScript errors
+ </Label>
+ <p className="text-xs text-muted-foreground mt-1">
+ Automatically capture and track JavaScript errors on your site
+ </p>
+ </div>
+ <Switch
+ id="trackErrors"
+ checked={trackErrors}
+ onCheckedChange={setTrackErrors}
+ />
+ </div>
+ </div>
+
{/* Skip Patterns Option */}
<div className="space-y-2">
<div>
diff --git a/client/src/components/TooltipIcons/TooltipIcons.tsx b/client/src/components/TooltipIcons/TooltipIcons.tsx
new file mode 100644
index 000000000..90e735523
--- /dev/null
+++ b/client/src/components/TooltipIcons/TooltipIcons.tsx
@@ -0,0 +1,133 @@
+import { Laptop, Smartphone } from "lucide-react";
+import { Browser } from "../../app/[site]/components/shared/icons/Browser";
+import { CountryFlag } from "../../app/[site]/components/shared/icons/CountryFlag";
+import { OperatingSystem } from "../../app/[site]/components/shared/icons/OperatingSystem";
+import { getRegionName } from "../../lib/geo";
+import { getCountryName } from "../../lib/utils";
+import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
+
+// DeviceIcon component for displaying mobile/desktop icons
+function DeviceIcon({ deviceType }: { deviceType: string }) {
+ const type = deviceType.toLowerCase();
+
+ if (type.includes("mobile") || type.includes("tablet")) {
+ return <Smartphone className="w-4 h-4" />;
+ }
+
+ return <Laptop className="w-4 h-4" />;
+}
+
+export function CountryFlagTooltipIcon({
+ country,
+ city,
+ region,
+}: {
+ country: string;
+ city: string;
+ region: string;
+}) {
+ const getFullLocation = <
+ T extends { country: string; city: string; region: string }
+ >(
+ session: T
+ ) => {
+ let location = "";
+ if (session.city) {
+ location += `${session.city}, `;
+ }
+ if (getRegionName(session.region)) {
+ location += `${getRegionName(session.region)}, `;
+ }
+ if (session.country) {
+ location += getCountryName(session.country);
+ }
+ return location;
+ };
+
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <div className="flex items-center">
+ <CountryFlag country={country} />
+ </div>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>{getFullLocation({ country, city, region })}</p>
+ </TooltipContent>
+ </Tooltip>
+ );
+}
+
+export function BrowserTooltipIcon({
+ browser,
+ browser_version,
+}: {
+ browser: string;
+ browser_version?: string;
+}) {
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <div className="flex-shrink-0">
+ <Browser browser={browser || "Unknown"} />
+ </div>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>
+ {browser || "Unknown browser"}
+ {browser_version && ` ${browser_version}`}
+ </p>
+ </TooltipContent>
+ </Tooltip>
+ );
+}
+
+export function OperatingSystemTooltipIcon({
+ operating_system,
+ operating_system_version,
+}: {
+ operating_system: string;
+ operating_system_version?: string;
+}) {
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <div className="flex-shrink-0">
+ <OperatingSystem os={operating_system || ""} />
+ </div>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>
+ {operating_system || "Unknown OS"}
+ {operating_system_version && ` ${operating_system_version}`}
+ </p>
+ </TooltipContent>
+ </Tooltip>
+ );
+}
+
+export function DeviceTypeTooltipIcon({
+ device_type,
+ screen_width,
+ screen_height,
+}: {
+ device_type: string;
+ screen_width?: number;
+ screen_height?: number;
+}) {
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <div>
+ <DeviceIcon deviceType={device_type || ""} />
+ </div>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>
+ {device_type || "Unknown device"}
+ {screen_width && screen_height && ` ${screen_width}x${screen_height}`}
+ </p>
+ </TooltipContent>
+ </Tooltip>
+ );
+}
diff --git a/client/src/components/ui/activity-slider.tsx b/client/src/components/ui/activity-slider.tsx
new file mode 100644
index 000000000..a8d15dd5c
--- /dev/null
+++ b/client/src/components/ui/activity-slider.tsx
@@ -0,0 +1,110 @@
+"use client";
+
+import * as React from "react";
+import * as SliderPrimitive from "@radix-ui/react-slider";
+
+import { cn } from "@/lib/utils";
+
+interface ActivityPeriod {
+ start: number;
+ end: number;
+}
+
+interface ActivitySliderProps
+ extends React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> {
+ activityPeriods?: ActivityPeriod[];
+ duration?: number;
+ events?: Array<{ timestamp: number; type: string | number }>;
+}
+
+const ActivitySlider = React.forwardRef<
+ React.ElementRef<typeof SliderPrimitive.Root>,
+ ActivitySliderProps
+>(({ className, activityPeriods = [], duration = 100, events = [], ...props }, ref) => {
+ const firstEventTime = events.length > 0 ? events[0].timestamp : 0;
+
+ return (
+ <div className="w-full">
+ {/* Event Timeline */}
+ <div className="relative h-6 w-full mb-2">
+ {events.map((event, index) => {
+ const relativeTime = event.timestamp - firstEventTime;
+ const position = duration > 0 ? (relativeTime / duration) * 100 : 0;
+
+ // Different colors for different event types
+ const getEventColor = (type: string | number) => {
+ const eventType = parseInt(type.toString());
+ switch (eventType) {
+ case 2: return "bg-blue-500"; // FullSnapshot
+ case 3: return "bg-yellow-500"; // IncrementalSnapshot
+ case 4: return "bg-purple-500"; // Meta
+ default: return "bg-gray-500";
+ }
+ };
+
+ // Calculate margins to prevent overflow
+ // Event bubble is 8px (w-2), so we need 4px margin on each side
+ // This translates to approximately 0.5% margin on typical screen widths
+ const leftMargin = 0.5;
+ const rightMargin = 0.5;
+ const constrainedPosition = Math.max(leftMargin, Math.min(100 - rightMargin, position));
+
+ return (
+ <div
+ key={index}
+ className={cn(
+ "absolute w-2 h-2 rounded-full transform -translate-x-1/2",
+ getEventColor(event.type)
+ )}
+ style={{
+ left: `${constrainedPosition}%`,
+ top: "50%",
+ transform: "translate(-50%, -50%)",
+ }}
+ title={`Event ${event.type} at ${new Date(event.timestamp).toLocaleTimeString()}`}
+ />
+ );
+ })}
+ </div>
+
+ {/* Activity Slider */}
+ <SliderPrimitive.Root
+ ref={ref}
+ className={cn(
+ "relative flex w-full touch-none select-none items-center",
+ className
+ )}
+ {...props}
+ >
+ <SliderPrimitive.Track className="relative h-1.5 w-full grow overflow-hidden rounded-full bg-neutral-800">
+ {/* Inactive background */}
+ <div className="absolute h-full w-full bg-neutral-700" />
+
+ {/* Activity periods */}
+ {activityPeriods.map((period, index) => {
+ const startPercent = duration > 0 ? (period.start / duration) * 100 : 0;
+ const widthPercent = duration > 0 ? ((period.end - period.start) / duration) * 100 : 0;
+
+ return (
+ <div
+ key={index}
+ className="absolute h-full bg-neutral-600"
+ style={{
+ left: `${startPercent}%`,
+ width: `${widthPercent}%`,
+ }}
+ />
+ );
+ })}
+
+ {/* Progress range */}
+ <SliderPrimitive.Range className="absolute h-full bg-green-500" />
+ </SliderPrimitive.Track>
+ <SliderPrimitive.Thumb className="block h-4 w-4 rounded-full border border-green-500 bg-white shadow transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-green-500 disabled:pointer-events-none disabled:opacity-50 dark:border-green-500 dark:bg-white" />
+ </SliderPrimitive.Root>
+ </div>
+ );
+});
+ActivitySlider.displayName = "ActivitySlider";
+
+export { ActivitySlider };
\ No newline at end of file
diff --git a/client/src/components/ui/scroll-area.tsx b/client/src/components/ui/scroll-area.tsx
new file mode 100644
index 000000000..e6c144af5
--- /dev/null
+++ b/client/src/components/ui/scroll-area.tsx
@@ -0,0 +1,58 @@
+"use client";
+
+import * as React from "react";
+import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";
+
+import { cn } from "@/lib/utils";
+
+function ScrollArea({
+ className,
+ children,
+ ...props
+}: React.ComponentProps<typeof ScrollAreaPrimitive.Root>) {
+ return (
+ <ScrollAreaPrimitive.Root
+ data-slot="scroll-area"
+ className={cn("relative", className)}
+ {...props}
+ >
+ <ScrollAreaPrimitive.Viewport
+ data-slot="scroll-area-viewport"
+ className="focus-visible:ring-ring/50 size-full rounded-[inherit] transition-[color,box-shadow] outline-none focus-visible:ring-[3px] focus-visible:outline-1"
+ >
+ {children}
+ </ScrollAreaPrimitive.Viewport>
+ <ScrollBar />
+ <ScrollAreaPrimitive.Corner />
+ </ScrollAreaPrimitive.Root>
+ );
+}
+
+function ScrollBar({
+ className,
+ orientation = "vertical",
+ ...props
+}: React.ComponentProps<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>) {
+ return (
+ <ScrollAreaPrimitive.ScrollAreaScrollbar
+ data-slot="scroll-area-scrollbar"
+ orientation={orientation}
+ className={cn(
+ "flex touch-none p-px transition-colors select-none",
+ orientation === "vertical" &&
+ "h-full w-2.5 border-l border-l-transparent",
+ orientation === "horizontal" &&
+ "h-2.5 flex-col border-t border-t-transparent",
+ className
+ )}
+ {...props}
+ >
+ <ScrollAreaPrimitive.ScrollAreaThumb
+ data-slot="scroll-area-thumb"
+ className="bg-border relative flex-1 rounded-full"
+ />
+ </ScrollAreaPrimitive.ScrollAreaScrollbar>
+ );
+}
+
+export { ScrollArea, ScrollBar };
diff --git a/client/src/lib/geo.ts b/client/src/lib/geo.ts
index dd79fa291..2ca88fb14 100644
--- a/client/src/lib/geo.ts
+++ b/client/src/lib/geo.ts
@@ -1,66 +1,49 @@
-import { useQuery } from "@tanstack/react-query";
+import { useGeoStore, getSubdivisions, getCountries } from "./geoStore";
-const countriesGeoUrl = "/countries.json";
-const subdivisionsGeoUrl = "/subdivisions.json";
-
-export type Subdivisions = {
- type: string;
- features: Array<{
- type: string;
- properties: {
- name: string;
- iso_3166_2: string;
- admin: string;
- border: number;
- };
- geometry: {
- type: string;
- coordinates: Array<Array<Array<any>>>;
- };
- }>;
-};
-
-export type Country = {
- type: string;
- features: Array<{
- type: string;
- properties: {
- ISO_A2: string;
- ADMIN: string;
- ISO_A3: string;
- BORDER: number;
- };
- geometry: {
- type: string;
- coordinates: Array<Array<Array<any>>>;
- };
- }>;
-};
+// Re-export types for backward compatibility
+export type { Subdivisions, Country } from "./geoStore";
+// Hook-based access (for components that need reactive updates)
export const useSubdivisions = () => {
- return useQuery<Subdivisions>({
- queryKey: ["subdivisions"],
- queryFn: () => fetch(subdivisionsGeoUrl).then((res) => res.json()),
- });
+ const {
+ subdivisions,
+ isLoadingSubdivisions: isLoading,
+ subdivisionsError: error,
+ } = useGeoStore();
+
+ return {
+ data: subdivisions,
+ isLoading,
+ error,
+ };
};
export const useCountries = () => {
- return useQuery<Country>({
- queryKey: ["countries"],
- queryFn: () => fetch(countriesGeoUrl).then((res) => res.json()),
- });
+ const {
+ countries,
+ isLoadingCountries: isLoading,
+ countriesError: error,
+ } = useGeoStore();
+
+ return {
+ data: countries,
+ isLoading,
+ error,
+ };
};
export const useGetRegionName = () => {
- const { data: subdivisions } = useSubdivisions();
+ const { getRegionName } = useGeoStore();
return {
- getRegionName: (region: string) => {
- return (
- subdivisions?.features.find(
- (feature) => feature.properties.iso_3166_2 === region
- )?.properties.name ?? ""
- );
- },
+ getRegionName,
};
};
+
+// Direct access functions (no hooks required)
+export const getRegionName = (region: string) => {
+ return useGeoStore.getState().getRegionName(region);
+};
+
+// Direct access to current data
+export { getSubdivisions, getCountries };
diff --git a/client/src/lib/geoStore.ts b/client/src/lib/geoStore.ts
new file mode 100644
index 000000000..954e6203e
--- /dev/null
+++ b/client/src/lib/geoStore.ts
@@ -0,0 +1,114 @@
+import { create } from "zustand";
+
+const countriesGeoUrl = "/countries.json";
+const subdivisionsGeoUrl = "/subdivisions.json";
+
+export type Subdivisions = {
+ type: string;
+ features: Array<{
+ type: string;
+ properties: {
+ name: string;
+ iso_3166_2: string;
+ admin: string;
+ border: number;
+ };
+ geometry: {
+ type: string;
+ coordinates: Array<Array<Array<any>>>;
+ };
+ }>;
+};
+
+export type Country = {
+ type: string;
+ features: Array<{
+ type: string;
+ properties: {
+ ISO_A2: string;
+ ADMIN: string;
+ ISO_A3: string;
+ BORDER: number;
+ };
+ geometry: {
+ type: string;
+ coordinates: Array<Array<Array<any>>>;
+ };
+ }>;
+};
+
+type GeoStore = {
+ subdivisions: Subdivisions | null;
+ countries: Country | null;
+ isLoadingSubdivisions: boolean;
+ isLoadingCountries: boolean;
+ subdivisionsError: string | null;
+ countriesError: string | null;
+ fetchSubdivisions: () => Promise<void>;
+ fetchCountries: () => Promise<void>;
+ getRegionName: (region: string) => string;
+};
+
+export const useGeoStore = create<GeoStore>((set, get) => ({
+ subdivisions: null,
+ countries: null,
+ isLoadingSubdivisions: false,
+ isLoadingCountries: false,
+ subdivisionsError: null,
+ countriesError: null,
+
+ fetchSubdivisions: async () => {
+ set({ isLoadingSubdivisions: true, subdivisionsError: null });
+ try {
+ const response = await fetch(subdivisionsGeoUrl);
+ if (!response.ok) throw new Error("Failed to fetch subdivisions");
+ const data = await response.json();
+ set({ subdivisions: data, isLoadingSubdivisions: false });
+ } catch (error) {
+ set({
+ subdivisionsError:
+ error instanceof Error ? error.message : "Unknown error",
+ isLoadingSubdivisions: false,
+ });
+ }
+ },
+
+ fetchCountries: async () => {
+ set({ isLoadingCountries: true, countriesError: null });
+ try {
+ const response = await fetch(countriesGeoUrl);
+ if (!response.ok) throw new Error("Failed to fetch countries");
+ const data = await response.json();
+ set({ countries: data, isLoadingCountries: false });
+ } catch (error) {
+ set({
+ countriesError:
+ error instanceof Error ? error.message : "Unknown error",
+ isLoadingCountries: false,
+ });
+ }
+ },
+
+ getRegionName: (region: string) => {
+ const { subdivisions } = get();
+ return (
+ subdivisions?.features.find(
+ (feature) => feature.properties.iso_3166_2 === region
+ )?.properties.name ?? ""
+ );
+ },
+}));
+
+// Auto-fetch data on store initialization
+const store = useGeoStore.getState();
+store.fetchSubdivisions();
+store.fetchCountries();
+
+// Helper functions to get current data
+export const getSubdivisions = () => {
+ return useGeoStore.getState().subdivisions;
+};
+
+export const getCountries = () => {
+ return useGeoStore.getState().countries;
+};
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 53705fbea..698ee23f8 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -14,7 +14,9 @@ export type StatType =
export const SESSION_PAGE_FILTERS: FilterParameter[] = [
"hostname",
"browser",
+ "browser_version",
"operating_system",
+ "operating_system_version",
"language",
"country",
"region",
@@ -32,6 +34,21 @@ export const SESSION_PAGE_FILTERS: FilterParameter[] = [
"utm_content",
];
+export const SESSION_REPLAY_PAGE_FILTERS: FilterParameter[] = [
+ "hostname",
+ "browser",
+ "browser_version",
+ "operating_system",
+ "operating_system_version",
+ "language",
+ "country",
+ "region",
+ "city",
+ "device_type",
+ "referrer",
+ "channel",
+];
+
export const EVENT_FILTERS: FilterParameter[] = [
// "event_name",
// "browser",
@@ -41,7 +58,9 @@ export const EVENT_FILTERS: FilterParameter[] = [
// "referrer",
"hostname",
"browser",
+ "browser_version",
"operating_system",
+ "operating_system_version",
"language",
"country",
"region",
@@ -66,7 +85,9 @@ export const EVENT_FILTERS: FilterParameter[] = [
export const GOALS_PAGE_FILTERS: FilterParameter[] = [
"hostname",
"browser",
+ "browser_version",
"operating_system",
+ "operating_system_version",
"language",
"country",
"region",
@@ -82,7 +103,9 @@ export const GOALS_PAGE_FILTERS: FilterParameter[] = [
export const USER_PAGE_FILTERS: FilterParameter[] = [
"hostname",
"browser",
+ "browser_version",
"operating_system",
+ "operating_system_version",
"language",
"country",
"region",
diff --git a/docs/src/content/script.mdx b/docs/src/content/script.mdx
index 7f5f4157e..a21a6d5c7 100644
--- a/docs/src/content/script.mdx
+++ b/docs/src/content/script.mdx
@@ -22,6 +22,7 @@ You can customize the script's behavior using `data-*` attributes on the `<scrip
| `data-track-query`| `string` | `"true"`| Set to `"false"` to disable tracking of URL query strings. This enhances privacy by preventing potentially sensitive information in query parameters from being stored in your analytics data. |
| `data-track-outbound` | `string` | `"true"` | Set to `"false"` to disable automatic tracking of outbound link clicks. |
| `data-track-errors` | `string` | `"false"` | Set to `"true"` to enable automatic tracking of JavaScript errors and unhandled promise rejections. Only tracks errors from the same origin to avoid noise from third-party scripts. |
+| `data-session-replay` | `string` | `"false"` | Set to `"true"` to enable session replay recording. Captures user interactions, mouse movements, and DOM changes for debugging and user experience analysis. |
| `data-web-vitals` | `string` | `"false"`| Set to `"true"` to enable Web Vitals performance metrics collection (LCP, CLS, INP, FCP, TTFB). Web Vitals are disabled by default to reduce script size and network requests. |
| `data-skip-patterns` | `string` | `[]` | A JSON string array of URL path patterns to ignore. Pageviews matching these patterns won't be tracked. Supports two types of wildcards: `*` (matches within a segment) and `**` (matches across segments). |
| `data-mask-patterns` | `string` | `[]` | A JSON string array of URL path patterns to mask for privacy. Pageviews matching these patterns will be tracked, but the actual URL path will be replaced with the pattern itself in analytics data. Supports the same wildcards as `data-skip-patterns`. |
@@ -55,12 +56,34 @@ data-mask-patterns='["/users/*/settings", "/accounts/**", "/orders/*/details"]'
data-skip-patterns='["/admin/**", "/preview/*"]'
data-mask-patterns='["/users/*/profile", "/orders/**"]'
data-track-query="false"
+ data-session-replay="true"
data-track-errors="true"
data-web-vitals="true"
data-debounce="300"
></script>
```
+## Session Replay
+
+Session replay is disabled by default. When enabled with `data-session-replay="true"`, the script will:
+
+- Record user interactions, mouse movements, clicks, and scrolling
+- Capture DOM changes and page navigation
+- Store replay data for debugging and user experience analysis
+- Provide visual playback of user sessions in your analytics dashboard
+
+**Enable Session Replay:**
+```html
+<script
+ src="https://app.rybbit.io/api/script.js"
+ async
+ data-site-id="YOUR_SITE_ID"
+ data-session-replay="true"
+></script>
+```
+
+**Privacy Note:** Session replay captures visual user interactions but does not record sensitive form inputs like passwords or credit card numbers. Text inputs are masked by default to protect user privacy.
+
## Web Vitals Performance Metrics
Web Vitals are disabled by default to keep the script lightweight. When enabled with `data-web-vitals="true"`, the script will:
@@ -82,7 +105,7 @@ Web Vitals are disabled by default to keep the script lightweight. When enabled
## Error Tracking
-Error tracking is disabled by default to keep the script lightweight and respect user privacy. When enabled with `data-track-errors="true"`, the script will:
+Error tracking is disabled by default. When enabled with `data-track-errors="true"`, the script will:
- Automatically capture JavaScript errors and unhandled promise rejections
- Filter out errors from third-party scripts (only tracks errors from your domain)
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 1225e1529..988258830 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -87,6 +87,16 @@
[]
);
const apiKey = scriptTag.getAttribute("data-api-key") || void 0;
+ const sessionReplayBatchSize = scriptTag.getAttribute(
+ "data-replay-batch-size"
+ ) ? Math.max(1, parseInt(scriptTag.getAttribute("data-replay-batch-size"))) : 3;
+ const sessionReplayBatchInterval = scriptTag.getAttribute(
+ "data-replay-batch-interval"
+ ) ? Math.max(
+ 1e3,
+ parseInt(scriptTag.getAttribute("data-replay-batch-interval"))
+ ) : 2e3;
+ console.info(scriptTag);
return {
analyticsHost,
siteId,
@@ -97,18 +107,249 @@
trackOutbound: scriptTag.getAttribute("data-track-outbound") !== "false",
enableWebVitals: scriptTag.getAttribute("data-web-vitals") === "true",
trackErrors: scriptTag.getAttribute("data-track-errors") === "true",
+ enableSessionReplay: scriptTag.getAttribute("data-session-replay") === "true",
+ sessionReplayBatchSize,
+ sessionReplayBatchInterval,
skipPatterns,
maskPatterns,
apiKey
};
}
+ // sessionReplay.ts
+ var SessionReplayRecorder = class {
+ constructor(config, userId, sendBatch) {
+ this.isRecording = false;
+ this.eventBuffer = [];
+ this.config = config;
+ this.userId = userId;
+ this.sendBatch = sendBatch;
+ }
+ async initialize() {
+ if (!this.config.enableSessionReplay) {
+ return;
+ }
+ if (!window.rrweb) {
+ await this.loadRrweb();
+ }
+ if (window.rrweb) {
+ this.startRecording();
+ } else {
+ console.warn("Failed to load rrweb, session replay disabled");
+ }
+ }
+ async loadRrweb() {
+ return new Promise((resolve, reject) => {
+ const script = document.createElement("script");
+ script.src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js";
+ script.async = false;
+ script.onload = () => {
+ console.log("[Session Replay] rrweb loaded successfully");
+ resolve();
+ };
+ script.onerror = () => reject(new Error("Failed to load rrweb"));
+ document.head.appendChild(script);
+ });
+ }
+ startRecording() {
+ if (this.isRecording || !window.rrweb || !this.config.enableSessionReplay) {
+ console.log("[Session Replay] Cannot start recording:", {
+ isRecording: this.isRecording,
+ hasRrweb: !!window.rrweb,
+ enableSessionReplay: this.config.enableSessionReplay
+ });
+ return;
+ }
+ console.log(
+ "[Session Replay] Starting recording at",
+ (/* @__PURE__ */ new Date()).toISOString()
+ );
+ console.log("[Session Replay] Document ready state:", document.readyState);
+ try {
+ this.stopRecordingFn = window.rrweb.record({
+ emit: (event) => {
+ const eventTypeNames = {
+ 0: "DOMContentLoaded",
+ 1: "Load",
+ 2: "FullSnapshot",
+ 3: "IncrementalSnapshot",
+ 4: "Meta",
+ 5: "Custom",
+ 6: "Plugin"
+ };
+ const typeName = eventTypeNames[event.type] || `Unknown(${event.type})`;
+ console.log(
+ `[Session Replay] Event collected: Type ${event.type} (${typeName}) at ${new Date(event.timestamp || Date.now()).toISOString()}`
+ );
+ this.addEvent({
+ type: event.type,
+ data: event.data,
+ timestamp: event.timestamp || Date.now()
+ });
+ },
+ recordCanvas: true,
+ // Record canvas elements
+ collectFonts: true,
+ // Collect font info for better replay
+ checkoutEveryNms: 3e4,
+ // Checkout every 30 seconds
+ checkoutEveryNth: 200,
+ // Checkout every 200 events
+ maskAllInputs: true,
+ // Mask all input values for privacy
+ maskInputOptions: {
+ password: true,
+ email: true
+ },
+ slimDOMOptions: {
+ script: false,
+ comment: true,
+ headFavicon: true,
+ headWhitespace: true,
+ headMetaDescKeywords: true,
+ headMetaSocial: true,
+ headMetaRobots: true,
+ headMetaHttpEquiv: true,
+ headMetaAuthorship: true,
+ headMetaVerification: true
+ },
+ sampling: {
+ // Optional: reduce recording frequency to save bandwidth
+ mousemove: false,
+ // Don't record every mouse move
+ mouseInteraction: true,
+ scroll: 150,
+ // Sample scroll events every 150ms
+ input: "last"
+ // Only record the final input value
+ }
+ });
+ this.isRecording = true;
+ this.setupBatchTimer();
+ console.log("Session replay recording started");
+ } catch (error) {
+ console.error("Failed to start session replay recording:", error);
+ }
+ }
+ stopRecording() {
+ if (!this.isRecording) {
+ return;
+ }
+ if (this.stopRecordingFn) {
+ this.stopRecordingFn();
+ }
+ this.isRecording = false;
+ this.clearBatchTimer();
+ if (this.eventBuffer.length > 0) {
+ this.flushEvents();
+ }
+ console.log("Session replay recording stopped");
+ }
+ isActive() {
+ return this.isRecording;
+ }
+ addEvent(event) {
+ this.eventBuffer.push(event);
+ console.log(
+ `[Session Replay] Event added to buffer (${this.eventBuffer.length}/${this.config.sessionReplayBatchSize})`
+ );
+ if (this.eventBuffer.length >= this.config.sessionReplayBatchSize) {
+ console.log(
+ `[Session Replay] Buffer full, flushing ${this.eventBuffer.length} events`
+ );
+ this.flushEvents();
+ }
+ }
+ setupBatchTimer() {
+ this.clearBatchTimer();
+ this.batchTimer = window.setInterval(() => {
+ if (this.eventBuffer.length > 0) {
+ console.log(
+ `[Session Replay] Timer triggered, flushing ${this.eventBuffer.length} events`
+ );
+ this.flushEvents();
+ }
+ }, this.config.sessionReplayBatchInterval);
+ }
+ clearBatchTimer() {
+ if (this.batchTimer) {
+ clearInterval(this.batchTimer);
+ this.batchTimer = void 0;
+ }
+ }
+ async flushEvents() {
+ if (this.eventBuffer.length === 0) {
+ return;
+ }
+ const events = [...this.eventBuffer];
+ this.eventBuffer = [];
+ console.log(
+ `[Session Replay] Sending batch with ${events.length} events to server`
+ );
+ console.log(
+ `[Session Replay] Event types in batch:`,
+ events.map((e2) => `Type ${e2.type}`).join(", ")
+ );
+ console.log(
+ `[Session Replay] Batch size:`,
+ JSON.stringify(events).length,
+ "characters"
+ );
+ const batch = {
+ userId: this.userId,
+ events,
+ metadata: {
+ pageUrl: window.location.href,
+ viewportWidth: window.innerWidth,
+ viewportHeight: window.innerHeight,
+ language: navigator.language
+ }
+ };
+ try {
+ await this.sendBatch(batch);
+ console.log(
+ `[Session Replay] Successfully sent batch with ${events.length} events`
+ );
+ } catch (error) {
+ console.error("Failed to send session replay batch:", error);
+ console.error("Failed batch details:", {
+ eventCount: events.length,
+ eventTypes: events.map((e2) => e2.type),
+ batchSize: JSON.stringify(batch).length,
+ userId: this.userId,
+ url: window.location.href
+ });
+ console.log(
+ `[Session Replay] Re-queuing ${events.length} failed events for retry`
+ );
+ this.eventBuffer.unshift(...events);
+ }
+ }
+ // Update user ID when it changes
+ updateUserId(userId) {
+ this.userId = userId;
+ }
+ // Handle page navigation for SPAs
+ onPageChange() {
+ if (this.isRecording) {
+ this.flushEvents();
+ }
+ }
+ // Cleanup on page unload
+ cleanup() {
+ this.stopRecording();
+ }
+ };
+
// tracking.ts
var Tracker = class {
constructor(config) {
this.customUserId = null;
this.config = config;
this.loadUserId();
+ if (config.enableSessionReplay) {
+ this.initializeSessionReplay();
+ }
}
loadUserId() {
try {
@@ -119,6 +360,41 @@
} catch (e2) {
}
}
+ async initializeSessionReplay() {
+ try {
+ this.sessionReplayRecorder = new SessionReplayRecorder(
+ this.config,
+ this.customUserId || this.generateUserId(),
+ (batch) => this.sendSessionReplayBatch(batch)
+ );
+ await this.sessionReplayRecorder.initialize();
+ } catch (error) {
+ console.error("Failed to initialize session replay:", error);
+ }
+ }
+ generateUserId() {
+ return "anon_" + Math.random().toString(36).substring(2) + Date.now().toString(36);
+ }
+ async sendSessionReplayBatch(batch) {
+ try {
+ await fetch(
+ `${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify(batch),
+ mode: "cors",
+ keepalive: false
+ // Disable keepalive for large session replay requests
+ }
+ );
+ } catch (error) {
+ console.error("Failed to send session replay batch:", error);
+ throw error;
+ }
+ }
createBasePayload() {
const url = new URL(window.location.href);
let pathname = url.pathname;
@@ -263,6 +539,9 @@
} catch (e2) {
console.warn("Could not persist user ID to localStorage");
}
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.updateUserId(this.customUserId);
+ }
}
clearUserId() {
this.customUserId = null;
@@ -274,6 +553,34 @@
getUserId() {
return this.customUserId;
}
+ // Session Replay methods
+ startSessionReplay() {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.startRecording();
+ } else {
+ console.warn("Session replay not initialized");
+ }
+ }
+ stopSessionReplay() {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.stopRecording();
+ }
+ }
+ isSessionReplayActive() {
+ return this.sessionReplayRecorder?.isActive() ?? false;
+ }
+ // Handle page changes for SPA
+ onPageChange() {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.onPageChange();
+ }
+ }
+ // Cleanup
+ cleanup() {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.cleanup();
+ }
+ }
};
// ../../node_modules/web-vitals/dist/web-vitals.js
@@ -587,7 +894,12 @@
},
clearUserId: () => {
},
- getUserId: () => null
+ getUserId: () => null,
+ startSessionReplay: () => {
+ },
+ stopSessionReplay: () => {
+ },
+ isSessionReplayActive: () => false
};
return;
}
@@ -660,13 +972,21 @@
history.pushState = function(...args) {
originalPushState.apply(this, args);
debouncedTrackPageview();
+ tracker.onPageChange();
};
history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
debouncedTrackPageview();
+ tracker.onPageChange();
};
- window.addEventListener("popstate", debouncedTrackPageview);
- window.addEventListener("hashchange", debouncedTrackPageview);
+ window.addEventListener("popstate", () => {
+ debouncedTrackPageview();
+ tracker.onPageChange();
+ });
+ window.addEventListener("hashchange", () => {
+ debouncedTrackPageview();
+ tracker.onPageChange();
+ });
}
}
window.rybbit = {
@@ -675,9 +995,15 @@
trackOutbound: (url, text = "", target = "_self") => tracker.trackOutbound(url, text, target),
identify: (userId) => tracker.identify(userId),
clearUserId: () => tracker.clearUserId(),
- getUserId: () => tracker.getUserId()
+ getUserId: () => tracker.getUserId(),
+ startSessionReplay: () => tracker.startSessionReplay(),
+ stopSessionReplay: () => tracker.stopSessionReplay(),
+ isSessionReplayActive: () => tracker.isSessionReplayActive()
};
setupEventListeners();
+ window.addEventListener("beforeunload", () => {
+ tracker.cleanup();
+ });
if (config.autoTrackPageview) {
tracker.trackPageview();
}
diff --git a/server/public/script.js b/server/public/script.js
index 255ba0f73..2a30f8fd9 100644
--- a/server/public/script.js
+++ b/server/public/script.js
@@ -1 +1 @@
-"use strict";(()=>{var it=Object.defineProperty;var at=(r,t,e)=>t in r?it(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var h=(r,t,e)=>at(r,typeof t!="symbol"?t+"":t,e);function st(r){let t="__DOUBLE_ASTERISK_TOKEN__",e="__SINGLE_ASTERISK_TOKEN__",i=r.replace(/\*\*/g,t).replace(/\*/g,e).replace(/[.+?^${}()|[\]\\]/g,"\\$&");i=i.replace(new RegExp(`/${t}/`,"g"),"/(?:.+/)?"),i=i.replace(new RegExp(t,"g"),".*"),i=i.replace(/\//g,"\\/");let c=i.replace(new RegExp(e,"g"),"[^/]+");return new RegExp("^"+c+"$")}function E(r,t){for(let e of t)try{if(st(e).test(r))return e}catch(n){console.error(`Invalid pattern: ${e}`,n)}return null}function N(r,t){let e=null;return(...n)=>{e&&clearTimeout(e),e=setTimeout(()=>r(...n),t)}}function W(r){try{let t=window.location.hostname,e=new URL(r).hostname;return e!==t&&e!==""}catch{return!1}}function P(r,t){if(!r)return t;try{let e=JSON.parse(r);return Array.isArray(t)&&!Array.isArray(e)?t:e}catch(e){return console.error("Error parsing JSON:",e),t}}function B(r){let t=r.getAttribute("src");if(!t)return console.error("Script src attribute is missing"),null;let e=t.split("/script.js")[0];if(!e)return console.error("Please provide a valid analytics host"),null;let n=r.getAttribute("data-site-id")||r.getAttribute("site-id");if(!n||isNaN(Number(n)))return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let i=r.getAttribute("data-debounce")?Math.max(0,parseInt(r.getAttribute("data-debounce"))):500,c=P(r.getAttribute("data-skip-patterns"),[]),s=P(r.getAttribute("data-mask-patterns"),[]),a=r.getAttribute("data-api-key")||void 0;return{analyticsHost:e,siteId:n,debounceDuration:i,autoTrackPageview:r.getAttribute("data-auto-track-pageview")!=="false",autoTrackSpa:r.getAttribute("data-track-spa")!=="false",trackQuerystring:r.getAttribute("data-track-query")!=="false",trackOutbound:r.getAttribute("data-track-outbound")!=="false",enableWebVitals:r.getAttribute("data-web-vitals")==="true",trackErrors:r.getAttribute("data-track-errors")==="true",skipPatterns:c,maskPatterns:s,apiKey:a}}var b=class{constructor(t){this.customUserId=null;this.config=t,this.loadUserId()}loadUserId(){try{let t=localStorage.getItem("rybbit-user-id");t&&(this.customUserId=t)}catch{}}createBasePayload(){let t=new URL(window.location.href),e=t.pathname;if(t.hash&&t.hash.startsWith("#/")&&(e=t.hash.substring(1)),E(e,this.config.skipPatterns))return null;let n=E(e,this.config.maskPatterns);n&&(e=n);let i={site_id:this.config.siteId,hostname:t.hostname,pathname:e,querystring:this.config.trackQuerystring?t.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return this.customUserId&&(i.user_id=this.customUserId),this.config.apiKey&&(i.api_key=this.config.apiKey),i}async sendTrackingData(t){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),mode:"cors",keepalive:!0})}catch(e){console.error("Failed to send tracking data:",e)}}track(t,e="",n={}){if(t==="custom_event"&&(!e||typeof e!="string")){console.error("Event name is required and must be a string for custom events");return}let i=this.createBasePayload();if(!i)return;let c={...i,type:t,event_name:e,properties:t==="custom_event"||t==="outbound"||t==="error"?JSON.stringify(n):void 0};this.sendTrackingData(c)}trackPageview(){this.track("pageview")}trackEvent(t,e={}){this.track("custom_event",t,e)}trackOutbound(t,e="",n="_self"){this.track("outbound","",{url:t,text:e,target:n})}trackWebVitals(t){let e=this.createBasePayload();if(!e)return;let n={...e,type:"performance",event_name:"web-vitals",...t};this.sendTrackingData(n)}trackError(t,e={}){let n=window.location.origin,i=e.filename||"",c=t.stack||"";if(i)try{if(new URL(i).origin!==n)return}catch{}else if(c&&!c.includes(n))return;let s={message:t.message?.substring(0,500)||"Unknown error",stack:c.substring(0,2e3)||""};if(i&&(s.fileName=i),e.lineno){let a=typeof e.lineno=="string"?parseInt(e.lineno,10):e.lineno;a&&a!==0&&(s.lineNumber=a)}if(e.colno){let a=typeof e.colno=="string"?parseInt(e.colno,10):e.colno;a&&a!==0&&(s.columnNumber=a)}for(let a in e)!["lineno","colno"].includes(a)&&e[a]!==void 0&&(s[a]=e[a]);this.track("error",t.name||"Error",s)}identify(t){if(typeof t!="string"||t.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=t.trim();try{localStorage.setItem("rybbit-user-id",this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}}clearUserId(){this.customUserId=null;try{localStorage.removeItem("rybbit-user-id")}catch{}}getUserId(){return this.customUserId}};var G=-1,m=r=>{addEventListener("pageshow",t=>{t.persisted&&(G=t.timeStamp,r(t))},!0)},d=(r,t,e,n)=>{let i,c;return s=>{t.value>=0&&(s||n)&&(c=t.value-(i??0),(c||i===void 0)&&(i=t.value,t.delta=c,t.rating=((a,o)=>a>o[1]?"poor":a>o[0]?"needs-improvement":"good")(t.value,e),r(t)))}},R=r=>{requestAnimationFrame(()=>requestAnimationFrame(()=>r()))},M=()=>{let r=performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r},p=()=>M()?.activationStart??0,u=(r,t=-1)=>{let e=M(),n="navigate";return G>=0?n="back-forward-cache":e&&(document.prerendering||p()>0?n="prerender":document.wasDiscarded?n="restore":e.type&&(n=e.type.replace(/_/g,"-"))),{name:r,value:t,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:n}},S=new WeakMap;function U(r,t){return S.get(r)||S.set(r,new t),S.get(r)}var L=class{constructor(){h(this,"t");h(this,"i",0);h(this,"o",[])}h(t){if(t.hadRecentInput)return;let e=this.o[0],n=this.o.at(-1);this.i&&e&&n&&t.startTime-n.startTime<1e3&&t.startTime-e.startTime<5e3?(this.i+=t.value,this.o.push(t)):(this.i=t.value,this.o=[t]),this.t?.(t)}},f=(r,t,e={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(r)){let n=new PerformanceObserver(i=>{Promise.resolve().then(()=>{t(i.getEntries())})});return n.observe({type:r,buffered:!0,...e}),n}}catch{}},O=r=>{let t=!1;return()=>{t||(r(),t=!0)}},g=-1,H=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,y=r=>{document.visibilityState==="hidden"&&g>-1&&(g=r.type==="visibilitychange"?r.timeStamp:0,ot())},V=()=>{addEventListener("visibilitychange",y,!0),addEventListener("prerenderingchange",y,!0)},ot=()=>{removeEventListener("visibilitychange",y,!0),removeEventListener("prerenderingchange",y,!0)},Q=()=>{if(g<0){let r=p();g=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter(e=>e.name==="hidden"&&e.startTime>r)[0]?.startTime)??H(),V(),m(()=>{setTimeout(()=>{g=H(),V()})})}return{get firstHiddenTime(){return g}}},T=r=>{document.prerendering?addEventListener("prerenderingchange",()=>r(),!0):r()},F=[1800,3e3],x=(r,t={})=>{T(()=>{let e=Q(),n,i=u("FCP"),c=f("paint",s=>{for(let a of s)a.name==="first-contentful-paint"&&(c.disconnect(),a.startTime<e.firstHiddenTime&&(i.value=Math.max(a.startTime-p(),0),i.entries.push(a),n(!0)))});c&&(n=d(r,i,F,t.reportAllChanges),m(s=>{i=u("FCP"),n=d(r,i,F,t.reportAllChanges),R(()=>{i.value=performance.now()-s.timeStamp,n(!0)})}))})},K=[.1,.25],Y=(r,t={})=>{x(O(()=>{let e,n=u("CLS",0),i=U(t,L),c=a=>{for(let o of a)i.h(o);i.i>n.value&&(n.value=i.i,n.entries=i.o,e())},s=f("layout-shift",c);s&&(e=d(r,n,K,t.reportAllChanges),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(s.takeRecords()),e(!0))}),m(()=>{i.i=0,n=u("CLS",0),e=d(r,n,K,t.reportAllChanges),R(()=>e())}),setTimeout(e))}))},X=0,C=1/0,v=0,ct=r=>{for(let t of r)t.interactionId&&(C=Math.min(C,t.interactionId),v=Math.max(v,t.interactionId),X=v?(v-C)/7+1:0)},I,$=()=>I?X:performance.interactionCount??0,lt=()=>{"interactionCount"in performance||I||(I=f("event",ct,{type:"event",buffered:!0,durationThreshold:0}))},J=0,_=class{constructor(){h(this,"u",[]);h(this,"l",new Map);h(this,"m");h(this,"v")}p(){J=$(),this.u.length=0,this.l.clear()}P(){let t=Math.min(this.u.length-1,Math.floor(($()-J)/50));return this.u[t]}h(t){if(this.m?.(t),!t.interactionId&&t.entryType!=="first-input")return;let e=this.u.at(-1),n=this.l.get(t.interactionId);if(n||this.u.length<10||t.duration>e.T){if(n?t.duration>n.T?(n.entries=[t],n.T=t.duration):t.duration===n.T&&t.startTime===n.entries[0].startTime&&n.entries.push(t):(n={id:t.interactionId,entries:[t],T:t.duration},this.l.set(n.id,n),this.u.push(n)),this.u.sort((i,c)=>c.T-i.T),this.u.length>10){let i=this.u.splice(10);for(let c of i)this.l.delete(c.id)}this.v?.(n)}}},Z=r=>{let t=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?r():(r=O(r),document.addEventListener("visibilitychange",r,{once:!0}),t(()=>{r(),document.removeEventListener("visibilitychange",r)}))},j=[200,500],tt=(r,t={})=>{globalThis.PerformanceEventTiming&&"interactionId"in PerformanceEventTiming.prototype&&T(()=>{lt();let e,n=u("INP"),i=U(t,_),c=a=>{Z(()=>{for(let l of a)i.h(l);let o=i.P();o&&o.T!==n.value&&(n.value=o.T,n.entries=o.entries,e())})},s=f("event",c,{durationThreshold:t.durationThreshold??40});e=d(r,n,j,t.reportAllChanges),s&&(s.observe({type:"first-input",buffered:!0}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(s.takeRecords()),e(!0))}),m(()=>{i.p(),n=u("INP"),e=d(r,n,j,t.reportAllChanges)}))})},A=class{constructor(){h(this,"m")}h(t){this.m?.(t)}},q=[2500,4e3],et=(r,t={})=>{T(()=>{let e=Q(),n,i=u("LCP"),c=U(t,A),s=o=>{t.reportAllChanges||(o=o.slice(-1));for(let l of o)c.h(l),l.startTime<e.firstHiddenTime&&(i.value=Math.max(l.startTime-p(),0),i.entries=[l],n())},a=f("largest-contentful-paint",s);if(a){n=d(r,i,q,t.reportAllChanges);let o=O(()=>{s(a.takeRecords()),a.disconnect(),n(!0)});for(let l of["keydown","click","visibilitychange"])addEventListener(l,()=>Z(o),{capture:!0,once:!0});m(l=>{i=u("LCP"),n=d(r,i,q,t.reportAllChanges),R(()=>{i.value=performance.now()-l.timeStamp,n(!0)})})}})},z=[800,1800],D=r=>{document.prerendering?T(()=>D(r)):document.readyState!=="complete"?addEventListener("load",()=>D(r),!0):setTimeout(r)},rt=(r,t={})=>{let e=u("TTFB"),n=d(r,e,z,t.reportAllChanges);D(()=>{let i=M();i&&(e.value=Math.max(i.responseStart-p(),0),e.entries=[i],n(!0),m(()=>{e=u("TTFB",0),n=d(r,e,z,t.reportAllChanges),n(!0)}))})};var k=class{constructor(t){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=t}initialize(){try{et(this.collectMetric.bind(this)),Y(this.collectMetric.bind(this)),tt(this.collectMetric.bind(this)),x(this.collectMetric.bind(this)),rt(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>{this.sent||this.sendData()},2e4),window.addEventListener("beforeunload",()=>{this.sent||this.sendData()})}catch(t){console.warn("Error initializing web vitals tracking:",t)}}collectMetric(t){if(this.sent)return;let e=t.name.toLowerCase();this.data[e]=t.value,Object.values(this.data).every(i=>i!==null)&&this.sendData()}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};(function(){let r=document.currentScript;if(!r){console.error("Could not find current script tag");return}if(window.__RYBBIT_OPTOUT__||localStorage.getItem("disable-rybbit")!==null){window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null};return}let t=B(r);if(!t)return;let e=new b(t);t.enableWebVitals&&new k(a=>{e.trackWebVitals(a)}).initialize(),t.trackErrors&&(window.addEventListener("error",s=>{e.trackError(s.error||new Error(s.message),{filename:s.filename,lineno:s.lineno,colno:s.colno})}),window.addEventListener("unhandledrejection",s=>{let a=s.reason instanceof Error?s.reason:new Error(String(s.reason));e.trackError(a,{type:"unhandledrejection"})}));let n=()=>e.trackPageview(),i=t.debounceDuration>0?N(n,t.debounceDuration):n;function c(){if(document.addEventListener("click",function(s){let a=s.target;for(;a&&a!==document.documentElement;){if(a.hasAttribute("data-rybbit-event")){let o=a.getAttribute("data-rybbit-event");if(o){let l={};for(let w of a.attributes)if(w.name.startsWith("data-rybbit-prop-")){let nt=w.name.replace("data-rybbit-prop-","");l[nt]=w.value}e.trackEvent(o,l)}break}a=a.parentElement}if(t.trackOutbound){let o=s.target.closest("a");o?.href&&W(o.href)&&e.trackOutbound(o.href,o.innerText||o.textContent||"",o.target||"_self")}}),t.autoTrackSpa){let s=history.pushState,a=history.replaceState;history.pushState=function(...o){s.apply(this,o),i()},history.replaceState=function(...o){a.apply(this,o),i()},window.addEventListener("popstate",i),window.addEventListener("hashchange",i)}}window.rybbit={pageview:()=>e.trackPageview(),event:(s,a={})=>e.trackEvent(s,a),trackOutbound:(s,a="",o="_self")=>e.trackOutbound(s,a,o),identify:s=>e.identify(s),clearUserId:()=>e.clearUserId(),getUserId:()=>e.getUserId()},c(),t.autoTrackPageview&&e.trackPageview()})();})();
+"use strict";(()=>{var se=Object.defineProperty;var ae=(i,e,t)=>e in i?se(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>ae(i,typeof e!="symbol"?e+"":e,t);function oe(i){let e="__DOUBLE_ASTERISK_TOKEN__",t="__SINGLE_ASTERISK_TOKEN__",n=i.replace(/\*\*/g,e).replace(/\*/g,t).replace(/[.+?^${}()|[\]\\]/g,"\\$&");n=n.replace(new RegExp(`/${e}/`,"g"),"/(?:.+/)?"),n=n.replace(new RegExp(e,"g"),".*"),n=n.replace(/\//g,"\\/");let c=n.replace(new RegExp(t,"g"),"[^/]+");return new RegExp("^"+c+"$")}function k(i,e){for(let t of e)try{if(oe(t).test(i))return t}catch(r){console.error(`Invalid pattern: ${t}`,r)}return null}function x(i,e){let t=null;return(...r)=>{t&&clearTimeout(t),t=setTimeout(()=>i(...r),e)}}function F(i){try{let e=window.location.hostname,t=new URL(i).hostname;return t!==e&&t!==""}catch{return!1}}function E(i,e){if(!i)return e;try{let t=JSON.parse(i);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}function W(i){let e=i.getAttribute("src");if(!e)return console.error("Script src attribute is missing"),null;let t=e.split("/script.js")[0];if(!t)return console.error("Please provide a valid analytics host"),null;let r=i.getAttribute("data-site-id")||i.getAttribute("site-id");if(!r||isNaN(Number(r)))return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let n=i.getAttribute("data-debounce")?Math.max(0,parseInt(i.getAttribute("data-debounce"))):500,c=E(i.getAttribute("data-skip-patterns"),[]),a=E(i.getAttribute("data-mask-patterns"),[]),s=i.getAttribute("data-api-key")||void 0,o=i.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(i.getAttribute("data-replay-batch-size"))):3,l=i.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(i.getAttribute("data-replay-batch-interval"))):2e3;return console.info(i),{analyticsHost:t,siteId:r,debounceDuration:n,autoTrackPageview:i.getAttribute("data-auto-track-pageview")!=="false",autoTrackSpa:i.getAttribute("data-track-spa")!=="false",trackQuerystring:i.getAttribute("data-track-query")!=="false",trackOutbound:i.getAttribute("data-track-outbound")!=="false",enableWebVitals:i.getAttribute("data-web-vitals")==="true",trackErrors:i.getAttribute("data-track-errors")==="true",enableSessionReplay:i.getAttribute("data-session-replay")==="true",sessionReplayBatchSize:o,sessionReplayBatchInterval:l,skipPatterns:c,maskPatterns:a,apiKey:s}}var y=class{constructor(e,t,r){this.isRecording=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=r}async initialize(){this.config.enableSessionReplay&&(window.rrweb||await this.loadRrweb(),window.rrweb?this.startRecording():console.warn("Failed to load rrweb, session replay disabled"))}async loadRrweb(){return new Promise((e,t)=>{let r=document.createElement("script");r.src="https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js",r.async=!1,r.onload=()=>{console.log("[Session Replay] rrweb loaded successfully"),e()},r.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(r)})}startRecording(){if(this.isRecording||!window.rrweb||!this.config.enableSessionReplay){console.log("[Session Replay] Cannot start recording:",{isRecording:this.isRecording,hasRrweb:!!window.rrweb,enableSessionReplay:this.config.enableSessionReplay});return}console.log("[Session Replay] Starting recording at",new Date().toISOString()),console.log("[Session Replay] Document ready state:",document.readyState);try{this.stopRecordingFn=window.rrweb.record({emit:e=>{let r={0:"DOMContentLoaded",1:"Load",2:"FullSnapshot",3:"IncrementalSnapshot",4:"Meta",5:"Custom",6:"Plugin"}[e.type]||`Unknown(${e.type})`;console.log(`[Session Replay] Event collected: Type ${e.type} (${r}) at ${new Date(e.timestamp||Date.now()).toISOString()}`),this.addEvent({type:e.type,data:e.data,timestamp:e.timestamp||Date.now()})},recordCanvas:!0,collectFonts:!0,checkoutEveryNms:3e4,checkoutEveryNth:200,maskAllInputs:!0,maskInputOptions:{password:!0,email:!0},slimDOMOptions:{script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},sampling:{mousemove:!1,mouseInteraction:!0,scroll:150,input:"last"}}),this.isRecording=!0,this.setupBatchTimer(),console.log("Session replay recording started")}catch(e){console.error("Failed to start session replay recording:",e)}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents(),console.log("Session replay recording stopped"))}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),console.log(`[Session Replay] Event added to buffer (${this.eventBuffer.length}/${this.config.sessionReplayBatchSize})`),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&(console.log(`[Session Replay] Buffer full, flushing ${this.eventBuffer.length} events`),this.flushEvents())}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&(console.log(`[Session Replay] Timer triggered, flushing ${this.eventBuffer.length} events`),this.flushEvents())},this.config.sessionReplayBatchInterval)}clearBatchTimer(){this.batchTimer&&(clearInterval(this.batchTimer),this.batchTimer=void 0)}async flushEvents(){if(this.eventBuffer.length===0)return;let e=[...this.eventBuffer];this.eventBuffer=[],console.log(`[Session Replay] Sending batch with ${e.length} events to server`),console.log("[Session Replay] Event types in batch:",e.map(r=>`Type ${r.type}`).join(", ")),console.log("[Session Replay] Batch size:",JSON.stringify(e).length,"characters");let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:window.innerWidth,viewportHeight:window.innerHeight,language:navigator.language}};try{await this.sendBatch(t),console.log(`[Session Replay] Successfully sent batch with ${e.length} events`)}catch(r){console.error("Failed to send session replay batch:",r),console.error("Failed batch details:",{eventCount:e.length,eventTypes:e.map(n=>n.type),batchSize:JSON.stringify(t).length,userId:this.userId,url:window.location.href}),console.log(`[Session Replay] Re-queuing ${e.length} failed events for retry`),this.eventBuffer.unshift(...e)}}updateUserId(e){this.userId=e}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var v=class{constructor(e){this.customUserId=null;this.config=e,this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}loadUserId(){try{let e=localStorage.getItem("rybbit-user-id");e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new y(this.config,this.customUserId||this.generateUserId(),e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}generateUserId(){return"anon_"+Math.random().toString(36).substring(2)+Date.now().toString(36)}async sendSessionReplayBatch(e){try{await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!1})}catch(t){throw console.error("Failed to send session replay batch:",t),t}}createBasePayload(){let e=new URL(window.location.href),t=e.pathname;if(e.hash&&e.hash.startsWith("#/")&&(t=e.hash.substring(1)),k(t,this.config.skipPatterns))return null;let r=k(t,this.config.maskPatterns);r&&(t=r);let n={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return this.customUserId&&(n.user_id=this.customUserId),this.config.apiKey&&(n.api_key=this.config.apiKey),n}async sendTrackingData(e){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!0})}catch(t){console.error("Failed to send tracking data:",t)}}track(e,t="",r={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let n=this.createBasePayload();if(!n)return;let c={...n,type:e,event_name:t,properties:e==="custom_event"||e==="outbound"||e==="error"?JSON.stringify(r):void 0};this.sendTrackingData(c)}trackPageview(){this.track("pageview")}trackEvent(e,t={}){this.track("custom_event",e,t)}trackOutbound(e,t="",r="_self"){this.track("outbound","",{url:e,text:t,target:r})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let r={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(r)}trackError(e,t={}){let r=window.location.origin,n=t.filename||"",c=e.stack||"";if(n)try{if(new URL(n).origin!==r)return}catch{}else if(c&&!c.includes(r))return;let a={message:e.message?.substring(0,500)||"Unknown error",stack:c.substring(0,2e3)||""};if(n&&(a.fileName=n),t.lineno){let s=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;s&&s!==0&&(a.lineNumber=s)}if(t.colno){let s=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;s&&s!==0&&(a.columnNumber=s)}for(let s in t)!["lineno","colno"].includes(s)&&t[s]!==void 0&&(a[s]=t[s]);this.track("error",e.name||"Error",a)}identify(e){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem("rybbit-user-id",this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}clearUserId(){this.customUserId=null;try{localStorage.removeItem("rybbit-user-id")}catch{}}getUserId(){return this.customUserId}startSessionReplay(){this.sessionReplayRecorder?this.sessionReplayRecorder.startRecording():console.warn("Session replay not initialized")}stopSessionReplay(){this.sessionReplayRecorder&&this.sessionReplayRecorder.stopRecording()}isSessionReplayActive(){return this.sessionReplayRecorder?.isActive()??!1}onPageChange(){this.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Q=-1,g=i=>{addEventListener("pageshow",e=>{e.persisted&&(Q=e.timeStamp,i(e))},!0)},d=(i,e,t,r)=>{let n,c;return a=>{e.value>=0&&(a||r)&&(c=e.value-(n??0),(c||n===void 0)&&(n=e.value,e.delta=c,e.rating=((s,o)=>s>o[1]?"poor":s>o[0]?"needs-improvement":"good")(e.value,t),i(e)))}},M=i=>{requestAnimationFrame(()=>requestAnimationFrame(()=>i()))},O=()=>{let i=performance.getEntriesByType("navigation")[0];if(i&&i.responseStart>0&&i.responseStart<performance.now())return i},f=()=>O()?.activationStart??0,u=(i,e=-1)=>{let t=O(),r="navigate";return Q>=0?r="back-forward-cache":t&&(document.prerendering||f()>0?r="prerender":document.wasDiscarded?r="restore":t.type&&(r=t.type.replace(/_/g,"-"))),{name:i,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:r}},P=new WeakMap;function _(i,e){return P.get(i)||P.set(i,new e),P.get(i)}var C=class{constructor(){h(this,"t");h(this,"i",0);h(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],r=this.o.at(-1);this.i&&t&&r&&e.startTime-r.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}},m=(i,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(i)){let r=new PerformanceObserver(n=>{Promise.resolve().then(()=>{e(n.getEntries())})});return r.observe({type:i,buffered:!0,...t}),r}}catch{}},N=i=>{let e=!1;return()=>{e||(i(),e=!0)}},p=-1,$=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,R=i=>{document.visibilityState==="hidden"&&p>-1&&(p=i.type==="visibilitychange"?i.timeStamp:0,ce())},H=()=>{addEventListener("visibilitychange",R,!0),addEventListener("prerenderingchange",R,!0)},ce=()=>{removeEventListener("visibilitychange",R,!0),removeEventListener("prerenderingchange",R,!0)},Y=()=>{if(p<0){let i=f();p=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter(t=>t.name==="hidden"&&t.startTime>i)[0]?.startTime)??$(),H(),g(()=>{setTimeout(()=>{p=$(),H()})})}return{get firstHiddenTime(){return p}}},w=i=>{document.prerendering?addEventListener("prerenderingchange",()=>i(),!0):i()},z=[1800,3e3],U=(i,e={})=>{w(()=>{let t=Y(),r,n=u("FCP"),c=m("paint",a=>{for(let s of a)s.name==="first-contentful-paint"&&(c.disconnect(),s.startTime<t.firstHiddenTime&&(n.value=Math.max(s.startTime-f(),0),n.entries.push(s),r(!0)))});c&&(r=d(i,n,z,e.reportAllChanges),g(a=>{n=u("FCP"),r=d(i,n,z,e.reportAllChanges),M(()=>{n.value=performance.now()-a.timeStamp,r(!0)})}))})},V=[.1,.25],X=(i,e={})=>{U(N(()=>{let t,r=u("CLS",0),n=_(e,C),c=s=>{for(let o of s)n.h(o);n.i>r.value&&(r.value=n.i,r.entries=n.o,t())},a=m("layout-shift",c);a&&(t=d(i,r,V,e.reportAllChanges),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.i=0,r=u("CLS",0),t=d(i,r,V,e.reportAllChanges),M(()=>t())}),setTimeout(t))}))},Z=0,I=1/0,b=0,le=i=>{for(let e of i)e.interactionId&&(I=Math.min(I,e.interactionId),b=Math.max(b,e.interactionId),Z=b?(b-I)/7+1:0)},B,J=()=>B?Z:performance.interactionCount??0,de=()=>{"interactionCount"in performance||B||(B=m("event",le,{type:"event",buffered:!0,durationThreshold:0}))},K=0,A=class{constructor(){h(this,"u",[]);h(this,"l",new Map);h(this,"m");h(this,"v")}p(){K=J(),this.u.length=0,this.l.clear()}P(){let e=Math.min(this.u.length-1,Math.floor((J()-K)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),r=this.l.get(e.interactionId);if(r||this.u.length<10||e.duration>t.T){if(r?e.duration>r.T?(r.entries=[e],r.T=e.duration):e.duration===r.T&&e.startTime===r.entries[0].startTime&&r.entries.push(e):(r={id:e.interactionId,entries:[e],T:e.duration},this.l.set(r.id,r),this.u.push(r)),this.u.sort((n,c)=>c.T-n.T),this.u.length>10){let n=this.u.splice(10);for(let c of n)this.l.delete(c.id)}this.v?.(r)}}},ee=i=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?i():(i=N(i),document.addEventListener("visibilitychange",i,{once:!0}),e(()=>{i(),document.removeEventListener("visibilitychange",i)}))},j=[200,500],te=(i,e={})=>{globalThis.PerformanceEventTiming&&"interactionId"in PerformanceEventTiming.prototype&&w(()=>{de();let t,r=u("INP"),n=_(e,A),c=s=>{ee(()=>{for(let l of s)n.h(l);let o=n.P();o&&o.T!==r.value&&(r.value=o.T,r.entries=o.entries,t())})},a=m("event",c,{durationThreshold:e.durationThreshold??40});t=d(i,r,j,e.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.p(),r=u("INP"),t=d(i,r,j,e.reportAllChanges)}))})},L=class{constructor(){h(this,"m")}h(e){this.m?.(e)}},q=[2500,4e3],ie=(i,e={})=>{w(()=>{let t=Y(),r,n=u("LCP"),c=_(e,L),a=o=>{e.reportAllChanges||(o=o.slice(-1));for(let l of o)c.h(l),l.startTime<t.firstHiddenTime&&(n.value=Math.max(l.startTime-f(),0),n.entries=[l],r())},s=m("largest-contentful-paint",a);if(s){r=d(i,n,q,e.reportAllChanges);let o=N(()=>{a(s.takeRecords()),s.disconnect(),r(!0)});for(let l of["keydown","click","visibilitychange"])addEventListener(l,()=>ee(o),{capture:!0,once:!0});g(l=>{n=u("LCP"),r=d(i,n,q,e.reportAllChanges),M(()=>{n.value=performance.now()-l.timeStamp,r(!0)})})}})},G=[800,1800],D=i=>{document.prerendering?w(()=>D(i)):document.readyState!=="complete"?addEventListener("load",()=>D(i),!0):setTimeout(i)},re=(i,e={})=>{let t=u("TTFB"),r=d(i,t,G,e.reportAllChanges);D(()=>{let n=O();n&&(t.value=Math.max(n.responseStart-f(),0),t.entries=[n],r(!0),g(()=>{t=u("TTFB",0),r=d(i,t,G,e.reportAllChanges),r(!0)}))})};var S=class{constructor(e){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=e}initialize(){try{ie(this.collectMetric.bind(this)),X(this.collectMetric.bind(this)),te(this.collectMetric.bind(this)),U(this.collectMetric.bind(this)),re(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>{this.sent||this.sendData()},2e4),window.addEventListener("beforeunload",()=>{this.sent||this.sendData()})}catch(e){console.warn("Error initializing web vitals tracking:",e)}}collectMetric(e){if(this.sent)return;let t=e.name.toLowerCase();this.data[t]=e.value,Object.values(this.data).every(n=>n!==null)&&this.sendData()}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};(function(){let i=document.currentScript;if(!i){console.error("Could not find current script tag");return}if(window.__RYBBIT_OPTOUT__||localStorage.getItem("disable-rybbit")!==null){window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null,startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let e=W(i);if(!e)return;let t=new v(e);e.enableWebVitals&&new S(s=>{t.trackWebVitals(s)}).initialize(),e.trackErrors&&(window.addEventListener("error",a=>{t.trackError(a.error||new Error(a.message),{filename:a.filename,lineno:a.lineno,colno:a.colno})}),window.addEventListener("unhandledrejection",a=>{let s=a.reason instanceof Error?a.reason:new Error(String(a.reason));t.trackError(s,{type:"unhandledrejection"})}));let r=()=>t.trackPageview(),n=e.debounceDuration>0?x(r,e.debounceDuration):r;function c(){if(document.addEventListener("click",function(a){let s=a.target;for(;s&&s!==document.documentElement;){if(s.hasAttribute("data-rybbit-event")){let o=s.getAttribute("data-rybbit-event");if(o){let l={};for(let T of s.attributes)if(T.name.startsWith("data-rybbit-prop-")){let ne=T.name.replace("data-rybbit-prop-","");l[ne]=T.value}t.trackEvent(o,l)}break}s=s.parentElement}if(e.trackOutbound){let o=a.target.closest("a");o?.href&&F(o.href)&&t.trackOutbound(o.href,o.innerText||o.textContent||"",o.target||"_self")}}),e.autoTrackSpa){let a=history.pushState,s=history.replaceState;history.pushState=function(...o){a.apply(this,o),n(),t.onPageChange()},history.replaceState=function(...o){s.apply(this,o),n(),t.onPageChange()},window.addEventListener("popstate",()=>{n(),t.onPageChange()}),window.addEventListener("hashchange",()=>{n(),t.onPageChange()})}}window.rybbit={pageview:()=>t.trackPageview(),event:(a,s={})=>t.trackEvent(a,s),trackOutbound:(a,s="",o="_self")=>t.trackOutbound(a,s,o),identify:a=>t.identify(a),clearUserId:()=>t.clearUserId(),getUserId:()=>t.getUserId(),startSessionReplay:()=>t.startSessionReplay(),stopSessionReplay:()=>t.stopSessionReplay(),isSessionReplayActive:()=>t.isSessionReplayActive()},c(),window.addEventListener("beforeunload",()=>{t.cleanup()}),e.autoTrackPageview&&t.trackPageview()})();})();
diff --git a/server/public/test-replay.html b/server/public/test-replay.html
new file mode 100644
index 000000000..4f67e36d4
--- /dev/null
+++ b/server/public/test-replay.html
@@ -0,0 +1,175 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <title>Session Replay Test</title>
+ <style>
+ body {
+ font-family: Arial, sans-serif;
+ padding: 20px;
+ line-height: 1.6;
+ }
+ .test-content {
+ max-width: 800px;
+ margin: 0 auto;
+ }
+ .button {
+ background: #007bff;
+ color: white;
+ padding: 10px 20px;
+ border: none;
+ border-radius: 5px;
+ cursor: pointer;
+ margin: 10px 5px;
+ }
+ .button:hover {
+ background: #0056b3;
+ }
+ .form-group {
+ margin: 20px 0;
+ }
+ input,
+ textarea {
+ width: 100%;
+ padding: 10px;
+ border: 1px solid #ddd;
+ border-radius: 4px;
+ margin: 5px 0;
+ }
+ .status {
+ padding: 10px;
+ margin: 10px 0;
+ border-radius: 4px;
+ background: #f8f9fa;
+ border: 1px solid #dee2e6;
+ }
+ </style>
+ </head>
+ <body>
+ <div class="test-content">
+ <h1>Session Replay Test Page</h1>
+ <p>
+ This page is used to test session replay functionality. Open browser
+ developer tools console to see logging output.
+ </p>
+
+ <div class="status" id="status">Status: Loading analytics script...</div>
+
+ <div class="form-group">
+ <label>Test Input Field:</label>
+ <input
+ type="text"
+ placeholder="Type something here to test input recording"
+ />
+ </div>
+
+ <div class="form-group">
+ <label>Test Textarea:</label>
+ <textarea
+ placeholder="Type in this textarea to test input masking"
+ ></textarea>
+ </div>
+
+ <div>
+ <button class="button" onclick="testEvent()">Click Me</button>
+ <button class="button" onclick="changeContent()">Change Content</button>
+ <button class="button" onclick="addElement()">Add Element</button>
+ </div>
+
+ <div id="dynamic-content">
+ <p>This content can be dynamically changed to test DOM mutations.</p>
+ </div>
+
+ <div id="new-elements">
+ <!-- Elements will be added here dynamically -->
+ </div>
+ </div>
+
+ <!-- Analytics Script -->
+ <script
+ src="http://localhost:3001/script.js"
+ data-site-id="21"
+ data-session-replay="true"
+ defer
+ ></script>
+
+ <script>
+ function updateStatus(message) {
+ document.getElementById("status").textContent = "Status: " + message;
+ console.log("[Test Page]", message);
+ }
+
+ function testEvent() {
+ updateStatus("Button clicked - custom event triggered");
+ if (window.rybbit) {
+ window.rybbit.event("test_button_click", { button: "test" });
+ }
+ }
+
+ function changeContent() {
+ const content = document.getElementById("dynamic-content");
+ content.innerHTML =
+ '<p style="color: red;">Content changed at ' +
+ new Date().toLocaleTimeString() +
+ "</p>";
+ updateStatus("Content changed - DOM mutation recorded");
+ }
+
+ function addElement() {
+ const container = document.getElementById("new-elements");
+ const newElement = document.createElement("div");
+ newElement.style.cssText =
+ "background: #e7f3ff; padding: 10px; margin: 5px 0; border-radius: 4px;";
+ newElement.textContent =
+ "New element added at " + new Date().toLocaleTimeString();
+ container.appendChild(newElement);
+ updateStatus("New element added - DOM mutation recorded");
+ }
+
+ // Wait for analytics script to load
+ window.addEventListener("load", function () {
+ setTimeout(function () {
+ if (window.rybbit) {
+ updateStatus(
+ "Analytics script loaded successfully. Session replay should be active."
+ );
+ console.log(
+ "[Test Page] Session replay active:",
+ window.rybbit.isSessionReplayActive()
+ );
+ } else {
+ updateStatus("ERROR: Analytics script failed to load");
+ }
+ }, 1000);
+ });
+
+ // Monitor for console messages
+ const originalLog = console.log;
+ console.log = function (...args) {
+ if (
+ args[0] &&
+ typeof args[0] === "string" &&
+ args[0].includes("[Session Replay]")
+ ) {
+ // Highlight session replay logs
+ originalLog.apply(console, ["🎬", ...args]);
+ } else {
+ originalLog.apply(console, args);
+ }
+ };
+
+ const originalError = console.error;
+ console.error = function (...args) {
+ if (
+ args[0] &&
+ typeof args[0] === "string" &&
+ args[0].includes("session replay")
+ ) {
+ updateStatus("ERROR: " + args.join(" "));
+ }
+ originalError.apply(console, args);
+ };
+ </script>
+ </body>
+</html>
diff --git a/server/src/analytics-script/config.ts b/server/src/analytics-script/config.ts
index 9a660ceee..6702dbedc 100644
--- a/server/src/analytics-script/config.ts
+++ b/server/src/analytics-script/config.ts
@@ -44,6 +44,23 @@ export function parseScriptConfig(
const apiKey = scriptTag.getAttribute("data-api-key") || undefined;
+ const sessionReplayBatchSize = scriptTag.getAttribute(
+ "data-replay-batch-size"
+ )
+ ? Math.max(1, parseInt(scriptTag.getAttribute("data-replay-batch-size")!))
+ : 3; // Reduced from 50 to 3 for production stability
+
+ const sessionReplayBatchInterval = scriptTag.getAttribute(
+ "data-replay-batch-interval"
+ )
+ ? Math.max(
+ 1000,
+ parseInt(scriptTag.getAttribute("data-replay-batch-interval")!)
+ )
+ : 2000; // Reduced from 5000 to 2000ms for faster, smaller batches
+
+ console.info(scriptTag);
+
return {
analyticsHost,
siteId,
@@ -55,6 +72,10 @@ export function parseScriptConfig(
trackOutbound: scriptTag.getAttribute("data-track-outbound") !== "false",
enableWebVitals: scriptTag.getAttribute("data-web-vitals") === "true",
trackErrors: scriptTag.getAttribute("data-track-errors") === "true",
+ enableSessionReplay:
+ scriptTag.getAttribute("data-session-replay") === "true",
+ sessionReplayBatchSize,
+ sessionReplayBatchInterval,
skipPatterns,
maskPatterns,
apiKey,
diff --git a/server/src/analytics-script/index.ts b/server/src/analytics-script/index.ts
index a90519297..23bc2e122 100644
--- a/server/src/analytics-script/index.ts
+++ b/server/src/analytics-script/index.ts
@@ -31,6 +31,9 @@ declare global {
identify: () => {},
clearUserId: () => {},
getUserId: () => null,
+ startSessionReplay: () => {},
+ stopSessionReplay: () => {},
+ isSessionReplayActive: () => false,
};
return;
}
@@ -132,15 +135,23 @@ declare global {
history.pushState = function (...args) {
originalPushState.apply(this, args);
debouncedTrackPageview();
+ tracker.onPageChange();
};
history.replaceState = function (...args) {
originalReplaceState.apply(this, args);
debouncedTrackPageview();
+ tracker.onPageChange();
};
- window.addEventListener("popstate", debouncedTrackPageview);
- window.addEventListener("hashchange", debouncedTrackPageview);
+ window.addEventListener("popstate", () => {
+ debouncedTrackPageview();
+ tracker.onPageChange();
+ });
+ window.addEventListener("hashchange", () => {
+ debouncedTrackPageview();
+ tracker.onPageChange();
+ });
}
}
@@ -154,11 +165,19 @@ declare global {
identify: (userId: string) => tracker.identify(userId),
clearUserId: () => tracker.clearUserId(),
getUserId: () => tracker.getUserId(),
+ startSessionReplay: () => tracker.startSessionReplay(),
+ stopSessionReplay: () => tracker.stopSessionReplay(),
+ isSessionReplayActive: () => tracker.isSessionReplayActive(),
};
// Initialize
setupEventListeners();
+ // Setup cleanup on page unload
+ window.addEventListener("beforeunload", () => {
+ tracker.cleanup();
+ });
+
// Track initial pageview if enabled
if (config!.autoTrackPageview) {
tracker.trackPageview();
diff --git a/server/src/analytics-script/sessionReplay.ts b/server/src/analytics-script/sessionReplay.ts
new file mode 100644
index 000000000..20ae32381
--- /dev/null
+++ b/server/src/analytics-script/sessionReplay.ts
@@ -0,0 +1,286 @@
+import {
+ ScriptConfig,
+ SessionReplayEvent,
+ SessionReplayBatch,
+} from "./types.js";
+
+// rrweb types (simplified for our use case)
+declare global {
+ interface Window {
+ rrweb?: {
+ record: (options: {
+ emit: (event: any) => void;
+ checkoutEveryNms?: number;
+ checkoutEveryNth?: number;
+ maskAllInputs?: boolean;
+ maskInputOptions?: any;
+ slimDOMOptions?: any;
+ sampling?: any;
+ recordCanvas?: boolean;
+ collectFonts?: boolean;
+ }) => () => void;
+ };
+ }
+}
+
+export class SessionReplayRecorder {
+ private config: ScriptConfig;
+ private isRecording: boolean = false;
+ private stopRecordingFn?: () => void;
+ private userId: string;
+ private eventBuffer: SessionReplayEvent[] = [];
+ private batchTimer?: number;
+ private sendBatch: (batch: SessionReplayBatch) => Promise<void>;
+
+ constructor(
+ config: ScriptConfig,
+ userId: string,
+ sendBatch: (batch: SessionReplayBatch) => Promise<void>
+ ) {
+ this.config = config;
+ this.userId = userId;
+ this.sendBatch = sendBatch;
+ }
+
+ async initialize(): Promise<void> {
+ if (!this.config.enableSessionReplay) {
+ return;
+ }
+
+ // Load rrweb if not already loaded
+ if (!window.rrweb) {
+ await this.loadRrweb();
+ }
+
+ if (window.rrweb) {
+ this.startRecording();
+ } else {
+ console.warn("Failed to load rrweb, session replay disabled");
+ }
+ }
+
+ private async loadRrweb(): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const script = document.createElement("script");
+ script.src =
+ "https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js";
+ script.async = false; // Load synchronously to ensure immediate availability
+ script.onload = () => {
+ console.log("[Session Replay] rrweb loaded successfully");
+ resolve();
+ };
+ script.onerror = () => reject(new Error("Failed to load rrweb"));
+ document.head.appendChild(script);
+ });
+ }
+
+ public startRecording(): void {
+ if (this.isRecording || !window.rrweb || !this.config.enableSessionReplay) {
+ console.log("[Session Replay] Cannot start recording:", {
+ isRecording: this.isRecording,
+ hasRrweb: !!window.rrweb,
+ enableSessionReplay: this.config.enableSessionReplay,
+ });
+ return;
+ }
+
+ console.log(
+ "[Session Replay] Starting recording at",
+ new Date().toISOString()
+ );
+ console.log("[Session Replay] Document ready state:", document.readyState);
+
+ try {
+ this.stopRecordingFn = window.rrweb.record({
+ emit: (event) => {
+ const eventTypeNames = {
+ 0: "DOMContentLoaded",
+ 1: "Load",
+ 2: "FullSnapshot",
+ 3: "IncrementalSnapshot",
+ 4: "Meta",
+ 5: "Custom",
+ 6: "Plugin",
+ };
+ const typeName =
+ eventTypeNames[event.type as keyof typeof eventTypeNames] ||
+ `Unknown(${event.type})`;
+ console.log(
+ `[Session Replay] Event collected: Type ${event.type} (${typeName}) at ${new Date(event.timestamp || Date.now()).toISOString()}`
+ );
+ this.addEvent({
+ type: event.type,
+ data: event.data,
+ timestamp: event.timestamp || Date.now(),
+ });
+ },
+ recordCanvas: true, // Record canvas elements
+ collectFonts: true, // Collect font info for better replay
+ checkoutEveryNms: 30000, // Checkout every 30 seconds
+ checkoutEveryNth: 200, // Checkout every 200 events
+ maskAllInputs: true, // Mask all input values for privacy
+ maskInputOptions: {
+ password: true,
+ email: true,
+ },
+ slimDOMOptions: {
+ script: false,
+ comment: true,
+ headFavicon: true,
+ headWhitespace: true,
+ headMetaDescKeywords: true,
+ headMetaSocial: true,
+ headMetaRobots: true,
+ headMetaHttpEquiv: true,
+ headMetaAuthorship: true,
+ headMetaVerification: true,
+ },
+ sampling: {
+ // Optional: reduce recording frequency to save bandwidth
+ mousemove: false, // Don't record every mouse move
+ mouseInteraction: true,
+ scroll: 150, // Sample scroll events every 150ms
+ input: "last", // Only record the final input value
+ },
+ });
+
+ this.isRecording = true;
+ this.setupBatchTimer();
+
+ console.log("Session replay recording started");
+ } catch (error) {
+ console.error("Failed to start session replay recording:", error);
+ }
+ }
+
+ public stopRecording(): void {
+ if (!this.isRecording) {
+ return;
+ }
+
+ if (this.stopRecordingFn) {
+ this.stopRecordingFn();
+ }
+
+ this.isRecording = false;
+ this.clearBatchTimer();
+
+ // Send any remaining events
+ if (this.eventBuffer.length > 0) {
+ this.flushEvents();
+ }
+
+ console.log("Session replay recording stopped");
+ }
+
+ public isActive(): boolean {
+ return this.isRecording;
+ }
+
+ private addEvent(event: SessionReplayEvent): void {
+ this.eventBuffer.push(event);
+ console.log(
+ `[Session Replay] Event added to buffer (${this.eventBuffer.length}/${this.config.sessionReplayBatchSize})`
+ );
+
+ // Auto-flush if buffer is full
+ if (this.eventBuffer.length >= this.config.sessionReplayBatchSize) {
+ console.log(
+ `[Session Replay] Buffer full, flushing ${this.eventBuffer.length} events`
+ );
+ this.flushEvents();
+ }
+ }
+
+ private setupBatchTimer(): void {
+ this.clearBatchTimer();
+ this.batchTimer = window.setInterval(() => {
+ if (this.eventBuffer.length > 0) {
+ console.log(
+ `[Session Replay] Timer triggered, flushing ${this.eventBuffer.length} events`
+ );
+ this.flushEvents();
+ }
+ }, this.config.sessionReplayBatchInterval);
+ }
+
+ private clearBatchTimer(): void {
+ if (this.batchTimer) {
+ clearInterval(this.batchTimer);
+ this.batchTimer = undefined;
+ }
+ }
+
+ private async flushEvents(): Promise<void> {
+ if (this.eventBuffer.length === 0) {
+ return;
+ }
+
+ const events = [...this.eventBuffer];
+ this.eventBuffer = [];
+
+ console.log(
+ `[Session Replay] Sending batch with ${events.length} events to server`
+ );
+ console.log(
+ `[Session Replay] Event types in batch:`,
+ events.map((e) => `Type ${e.type}`).join(", ")
+ );
+ console.log(
+ `[Session Replay] Batch size:`,
+ JSON.stringify(events).length,
+ "characters"
+ );
+
+ const batch: SessionReplayBatch = {
+ userId: this.userId,
+ events,
+ metadata: {
+ pageUrl: window.location.href,
+ viewportWidth: window.innerWidth,
+ viewportHeight: window.innerHeight,
+ language: navigator.language,
+ },
+ };
+
+ try {
+ await this.sendBatch(batch);
+ console.log(
+ `[Session Replay] Successfully sent batch with ${events.length} events`
+ );
+ } catch (error) {
+ console.error("Failed to send session replay batch:", error);
+ console.error("Failed batch details:", {
+ eventCount: events.length,
+ eventTypes: events.map((e) => e.type),
+ batchSize: JSON.stringify(batch).length,
+ userId: this.userId,
+ url: window.location.href,
+ });
+
+ // Re-queue the events for retry since this batch failed
+ console.log(
+ `[Session Replay] Re-queuing ${events.length} failed events for retry`
+ );
+ this.eventBuffer.unshift(...events);
+ }
+ }
+
+ // Update user ID when it changes
+ public updateUserId(userId: string): void {
+ this.userId = userId;
+ }
+
+ // Handle page navigation for SPAs
+ public onPageChange(): void {
+ if (this.isRecording) {
+ // Flush current events before page change
+ this.flushEvents();
+ }
+ }
+
+ // Cleanup on page unload
+ public cleanup(): void {
+ this.stopRecording();
+ }
+}
diff --git a/server/src/analytics-script/tracking.test.ts b/server/src/analytics-script/tracking.test.ts
index 6f4bbe85f..38096ff40 100644
--- a/server/src/analytics-script/tracking.test.ts
+++ b/server/src/analytics-script/tracking.test.ts
@@ -84,6 +84,9 @@ describe("Tracker", () => {
trackOutbound: true,
trackErrors: false,
enableWebVitals: false,
+ enableSessionReplay: false,
+ sessionReplayBatchSize: 50,
+ sessionReplayBatchInterval: 5000,
skipPatterns: [],
maskPatterns: [],
};
diff --git a/server/src/analytics-script/tracking.ts b/server/src/analytics-script/tracking.ts
index 1c8463ac7..7b3ca8f12 100644
--- a/server/src/analytics-script/tracking.ts
+++ b/server/src/analytics-script/tracking.ts
@@ -3,16 +3,23 @@ import {
ScriptConfig,
TrackingPayload,
WebVitalsData,
+ SessionReplayBatch,
} from "./types.js";
import { findMatchingPattern } from "./utils.js";
+import { SessionReplayRecorder } from "./sessionReplay.js";
export class Tracker {
private config: ScriptConfig;
private customUserId: string | null = null;
+ private sessionReplayRecorder?: SessionReplayRecorder;
constructor(config: ScriptConfig) {
this.config = config;
this.loadUserId();
+
+ if (config.enableSessionReplay) {
+ this.initializeSessionReplay();
+ }
}
private loadUserId(): void {
@@ -26,6 +33,41 @@ export class Tracker {
}
}
+ private async initializeSessionReplay(): Promise<void> {
+ try {
+ this.sessionReplayRecorder = new SessionReplayRecorder(
+ this.config,
+ this.customUserId || "",
+ (batch) => this.sendSessionReplayBatch(batch)
+ );
+ await this.sessionReplayRecorder.initialize();
+ } catch (error) {
+ console.error("Failed to initialize session replay:", error);
+ }
+ }
+
+ private async sendSessionReplayBatch(
+ batch: SessionReplayBatch
+ ): Promise<void> {
+ try {
+ await fetch(
+ `${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(batch),
+ mode: "cors",
+ keepalive: false, // Disable keepalive for large session replay requests
+ }
+ );
+ } catch (error) {
+ console.error("Failed to send session replay batch:", error);
+ throw error;
+ }
+ }
+
createBasePayload(): BasePayload | null {
const url = new URL(window.location.href);
let pathname = url.pathname;
@@ -192,28 +234,33 @@ export class Tracker {
if (filename) {
errorProperties.fileName = filename;
}
-
+
if (additionalInfo.lineno) {
- const lineNum = typeof additionalInfo.lineno === 'string'
- ? parseInt(additionalInfo.lineno, 10)
- : additionalInfo.lineno;
+ const lineNum =
+ typeof additionalInfo.lineno === "string"
+ ? parseInt(additionalInfo.lineno, 10)
+ : additionalInfo.lineno;
if (lineNum && lineNum !== 0) {
errorProperties.lineNumber = lineNum;
}
}
-
+
if (additionalInfo.colno) {
- const colNum = typeof additionalInfo.colno === 'string'
- ? parseInt(additionalInfo.colno, 10)
- : additionalInfo.colno;
+ const colNum =
+ typeof additionalInfo.colno === "string"
+ ? parseInt(additionalInfo.colno, 10)
+ : additionalInfo.colno;
if (colNum && colNum !== 0) {
errorProperties.columnNumber = colNum;
}
}
-
+
// Add any other additional info
for (const key in additionalInfo) {
- if (!['lineno', 'colno'].includes(key) && additionalInfo[key] !== undefined) {
+ if (
+ !["lineno", "colno"].includes(key) &&
+ additionalInfo[key] !== undefined
+ ) {
errorProperties[key] = additionalInfo[key];
}
}
@@ -233,6 +280,11 @@ export class Tracker {
} catch (e) {
console.warn("Could not persist user ID to localStorage");
}
+
+ // Update session replay recorder with new user ID
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.updateUserId(this.customUserId);
+ }
}
clearUserId(): void {
@@ -247,4 +299,37 @@ export class Tracker {
getUserId(): string | null {
return this.customUserId;
}
+
+ // Session Replay methods
+ startSessionReplay(): void {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.startRecording();
+ } else {
+ console.warn("Session replay not initialized");
+ }
+ }
+
+ stopSessionReplay(): void {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.stopRecording();
+ }
+ }
+
+ isSessionReplayActive(): boolean {
+ return this.sessionReplayRecorder?.isActive() ?? false;
+ }
+
+ // Handle page changes for SPA
+ onPageChange(): void {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.onPageChange();
+ }
+ }
+
+ // Cleanup
+ cleanup(): void {
+ if (this.sessionReplayRecorder) {
+ this.sessionReplayRecorder.cleanup();
+ }
+ }
}
diff --git a/server/src/analytics-script/types.ts b/server/src/analytics-script/types.ts
index d577d8524..ee64913ac 100644
--- a/server/src/analytics-script/types.ts
+++ b/server/src/analytics-script/types.ts
@@ -8,6 +8,9 @@ export interface ScriptConfig {
trackOutbound: boolean;
enableWebVitals: boolean;
trackErrors: boolean;
+ enableSessionReplay: boolean;
+ sessionReplayBatchSize: number;
+ sessionReplayBatchInterval: number;
skipPatterns: string[];
maskPatterns: string[];
apiKey?: string;
@@ -54,4 +57,24 @@ export interface RybbitAPI {
identify: (userId: string) => void;
clearUserId: () => void;
getUserId: () => string | null;
+ startSessionReplay: () => void;
+ stopSessionReplay: () => void;
+ isSessionReplayActive: () => boolean;
+}
+
+export interface SessionReplayEvent {
+ type: string | number;
+ data: any;
+ timestamp: number;
+}
+
+export interface SessionReplayBatch {
+ userId: string;
+ events: SessionReplayEvent[];
+ metadata?: {
+ pageUrl: string;
+ viewportWidth?: number;
+ viewportHeight?: number;
+ language?: string;
+ };
}
diff --git a/server/src/api/sessionReplay/getSessionReplayEvents.ts b/server/src/api/sessionReplay/getSessionReplayEvents.ts
new file mode 100644
index 000000000..54fde93d0
--- /dev/null
+++ b/server/src/api/sessionReplay/getSessionReplayEvents.ts
@@ -0,0 +1,31 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import { SessionReplayQueryService } from "../../services/replay/sessionReplayQueryService.js";
+
+export async function getSessionReplayEvents(
+ request: FastifyRequest<{
+ Params: { site: string; sessionId: string };
+ }>,
+ reply: FastifyReply
+) {
+ try {
+ const siteId = Number(request.params.site);
+ const { sessionId } = request.params;
+
+ const sessionReplayService = new SessionReplayQueryService();
+ const replayData = await sessionReplayService.getSessionReplayEvents(
+ siteId,
+ sessionId
+ );
+
+ return reply.send(replayData);
+ } catch (error) {
+ console.error("Error fetching session replay events:", error);
+ if (
+ error instanceof Error &&
+ error.message === "Session replay not found"
+ ) {
+ return reply.status(404).send({ error: "Session replay not found" });
+ }
+ return reply.status(500).send({ error: "Internal server error" });
+ }
+}
diff --git a/server/src/api/sessionReplay/getSessionReplays.ts b/server/src/api/sessionReplay/getSessionReplays.ts
new file mode 100644
index 000000000..6591866eb
--- /dev/null
+++ b/server/src/api/sessionReplay/getSessionReplays.ts
@@ -0,0 +1,42 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import { FilterParams } from "@rybbit/shared";
+import { SessionReplayQueryService } from "../../services/replay/sessionReplayQueryService.js";
+
+export async function getSessionReplays(
+ request: FastifyRequest<{
+ Params: { site: string };
+ Querystring: FilterParams<{
+ limit?: string;
+ offset?: string;
+ userId?: string;
+ }>;
+ }>,
+ reply: FastifyReply
+) {
+ try {
+ const siteId = Number(request.params.site);
+ const { limit, offset, userId, filters } = request.query;
+
+ const sessionReplayService = new SessionReplayQueryService();
+ const replays = await sessionReplayService.getSessionReplayList(siteId, {
+ limit: limit ? Number(limit) : 50,
+ offset: offset ? Number(offset) : 0,
+ userId: userId || undefined,
+ startDate: request.query.startDate,
+ endDate: request.query.endDate,
+ timeZone: request.query.timeZone,
+ pastMinutesStart: request.query.pastMinutesStart
+ ? Number(request.query.pastMinutesStart)
+ : undefined,
+ pastMinutesEnd: request.query.pastMinutesEnd
+ ? Number(request.query.pastMinutesEnd)
+ : undefined,
+ filters: filters || "",
+ });
+
+ return reply.send({ data: replays });
+ } catch (error) {
+ console.error("Error fetching session replays:", error);
+ return reply.status(500).send({ error: "Internal server error" });
+ }
+}
diff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.ts
new file mode 100644
index 000000000..be5a34970
--- /dev/null
+++ b/server/src/api/sessionReplay/recordSessionReplay.ts
@@ -0,0 +1,81 @@
+import { FastifyReply, FastifyRequest } from "fastify";
+import { z } from "zod";
+import { SessionReplayIngestService } from "../../services/replay/sessionReplayIngestService.js";
+import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
+
+const recordSessionReplaySchema = z.object({
+ userId: z.string(),
+ events: z.array(
+ z.object({
+ type: z.union([z.string(), z.number()]),
+ data: z.any(),
+ timestamp: z.number(),
+ })
+ ),
+ metadata: z
+ .object({
+ pageUrl: z.string(),
+ viewportWidth: z.number().optional(),
+ viewportHeight: z.number().optional(),
+ language: z.string().optional(),
+ })
+ .optional(),
+});
+
+export async function recordSessionReplay(
+ request: FastifyRequest<{
+ Params: { site: string };
+ Body: RecordSessionReplayRequest;
+ }>,
+ reply: FastifyReply
+) {
+ try {
+ const siteId = Number(request.params.site);
+ const body = recordSessionReplaySchema.parse(
+ request.body
+ ) as RecordSessionReplayRequest;
+
+ // Extract request metadata for tracking
+ const userAgent = request.headers["user-agent"] || "";
+ const ipAddress = getIpAddress(request);
+ const origin = request.headers.origin || "";
+ const referrer = request.headers.referer || "";
+
+ const sessionReplayService = new SessionReplayIngestService();
+ await sessionReplayService.recordEvents(siteId, body, {
+ userAgent,
+ ipAddress,
+ origin,
+ referrer,
+ });
+
+ return reply.send({ success: true });
+ } catch (error) {
+ if (error instanceof z.ZodError) {
+ return reply.status(400).send({ error: error.errors });
+ }
+ console.error("Error recording session replay:", error);
+ return reply.status(500).send({ error: "Internal server error" });
+ }
+}
+
+// Helper function to get IP address
+const getIpAddress = (request: FastifyRequest): string => {
+ const cfConnectingIp = request.headers["cf-connecting-ip"];
+ if (cfConnectingIp && typeof cfConnectingIp === "string") {
+ return cfConnectingIp.trim();
+ }
+
+ const forwardedFor = request.headers["x-forwarded-for"];
+ if (forwardedFor && typeof forwardedFor === "string") {
+ const ips = forwardedFor
+ .split(",")
+ .map((ip) => ip.trim())
+ .filter(Boolean);
+ if (ips.length > 0) {
+ return ips[ips.length - 1];
+ }
+ }
+
+ return request.ip;
+};
diff --git a/server/src/cron/index.ts b/server/src/cron/index.ts
index f67b17376..dadb5efdb 100644
--- a/server/src/cron/index.ts
+++ b/server/src/cron/index.ts
@@ -1,5 +1,4 @@
import * as cron from "node-cron";
-import { cleanupOldSessions } from "../db/postgres/session-cleanup.js";
import { IS_CLOUD } from "../lib/const.js";
import { updateOrganizationsMonthlyUsage } from "./monthly-usage-checker.js";
@@ -7,11 +6,10 @@ export async function initializeCronJobs() {
console.log("Initializing cron jobs...");
if (IS_CLOUD && process.env.NODE_ENV !== "development") {
- // Schedule the monthly usage checker to run every 5 minutes
+ // Schedule the monthly usage checker to run every 30 minutes
cron.schedule("*/30 * * * *", updateOrganizationsMonthlyUsage);
updateOrganizationsMonthlyUsage();
}
- cron.schedule("* * * * *", cleanupOldSessions);
console.log("Cron jobs initialized successfully");
}
diff --git a/server/src/db/clickhouse/clickhouse.ts b/server/src/db/clickhouse/clickhouse.ts
index b98917a4d..6a8b362b4 100644
--- a/server/src/db/clickhouse/clickhouse.ts
+++ b/server/src/db/clickhouse/clickhouse.ts
@@ -58,6 +58,67 @@ export const initializeClickhouse = async () => {
`,
});
+ // Create session replay tables
+ await clickhouse.exec({
+ query: `
+ CREATE TABLE IF NOT EXISTS session_replay_events (
+ site_id UInt16,
+ session_id String,
+ user_id String,
+ timestamp DateTime64(3),
+ event_type LowCardinality(String),
+ event_data String,
+ sequence_number UInt32,
+ event_size_bytes UInt32,
+ viewport_width Nullable(UInt16),
+ viewport_height Nullable(UInt16),
+ is_complete UInt8 DEFAULT 0
+ )
+ ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(timestamp)
+ ORDER BY (site_id, session_id, sequence_number)
+ TTL toDateTime(timestamp) + INTERVAL 30 DAY
+ `,
+ });
+
+ await clickhouse.exec({
+ query: `
+ CREATE TABLE IF NOT EXISTS session_replay_metadata (
+ site_id UInt16,
+ session_id String,
+ user_id String,
+ start_time DateTime,
+ end_time Nullable(DateTime),
+ duration_ms Nullable(UInt32),
+ event_count UInt32,
+ compressed_size_bytes UInt32,
+ page_url String,
+ country LowCardinality(FixedString(2)),
+ region LowCardinality(String),
+ city String,
+ lat Float64,
+ lon Float64,
+ browser LowCardinality(String),
+ browser_version LowCardinality(String),
+ operating_system LowCardinality(String),
+ operating_system_version LowCardinality(String),
+ language LowCardinality(String),
+ screen_width UInt16,
+ screen_height UInt16,
+ device_type LowCardinality(String),
+ channel String,
+ hostname String,
+ referrer String,
+ has_replay_data UInt8 DEFAULT 1,
+ created_at DateTime DEFAULT now()
+ )
+ ENGINE = ReplacingMergeTree(created_at)
+ PARTITION BY toYYYYMM(start_time)
+ ORDER BY (site_id, session_id)
+ TTL start_time + INTERVAL 30 DAY
+ `,
+ });
+
if (IS_CLOUD) {
await clickhouse.exec({
query: `
diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.ts
index dbf8bf017..d2669b1b6 100644
--- a/server/src/db/postgres/schema.ts
+++ b/server/src/db/postgres/schema.ts
@@ -83,19 +83,8 @@ export const activeSessions = pgTable("active_sessions", {
sessionId: text("session_id").primaryKey().notNull(),
siteId: integer("site_id"),
userId: text("user_id"),
- hostname: text("hostname"),
startTime: timestamp("start_time").defaultNow(),
lastActivity: timestamp("last_activity").defaultNow(),
- pageviews: integer("pageviews").default(0),
- entryPage: text("entry_page"),
- exitPage: text("exit_page"),
- deviceType: text("device_type"),
- screenWidth: integer("screen_width"),
- screenHeight: integer("screen_height"),
- browser: text("browser"),
- operatingSystem: text("operating_system"),
- language: text("language"),
- referrer: text("referrer"),
});
export const funnels = pgTable(
diff --git a/server/src/db/postgres/session-cleanup.ts b/server/src/db/postgres/session-cleanup.ts
deleted file mode 100644
index 8ffa9abaf..000000000
--- a/server/src/db/postgres/session-cleanup.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { sql } from "./postgres.js";
-
-// function convertPostgresToClickhouse(postgresTimestamp: string): string {
-// console.log("Parsing timestamp:", postgresTimestamp);
-// const dateTime = DateTime.fromISO(postgresTimestamp, {
-// zone: "utc",
-// });
-// if (!dateTime.isValid) {
-// console.error("Invalid timestamp:", postgresTimestamp);
-// console.error("Reason:", dateTime.invalidReason);
-// throw new Error("Invalid timestamp format");
-// }
-// return dateTime.toFormat("yyyy-MM-dd HH:mm:ss");
-// }
-
-// luxon is not working, so we need to use this
-function convertPostgresToClickhouse(postgresTimestamp: string): string {
- try {
- const date = new Date(postgresTimestamp);
- if (isNaN(date.getTime())) {
- throw new Error("Invalid Date");
- }
- const year = date.getUTCFullYear();
- const month = String(date.getUTCMonth() + 1).padStart(2, "0");
- const day = String(date.getUTCDate()).padStart(2, "0");
- const hours = String(date.getUTCHours()).padStart(2, "0");
- const minutes = String(date.getUTCMinutes()).padStart(2, "0");
- const seconds = String(date.getUTCSeconds()).padStart(2, "0");
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- } catch (error) {
- console.error("Error parsing timestamp:", postgresTimestamp, error);
- throw new Error("Invalid timestamp format");
- }
-}
-
-export async function cleanupOldSessions() {
- const deletedSessions = await sql`
- DELETE FROM active_sessions
- WHERE last_activity < NOW() - INTERVAL '30 minute'
- RETURNING *
- `;
-
- // console.log(`Cleaned up ${deletedSessions.length} sessions`)
-}
diff --git a/server/src/index.ts b/server/src/index.ts
index 9556700c3..fac1033b3 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -38,6 +38,9 @@ import { updateGoal } from "./api/analytics/goals/updateGoal.js";
import { getPerformanceByDimension } from "./api/analytics/performance/getPerformanceByDimension.js";
import { getPerformanceOverview } from "./api/analytics/performance/getPerformanceOverview.js";
import { getPerformanceTimeSeries } from "./api/analytics/performance/getPerformanceTimeSeries.js";
+import { recordSessionReplay } from "./api/sessionReplay/recordSessionReplay.js";
+import { getSessionReplays } from "./api/sessionReplay/getSessionReplays.js";
+import { getSessionReplayEvents } from "./api/sessionReplay/getSessionReplayEvents.js";
import { getConfig } from "./api/getConfig.js";
import { addSite } from "./api/sites/addSite.js";
import { changeSiteBlockBots } from "./api/sites/changeSiteBlockBots.js";
@@ -78,15 +81,18 @@ const server = Fastify({
},
maxParamLength: 1500,
trustProxy: true,
+ bodyLimit: 10 * 1024 * 1024, // 10MB limit for session replay data
});
server.register(cors, {
origin: (origin, callback) => {
- if (!origin || allowList.includes(normalizeOrigin(origin))) {
- callback(null, true);
- } else {
- callback(new Error("Not allowed by CORS"), false);
- }
+ callback(null, true);
+
+ // if (!origin || allowList.includes(normalizeOrigin(origin))) {
+ // callback(null, true);
+ // } else {
+ // callback(new Error("Not allowed by CORS"), false);
+ // }
},
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
@@ -128,12 +134,17 @@ server.register(
const PUBLIC_ROUTES: string[] = [
"/api/health",
"/api/track",
+ "/track",
"/api/script.js", // Updated script route
+ "/api/script-full.js", // Updated script route
+ "/script-full.js", // Updated script route
+ "/script.js", // Updated script route
"/api/config",
"/api/auth",
"/api/auth/callback/google",
"/api/auth/callback/github",
"/api/stripe/webhook",
+ "/api/session-replay/record",
];
// Define analytics routes that can be public
@@ -169,6 +180,7 @@ const ANALYTICS_ROUTES = [
"/api/error-names/",
"/api/error-events/",
"/api/error-bucketed/",
+ "/api/session-replay/",
];
server.addHook("onRequest", async (request, reply) => {
@@ -197,7 +209,7 @@ server.addHook("onRequest", async (request, reply) => {
const session = await getSessionFromReq(request);
if (!session) {
- return reply.status(401).send({ error: "Unauthorized" });
+ return reply.status(401).send({ error: "Unauthorized 1" });
}
// Attach session user info to request
@@ -257,6 +269,11 @@ server.get("/api/performance/overview/:site", getPerformanceOverview);
server.get("/api/performance/time-series/:site", getPerformanceTimeSeries);
server.get("/api/performance/by-dimension/:site", getPerformanceByDimension);
+// Session Replay
+server.post("/api/session-replay/record/:site", recordSessionReplay);
+server.get("/api/session-replay/list/:site", getSessionReplays);
+server.get("/api/session-replay/:sessionId/:site", getSessionReplayEvents);
+
// Administrative
server.get("/api/config", getConfig);
server.post("/api/add-site", addSite);
@@ -291,7 +308,9 @@ if (IS_CLOUD) {
server.get("/api/admin/organizations", getAdminOrganizations);
}
+server.post("/track", trackEvent);
server.post("/api/track", trackEvent);
+
server.get("/api/health", { logLevel: "silent" }, (_, reply) =>
reply.send("OK")
);
diff --git a/server/src/services/replay/sessionReplayIngestService.ts b/server/src/services/replay/sessionReplayIngestService.ts
new file mode 100644
index 000000000..87e5ec761
--- /dev/null
+++ b/server/src/services/replay/sessionReplayIngestService.ts
@@ -0,0 +1,191 @@
+import { DateTime } from "luxon";
+import { clickhouse } from "../../db/clickhouse/clickhouse.js";
+import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";
+import { processResults } from "../../api/analytics/utils.js";
+import { parseTrackingData } from "./trackingUtils.js";
+import { sessionsService } from "../sessions/sessionsService.js";
+import { userIdService } from "../userId/userIdService.js";
+
+export interface RequestMetadata {
+ userAgent: string;
+ ipAddress: string;
+ origin: string;
+ referrer: string;
+}
+
+/**
+ * Service responsible for ingesting session replay data
+ * Handles recording events and updating metadata
+ */
+export class SessionReplayIngestService {
+ async recordEvents(
+ siteId: number,
+ request: RecordSessionReplayRequest,
+ requestMeta?: RequestMetadata
+ ): Promise<void> {
+ const { userId: clientUserId, events, metadata } = request;
+
+ // Generate user ID server-side if not provided by client
+ const userId = clientUserId && clientUserId.trim()
+ ? clientUserId.trim()
+ : userIdService.generateUserId(
+ requestMeta?.ipAddress || "",
+ requestMeta?.userAgent || "",
+ siteId
+ );
+
+ // Get or create a session ID from the sessions service
+ const { sessionId } = await sessionsService.updateSession({
+ userId,
+ site_id: siteId.toString(),
+ });
+
+ // Prepare events for batch insert
+ const eventsToInsert = events.map((event, index) => ({
+ site_id: siteId,
+ session_id: sessionId,
+ user_id: userId,
+ timestamp: event.timestamp,
+ event_type: event.type,
+ event_data: JSON.stringify(event.data),
+ sequence_number: index,
+ event_size_bytes: JSON.stringify(event.data).length,
+ viewport_width: metadata?.viewportWidth || null,
+ viewport_height: metadata?.viewportHeight || null,
+ is_complete: 0,
+ }));
+
+ // Batch insert events
+ if (eventsToInsert.length > 0) {
+ await clickhouse.insert({
+ table: "session_replay_events",
+ values: eventsToInsert,
+ format: "JSONEachRow",
+ });
+ }
+
+ // Update or insert metadata
+ if (metadata) {
+ await this.updateSessionMetadata(
+ siteId,
+ sessionId,
+ userId,
+ metadata,
+ requestMeta
+ );
+ }
+ }
+
+ private async updateSessionMetadata(
+ siteId: number,
+ sessionId: string,
+ userId: string,
+ metadata: any,
+ requestMeta?: RequestMetadata
+ ): Promise<void> {
+ // Get existing session info from events table
+ const sessionInfo = await clickhouse.query({
+ query: `
+ SELECT
+ MIN(timestamp) as start_time,
+ MAX(timestamp) as end_time,
+ COUNT() as event_count,
+ SUM(event_size_bytes) as compressed_size_bytes,
+ MAX(viewport_width) as screen_width,
+ MAX(viewport_height) as screen_height
+ FROM session_replay_events
+ WHERE site_id = {siteId:UInt16}
+ AND session_id = {sessionId:String}
+ `,
+ query_params: { siteId, sessionId },
+ format: "JSONEachRow",
+ });
+
+ type SessionInfoResult = {
+ start_time: string;
+ end_time: string | null;
+ event_count: number;
+ compressed_size_bytes: number;
+ screen_width: number | null;
+ screen_height: number | null;
+ };
+
+ const sessionResults = await processResults<SessionInfoResult>(sessionInfo);
+
+ if (!sessionResults || sessionResults.length === 0) return;
+
+ const sessionReplayData = sessionResults[0];
+
+ // Parse tracking data from request metadata
+ let trackingData: any = {};
+ if (requestMeta?.userAgent) {
+ try {
+ // Extract hostname from the page URL
+ const urlObj = new URL(metadata.pageUrl);
+ const hostname = urlObj.hostname;
+
+ trackingData = await parseTrackingData(
+ requestMeta.userAgent,
+ requestMeta.ipAddress,
+ requestMeta.referrer || "",
+ urlObj.search || "", // querystring from URL
+ hostname,
+ metadata.language || "", // language from client
+ sessionReplayData.screen_width || metadata.viewportWidth || 0,
+ sessionReplayData.screen_height || metadata.viewportHeight || 0
+ );
+ } catch (error) {
+ console.error("Error parsing tracking data for session replay:", error);
+ }
+ }
+
+ // Calculate duration
+ const startTime = new Date(sessionReplayData.start_time);
+ const endTime = sessionReplayData.end_time
+ ? new Date(sessionReplayData.end_time)
+ : null;
+ const durationMs = endTime ? endTime.getTime() - startTime.getTime() : null;
+
+ // Insert or update metadata
+ await clickhouse.insert({
+ table: "session_replay_metadata",
+ values: [
+ {
+ site_id: siteId,
+ session_id: sessionId,
+ user_id: userId,
+ start_time: DateTime.fromJSDate(startTime).toFormat(
+ "yyyy-MM-dd HH:mm:ss"
+ ),
+ end_time: endTime
+ ? DateTime.fromJSDate(endTime).toFormat("yyyy-MM-dd HH:mm:ss")
+ : null,
+ duration_ms: durationMs,
+ event_count: sessionReplayData.event_count || 0,
+ compressed_size_bytes: sessionReplayData.compressed_size_bytes || 0,
+ page_url: metadata.pageUrl || "",
+ country: trackingData.country || "",
+ region: trackingData.region || "",
+ city: trackingData.city || "",
+ lat: trackingData.lat || 0,
+ lon: trackingData.lon || 0,
+ browser: trackingData.browser || "",
+ browser_version: trackingData.browserVersion || "",
+ operating_system: trackingData.operatingSystem || "",
+ operating_system_version: trackingData.operatingSystemVersion || "",
+ language: trackingData.language || "",
+ screen_width:
+ sessionReplayData.screen_width || metadata?.viewportWidth || 0,
+ screen_height:
+ sessionReplayData.screen_height || metadata?.viewportHeight || 0,
+ device_type: trackingData.deviceType || "",
+ channel: trackingData.channel || "",
+ hostname: trackingData.hostname || "",
+ referrer: trackingData.referrer || "",
+ has_replay_data: 1,
+ },
+ ],
+ format: "JSONEachRow",
+ });
+ }
+}
diff --git a/server/src/services/replay/sessionReplayQueryService.ts b/server/src/services/replay/sessionReplayQueryService.ts
new file mode 100644
index 000000000..9f28f35d9
--- /dev/null
+++ b/server/src/services/replay/sessionReplayQueryService.ts
@@ -0,0 +1,209 @@
+import { clickhouse } from "../../db/clickhouse/clickhouse.js";
+import {
+ SessionReplayMetadata,
+ SessionReplayListItem,
+ GetSessionReplayEventsResponse,
+} from "../../types/sessionReplay.js";
+import {
+ processResults,
+ getTimeStatement,
+ getFilterStatement,
+} from "../../api/analytics/utils.js";
+import { FilterParams } from "@rybbit/shared";
+
+/**
+ * Service responsible for querying/retrieving session replay data
+ * Handles listing sessions and getting replay events
+ */
+export class SessionReplayQueryService {
+ async getSessionReplayList(
+ siteId: number,
+ options: {
+ limit?: number;
+ offset?: number;
+ userId?: string;
+ } & Pick<
+ FilterParams,
+ | "startDate"
+ | "endDate"
+ | "timeZone"
+ | "pastMinutesStart"
+ | "pastMinutesEnd"
+ | "filters"
+ >
+ ): Promise<SessionReplayListItem[]> {
+ const { limit = 50, offset = 0, userId } = options;
+
+ const timeStatement = getTimeStatement(options).replace(
+ /timestamp/g,
+ "start_time"
+ );
+
+ const filterStatement = getFilterStatement(options.filters || "");
+
+ let whereConditions = [`site_id = {siteId:UInt16}`];
+ const queryParams: any = { siteId, limit, offset };
+
+ if (userId) {
+ whereConditions.push(`user_id = {userId:String}`);
+ queryParams.userId = userId;
+ }
+
+ // Build the base query for session IDs that have replay events
+ let sessionIdsSubquery = `
+ SELECT DISTINCT session_id
+ FROM session_replay_events
+ WHERE site_id = {siteId:UInt16} AND event_type = '2'
+ `;
+
+ // If filters are present, we need to further filter by sessions that match the filter criteria
+ if (filterStatement) {
+ sessionIdsSubquery = `
+ SELECT DISTINCT srm.session_id
+ FROM session_replay_metadata srm
+ FINAL
+ WHERE srm.site_id = {siteId:UInt16}
+ AND srm.session_id IN (
+ SELECT DISTINCT session_id
+ FROM session_replay_events
+ WHERE site_id = {siteId:UInt16} AND event_type = '2'
+ )
+ AND srm.session_id IN (
+ SELECT DISTINCT session_id
+ FROM events
+ WHERE site_id = {siteId:UInt16}
+ ${filterStatement}
+ )
+ `;
+ }
+
+ const query = `
+ SELECT
+ session_id,
+ user_id,
+ start_time,
+ end_time,
+ duration_ms,
+ page_url,
+ event_count,
+ country,
+ region,
+ city,
+ browser,
+ browser_version,
+ operating_system,
+ operating_system_version,
+ device_type,
+ screen_width,
+ screen_height
+ FROM session_replay_metadata
+ FINAL
+ WHERE ${whereConditions.join(" AND ")}
+ AND event_count >= 2
+ AND session_id IN (${sessionIdsSubquery})
+ ${timeStatement}
+ ORDER BY start_time DESC
+ LIMIT {limit:UInt32}
+ OFFSET {offset:UInt32}
+ `;
+
+ const result = await clickhouse.query({
+ query,
+ query_params: queryParams,
+ format: "JSONEachRow",
+ });
+
+ const rawResults = await processResults<any>(result);
+
+ const finalResults = rawResults;
+
+ return finalResults;
+ }
+
+ async getSessionReplayEvents(
+ siteId: number,
+ sessionId: string
+ ): Promise<GetSessionReplayEventsResponse> {
+ // Get metadata
+ const metadataResult = await clickhouse.query({
+ query: `
+ SELECT *
+ FROM session_replay_metadata
+ FINAL
+ WHERE site_id = {siteId:UInt16}
+ AND session_id = {sessionId:String}
+ LIMIT 1
+ `,
+ query_params: { siteId, sessionId },
+ format: "JSONEachRow",
+ });
+
+ const metadataResults = await processResults<any>(metadataResult);
+ const metadata = metadataResults[0];
+
+ if (!metadata) {
+ throw new Error("Session replay not found");
+ }
+
+ // Get events
+ const eventsResult = await clickhouse.query({
+ query: `
+ SELECT
+ toUnixTimestamp64Milli(timestamp) as timestamp,
+ event_type as type,
+ event_data as data
+ FROM session_replay_events
+ WHERE site_id = {siteId:UInt16}
+ AND session_id = {sessionId:String}
+ ORDER BY timestamp ASC, sequence_number ASC
+ `,
+ query_params: { siteId, sessionId },
+ format: "JSONEachRow",
+ });
+
+ type EventRow = {
+ timestamp: number;
+ type: string;
+ data: string;
+ };
+
+ const eventsResults = await processResults<EventRow>(eventsResult);
+
+ const events = eventsResults.map((event) => {
+ // Timestamp is already in milliseconds from the SQL query
+ const timestamp = event.timestamp;
+
+ return {
+ timestamp,
+ type: event.type, // Keep as string for now to match interface
+ data: JSON.parse(event.data),
+ };
+ });
+
+ return {
+ events,
+ metadata,
+ };
+ }
+
+ async getSessionReplayMetadata(
+ siteId: number,
+ sessionId: string
+ ): Promise<SessionReplayMetadata | null> {
+ const result = await clickhouse.query({
+ query: `
+ SELECT *
+ FROM session_replay_metadata
+ FINAL
+ WHERE site_id = {siteId:UInt16}
+ AND session_id = {sessionId:String}
+ LIMIT 1
+ `,
+ query_params: { siteId, sessionId },
+ format: "JSONEachRow",
+ });
+
+ const results = await processResults<SessionReplayMetadata>(result);
+ return results[0] || null;
+ }
+}
diff --git a/server/src/services/replay/trackingUtils.ts b/server/src/services/replay/trackingUtils.ts
new file mode 100644
index 000000000..bcc6d665e
--- /dev/null
+++ b/server/src/services/replay/trackingUtils.ts
@@ -0,0 +1,122 @@
+import { UAParser } from "ua-parser-js";
+import { getLocation } from "../../db/geolocation/geolocation.js";
+import { getChannel } from "../../tracker/getChannel.js";
+import { getDeviceType } from "../../utils.js";
+
+export interface TrackingData {
+ browser: string;
+ browserVersion: string;
+ operatingSystem: string;
+ operatingSystemVersion: string;
+ deviceType: string;
+ country: string;
+ region: string;
+ city: string;
+ lat: number;
+ lon: number;
+ channel: string;
+ language: string;
+ hostname: string;
+ referrer: string;
+}
+
+/**
+ * Parse tracking data from user agent, IP address, and other request data
+ * This is shared between pageview tracking and session replay
+ */
+export async function parseTrackingData(
+ userAgent: string,
+ ipAddress: string,
+ referrer: string,
+ querystring: string,
+ hostname: string,
+ language: string,
+ screenWidth: number,
+ screenHeight: number
+): Promise<TrackingData> {
+ // Parse user agent
+ const ua = UAParser(userAgent);
+
+ // Get geolocation data
+ let geoData: any = {};
+ try {
+ geoData = await getLocation(ipAddress);
+ } catch (error) {
+ console.error("Error getting geo data for session replay:", error);
+ }
+
+ const countryCode = geoData?.countryIso || "";
+ const regionCode = geoData?.subdivisions?.[0]?.isoCode || "";
+ const latitude = geoData?.latitude || 0;
+ const longitude = geoData?.longitude || 0;
+ const city = geoData?.city || "";
+
+ // Clear self-referrer if it's from the same domain
+ if (referrer && hostname) {
+ try {
+ const referrerUrl = new URL(referrer);
+ if (referrerUrl.hostname === hostname) {
+ referrer = "";
+ }
+ } catch (e) {
+ // Invalid URL, keep original referrer
+ }
+ }
+
+ return {
+ browser: ua.browser.name || "Unknown",
+ browserVersion: ua.browser.major || "",
+ operatingSystem: ua.os.name || "Unknown",
+ operatingSystemVersion: ua.os.version || "",
+ deviceType: getDeviceType(screenWidth, screenHeight, ua) || "desktop",
+ country: countryCode,
+ region: countryCode && regionCode ? countryCode + "-" + regionCode : "",
+ city: city || "",
+ lat: latitude || 0,
+ lon: longitude || 0,
+ channel: getChannel(referrer, querystring, hostname),
+ language: language || "",
+ hostname: hostname || "",
+ referrer: referrer || "",
+ };
+}
+
+/**
+ * Extract browser info from user agent
+ */
+export function getBrowserInfo(userAgent: string): {
+ name: string;
+ version: string;
+} {
+ const ua = UAParser(userAgent);
+ return {
+ name: ua.browser.name || "Unknown",
+ version: ua.browser.major || "",
+ };
+}
+
+/**
+ * Extract OS info from user agent
+ */
+export function getOSInfo(userAgent: string): {
+ name: string;
+ version: string;
+} {
+ const ua = UAParser(userAgent);
+ return {
+ name: ua.os.name || "Unknown",
+ version: ua.os.version || "",
+ };
+}
+
+/**
+ * Get device type from user agent and screen dimensions
+ */
+export function getDeviceTypeFromUA(
+ userAgent: string,
+ screenWidth: number,
+ screenHeight: number
+): string {
+ const ua = UAParser(userAgent);
+ return getDeviceType(screenWidth, screenHeight, ua);
+}
diff --git a/server/src/services/sessions/sessionsService.ts b/server/src/services/sessions/sessionsService.ts
new file mode 100644
index 000000000..5cc77c25a
--- /dev/null
+++ b/server/src/services/sessions/sessionsService.ts
@@ -0,0 +1,105 @@
+import { and, eq, lt } from "drizzle-orm";
+import * as cron from "node-cron";
+import { db } from "../../db/postgres/postgres.js";
+import { activeSessions } from "../../db/postgres/schema.js";
+
+class SessionsService {
+ private cleanupTask: cron.ScheduledTask | null = null;
+
+ constructor() {
+ this.initializeCleanupCron();
+ }
+
+ private initializeCleanupCron() {
+ // Schedule session cleanup to run every minute
+ this.cleanupTask = cron.schedule("* * * * *", async () => {
+ try {
+ const deletedCount = await this.cleanupOldSessions();
+ // Uncomment for debugging
+ console.log(
+ `[SessionsService] Cleaned up ${deletedCount} expired sessions`
+ );
+ } catch (error) {
+ console.error("[SessionsService] Error during session cleanup:", error);
+ }
+ });
+
+ console.log(
+ "[SessionsService] Session cleanup cron initialized (runs every minute)"
+ );
+ }
+ async getExistingSession(userId: string, siteId: string) {
+ const siteIdNumber = parseInt(siteId, 10);
+
+ const [existingSession] = await db
+ .select()
+ .from(activeSessions)
+ .where(
+ and(
+ eq(activeSessions.userId, userId),
+ eq(activeSessions.siteId, siteIdNumber)
+ )
+ )
+ .limit(1);
+
+ return existingSession || null;
+ }
+
+ async updateSession(payload: {
+ userId: string;
+ site_id: string;
+ }): Promise<{ sessionId: string }> {
+ const existingSession = await this.getExistingSession(
+ payload.userId,
+ payload.site_id
+ );
+
+ if (existingSession) {
+ await db
+ .update(activeSessions)
+ .set({
+ lastActivity: new Date(),
+ })
+ .where(eq(activeSessions.sessionId, existingSession.sessionId));
+ return { sessionId: existingSession.sessionId };
+ }
+
+ // Insert new session with Drizzle - only include columns that exist in schema
+ const insertData = {
+ sessionId: crypto.randomUUID(),
+ siteId:
+ typeof payload.site_id === "string"
+ ? parseInt(payload.site_id, 10)
+ : payload.site_id,
+ userId: payload.userId,
+ startTime: new Date(),
+ lastActivity: new Date(),
+ };
+
+ await db.insert(activeSessions).values(insertData);
+ return { sessionId: insertData.sessionId };
+ }
+
+ async cleanupOldSessions(): Promise<number> {
+ // Delete sessions older than 30 minutes
+ const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000);
+
+ const deletedSessions = await db
+ .delete(activeSessions)
+ .where(lt(activeSessions.lastActivity, thirtyMinutesAgo))
+ .returning();
+
+ // console.log(`Cleaned up ${deletedSessions.length} sessions`);
+ return deletedSessions.length;
+ }
+
+ // Method to stop the cleanup cron job (useful for graceful shutdown)
+ stopCleanupCron() {
+ if (this.cleanupTask) {
+ this.cleanupTask.stop();
+ console.log("[SessionsService] Session cleanup cron stopped");
+ }
+ }
+}
+
+export const sessionsService = new SessionsService();
diff --git a/server/src/services/userId/userIdService.ts b/server/src/services/userId/userIdService.ts
new file mode 100644
index 000000000..6ca3a2131
--- /dev/null
+++ b/server/src/services/userId/userIdService.ts
@@ -0,0 +1,73 @@
+import crypto from "crypto";
+import { siteConfig } from "../../lib/siteConfig.js";
+
+class UserIdService {
+ private cachedSalt: string | null = null;
+ private cacheDate: string | null = null; // Store the date the salt was generated for (YYYY-MM-DD format)
+
+ /**
+ * Generates a deterministic daily salt based on a secret environment variable.
+ * The salt remains the same for the entire UTC day and changes automatically
+ * when the UTC date changes. Caches the salt in memory for efficiency.
+ *
+ * @throws {Error} If the BETTER_AUTH_SECRET environment variable is not set.
+ * @returns {string} The daily salt as a hex string.
+ */
+ private getDailySalt(): string {
+ const secretKey = process.env.BETTER_AUTH_SECRET;
+
+ if (!secretKey) {
+ console.error(
+ "FATAL: BETTER_AUTH_SECRET environment variable is not set. User ID generation will be insecure or fail."
+ );
+ throw new Error("BETTER_AUTH_SECRET environment variable is missing.");
+ }
+
+ // Use UTC date to ensure consistency across timezones and server restarts
+ const currentDate = new Date().toISOString().split("T")[0]; // Gets 'YYYY-MM-DD' in UTC
+
+ // Check if the cached salt is still valid for the current UTC date
+ if (this.cachedSalt && this.cacheDate === currentDate) {
+ return this.cachedSalt;
+ }
+
+ const input = secretKey + currentDate;
+ const newSalt = crypto.createHash("sha256").update(input).digest("hex");
+
+ this.cachedSalt = newSalt;
+ this.cacheDate = currentDate;
+ return newSalt;
+ }
+
+ /**
+ * Generate a user ID based on IP and user agent
+ * If the site has salting enabled, also includes a daily rotating salt
+ *
+ * @param ip User's IP address
+ * @param userAgent User's user agent string
+ * @param siteId The site ID to check for salting configuration
+ * @returns A sha256 hash to identify the user
+ */
+ generateUserId(
+ ip: string,
+ userAgent: string,
+ siteId?: string | number
+ ): string {
+ // Only apply salt if the site has salting enabled
+ if (siteId && siteConfig.shouldSaltUserIds(siteId)) {
+ const dailySalt = this.getDailySalt(); // Get the salt for the current day
+ return crypto
+ .createHash("sha256")
+ .update(ip + userAgent + dailySalt)
+ .digest("hex");
+ }
+
+ // Otherwise, just hash IP and user agent
+ return crypto
+ .createHash("sha256")
+ .update(ip + userAgent)
+ .digest("hex");
+ }
+}
+
+export const userIdService = new UserIdService();
\ No newline at end of file
diff --git a/server/src/tracker/getChannel.ts b/server/src/tracker/getChannel.ts
index 8a64b1edd..547a2f65a 100644
--- a/server/src/tracker/getChannel.ts
+++ b/server/src/tracker/getChannel.ts
@@ -12,7 +12,7 @@ import {
socialAppIds,
videoAppIds,
} from "./const.js";
-import { getUTMParams } from "./trackingUtils.js";
+import { getUTMParams } from "./utils.js";
// Categorize mobile app by its bundle ID/package name
function getMobileAppCategory(
diff --git a/server/src/tracker/pageviewQueue.ts b/server/src/tracker/pageviewQueue.ts
index f8d4fbafd..6eb977c57 100644
--- a/server/src/tracker/pageviewQueue.ts
+++ b/server/src/tracker/pageviewQueue.ts
@@ -3,7 +3,7 @@ import { clickhouse } from "../db/clickhouse/clickhouse.js";
import { getLocation } from "../db/geolocation/geolocation.js";
import { getDeviceType } from "../utils.js";
import { getChannel } from "./getChannel.js";
-import { clearSelfReferrer, getAllUrlParams } from "./trackingUtils.js";
+import { clearSelfReferrer, getAllUrlParams } from "./utils.js";
import { TrackingPayload } from "./types.js";
type TotalPayload = TrackingPayload & {
diff --git a/server/src/tracker/trackEvent.ts b/server/src/tracker/trackEvent.ts
index 9770aefa0..fd8a8a3f4 100644
--- a/server/src/tracker/trackEvent.ts
+++ b/server/src/tracker/trackEvent.ts
@@ -1,21 +1,13 @@
import { FastifyReply, FastifyRequest } from "fastify";
-import { z, ZodError } from "zod";
import { isbot } from "isbot";
-import {
- clearSelfReferrer,
- createBasePayload,
- getExistingSession,
- isSiteOverLimit,
- TotalTrackingPayload,
-} from "./trackingUtils.js";
-import { db } from "../db/postgres/postgres.js";
-import { activeSessions } from "../db/postgres/schema.js";
-import { eq } from "drizzle-orm";
-import { getDeviceType, normalizeOrigin } from "../utils.js";
-import { pageviewQueue } from "./pageviewQueue.js";
+import { z, ZodError } from "zod";
+import { apiKeyRateLimiter } from "../lib/rateLimiter.js";
import { siteConfig } from "../lib/siteConfig.js";
+import { sessionsService } from "../services/sessions/sessionsService.js";
+import { normalizeOrigin } from "../utils.js";
import { DISABLE_ORIGIN_CHECK } from "./const.js";
-import { apiKeyRateLimiter } from "../lib/rateLimiter.js";
+import { createBasePayload, isSiteOverLimit } from "./utils.js";
+import { pageviewQueue } from "./pageviewQueue.js";
// Define Zod schema for validation
export const trackingPayloadSchema = z.discriminatedUnion("type", [
@@ -163,61 +155,6 @@ export const trackingPayloadSchema = z.discriminatedUnion("type", [
.strict(),
]);
-// Update session for both pageviews and events
-async function updateSession(
- payload: TotalTrackingPayload,
- existingSession: any | null,
- isPageview: boolean = true
-): Promise<void> {
- if (existingSession) {
- // Update session with Drizzle
- const updateData: any = {
- lastActivity: new Date(payload.timestamp),
- };
-
- // Only increment pageviews count for actual pageviews
- if (isPageview) {
- updateData.pageviews = (existingSession.pageviews || 0) + 1;
- }
-
- await db
- .update(activeSessions)
- .set(updateData)
- .where(eq(activeSessions.userId, existingSession.userId));
- return;
- }
-
- // Insert new session with Drizzle
- const insertData = {
- sessionId: payload.sessionId,
- siteId:
- typeof payload.site_id === "string"
- ? parseInt(payload.site_id, 10)
- : payload.site_id,
- userId: payload.userId,
- hostname: payload.hostname || null,
- startTime: new Date(payload.timestamp || Date.now()),
- lastActivity: new Date(payload.timestamp || Date.now()),
- pageviews: isPageview ? 1 : 0,
- entryPage: payload.pathname || null,
- deviceType: getDeviceType(
- payload.screenWidth,
- payload.screenHeight,
- payload.ua
- ),
- screenWidth: payload.screenWidth || null,
- screenHeight: payload.screenHeight || null,
- browser: payload.ua.browser.name || null,
- operatingSystem: payload.ua.os.name || null,
- language: payload.language || null,
- referrer: payload.hostname
- ? clearSelfReferrer(payload.referrer || "", payload.hostname)
- : payload.referrer || null,
- };
-
- await db.insert(activeSessions).values(insertData);
-}
-
/**
* Validates API key for the site
* @param siteId The site ID from the tracking payload
@@ -432,24 +369,14 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {
validatedPayload.type,
validatedPayload // Add validated payload back
);
-
- // Get existing session
- const existingSession = await getExistingSession(
- payload.userId,
- payload.site_id
- );
-
- if (existingSession) {
- payload.sessionId = existingSession.sessionId;
- }
+ // Update session
+ const { sessionId } = await sessionsService.updateSession({
+ userId: payload.userId,
+ site_id: payload.site_id,
+ });
// Add to queue for processing
- pageviewQueue.add(payload);
- await updateSession(
- payload,
- existingSession,
- validatedPayload.type === "pageview"
- );
+ await pageviewQueue.add({ ...payload, sessionId });
return reply.status(200).send({
success: true,
diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/utils.ts
similarity index 60%
rename from server/src/tracker/trackingUtils.ts
rename to server/src/tracker/utils.ts
index bb05de052..0656d415b 100644
--- a/server/src/tracker/trackingUtils.ts
+++ b/server/src/tracker/utils.ts
@@ -1,14 +1,12 @@
import crypto from "crypto";
-import { and, eq } from "drizzle-orm";
import { FastifyRequest } from "fastify";
import UAParser, { UAParser as userAgentParser } from "ua-parser-js";
import { z } from "zod";
import { sitesOverLimit } from "../cron/monthly-usage-checker.js";
-import { db } from "../db/postgres/postgres.js";
-import { activeSessions } from "../db/postgres/schema.js";
-import { trackingPayloadSchema } from "./trackEvent.js";
import { siteConfig } from "../lib/siteConfig.js";
+import { trackingPayloadSchema } from "./trackEvent.js";
import { TrackingPayload } from "./types.js";
+import { userIdService } from "../services/userId/userIdService.js";
export type TotalTrackingPayload = TrackingPayload & {
type?: string;
@@ -16,7 +14,6 @@ export type TotalTrackingPayload = TrackingPayload & {
properties?: string;
userId: string;
timestamp: string;
- sessionId: string;
ua: UAParser.IResult;
referrer: string;
ipAddress: string;
@@ -93,24 +90,6 @@ export function isSiteOverLimit(siteId: number | string): boolean {
return sitesOverLimit.has(Number(siteId));
}
-// Get existing user session
-export async function getExistingSession(userId: string, siteId: string) {
- const siteIdNumber = parseInt(siteId, 10); // Ensure siteId is a number for the query
-
- const [existingSession] = await db
- .select()
- .from(activeSessions)
- .where(
- and(
- eq(activeSessions.userId, userId),
- eq(activeSessions.siteId, siteIdNumber)
- )
- )
- .limit(1);
-
- return existingSession || null; // Return the session or null if not found
-}
-
// Create base tracking payload from request
export function createBasePayload(
request: FastifyRequest,
@@ -127,7 +106,7 @@ export function createBasePayload(
// Use custom user ID if provided, otherwise generate one
const userId = validatedBody.user_id
? validatedBody.user_id.trim()
- : getUserId(ipAddress, userAgent, siteId);
+ : userIdService.generateUserId(ipAddress, userAgent, siteId);
return {
...validatedBody,
@@ -144,76 +123,9 @@ export function createBasePayload(
timestamp: new Date().toISOString(),
ua: userAgentParser(userAgent),
userId: userId,
- sessionId: crypto.randomUUID(), // Will be replaced if session exists
};
}
-let cachedSalt: string | null = null;
-let cacheDate: string | null = null; // Store the date the salt was generated for (YYYY-MM-DD format)
-
-/**
- * Generates a deterministic daily salt based on a secret environment variable.
- * The salt remains the same for the entire UTC day and changes automatically
- * when the UTC date changes. Caches the salt in memory for efficiency.
- *
- * @throws {Error} If the BETTER_AUTH_SECRET environment variable is not set.
- * @returns {string} The daily salt as a hex string.
- */
-function getDailySalt(): string {
- const secretKey = process.env.BETTER_AUTH_SECRET;
-
- if (!secretKey) {
- console.error(
- "FATAL: BETTER_AUTH_SECRET environment variable is not set. User ID generation will be insecure or fail."
- );
- throw new Error("BETTER_AUTH_SECRET environment variable is missing.");
- }
-
- // Use UTC date to ensure consistency across timezones and server restarts
- const currentDate = new Date().toISOString().split("T")[0]; // Gets 'YYYY-MM-DD' in UTC
-
- // Check if the cached salt is still valid for the current UTC date
- if (cachedSalt && cacheDate === currentDate) {
- return cachedSalt;
- }
-
- const input = secretKey + currentDate;
- const newSalt = crypto.createHash("sha256").update(input).digest("hex");
-
- cachedSalt = newSalt;
- cacheDate = currentDate;
- return newSalt;
-}
-
-/**
- * Generate a user ID based on IP and user agent
- * If the site has salting enabled, also includes a daily rotating salt
- *
- * @param ip User's IP address
- * @param userAgent User's user agent string
- * @param siteId The site ID to check for salting configuration
- * @returns A sha256 hash to identify the user
- */
-function getUserId(
- ip: string,
- userAgent: string,
- siteId?: string | number
-): string {
- // Only apply salt if the site has salting enabled
- if (siteId && siteConfig.shouldSaltUserIds(siteId)) {
- const dailySalt = getDailySalt(); // Get the salt for the current day
- return crypto
- .createHash("sha256")
- .update(ip + userAgent + dailySalt)
- .digest("hex");
- }
-
- // Otherwise, just hash IP and user agent
- return crypto
- .createHash("sha256")
- .update(ip + userAgent)
- .digest("hex");
-}
// Helper function to get IP address
const getIpAddress = (request: FastifyRequest): string => {
diff --git a/server/src/types/sessionReplay.ts b/server/src/types/sessionReplay.ts
new file mode 100644
index 000000000..b8037021e
--- /dev/null
+++ b/server/src/types/sessionReplay.ts
@@ -0,0 +1,82 @@
+export interface SessionReplayEvent {
+ siteId: number;
+ sessionId: string;
+ userId: string;
+ timestamp: Date;
+ eventType: string;
+ eventData: string;
+ sequenceNumber: number;
+ eventSizeBytes: number;
+ viewportWidth?: number;
+ viewportHeight?: number;
+ isComplete: boolean;
+}
+
+export interface SessionReplayMetadata {
+ siteId: number;
+ sessionId: string;
+ userId: string;
+ startTime: Date;
+ endTime?: Date;
+ durationMs?: number;
+ eventCount: number;
+ compressedSizeBytes: number;
+ pageUrl: string;
+ country: string;
+ region: string;
+ city: string;
+ lat: number;
+ lon: number;
+ browser: string;
+ browserVersion: string;
+ operatingSystem: string;
+ operatingSystemVersion: string;
+ language: string;
+ screenWidth: number;
+ screenHeight: number;
+ deviceType: string;
+ channel: string;
+ hostname: string;
+ referrer: string;
+ hasReplayData: boolean;
+ recordingStatus: 'recording' | 'completed' | 'failed';
+ createdAt: Date;
+}
+
+export interface RecordSessionReplayRequest {
+ userId: string;
+ events: Array<{
+ type: string | number;
+ data: any;
+ timestamp: number;
+ }>;
+ metadata?: {
+ pageUrl: string;
+ viewportWidth?: number;
+ viewportHeight?: number;
+ language?: string;
+ };
+}
+
+export interface SessionReplayListItem {
+ sessionId: string;
+ userId: string;
+ startTime: Date;
+ endTime?: Date;
+ durationMs?: number;
+ pageUrl: string;
+ eventCount: number;
+ recordingStatus: string;
+ country: string;
+ browser: string;
+ deviceType: string;
+}
+
+export interface GetSessionReplayEventsResponse {
+ events: Array<{
+ timestamp: number;
+ type: string;
+ data: any;
+ }>;
+ metadata: SessionReplayMetadata;
+}
\ No newline at end of file