🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add D3.js library and update Sidebar component (#94)
Bill Yangfeff66e72080
diff --git a/client/package-lock.json b/client/package-lock.jsonindex 2156ba4a6..1f1bc89f6 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -45,6 +45,7 @@"cmdk": "^1.0.0","countries-list": "^3.1.1","country-flag-icons": "^1.5.14",+ "d3": "^7.9.0","d3-geo": "^3.1.1","date-fns": "^4.1.0","deps": "^1.0.0",@@ -5468,6 +5469,46 @@"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz","integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="},+ "node_modules/d3": {+ "version": "7.9.0",+ "resolved": "https://registry.npmjs.org/d3/-/d3-7.9.0.tgz",+ "integrity": "sha512-e1U46jVP+w7Iut8Jt8ri1YsPOvFpg46k+K8TpCb0P+zjCkjkPnV7WzfDJzMHy1LnA+wj5pLT1wjO901gLXeEhA==",+ "dependencies": {+ "d3-array": "3",+ "d3-axis": "3",+ "d3-brush": "3",+ "d3-chord": "3",+ "d3-color": "3",+ "d3-contour": "4",+ "d3-delaunay": "6",+ "d3-dispatch": "3",+ "d3-drag": "3",+ "d3-dsv": "3",+ "d3-ease": "3",+ "d3-fetch": "3",+ "d3-force": "3",+ "d3-format": "3",+ "d3-geo": "3",+ "d3-hierarchy": "3",+ "d3-interpolate": "3",+ "d3-path": "3",+ "d3-polygon": "3",+ "d3-quadtree": "3",+ "d3-random": "3",+ "d3-scale": "4",+ "d3-scale-chromatic": "3",+ "d3-selection": "3",+ "d3-shape": "3",+ "d3-time": "3",+ "d3-time-format": "4",+ "d3-timer": "3",+ "d3-transition": "3",+ "d3-zoom": "3"+ },+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-array": {"version": "3.2.4","resolved": "https://registry.npmjs.org/d3-array/-/d3-array-3.2.4.tgz",@@ -5479,6 +5520,40 @@"node": ">=12"}},+ "node_modules/d3-axis": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/d3-axis/-/d3-axis-3.0.0.tgz",+ "integrity": "sha512-IH5tgjV4jE/GhHkRV0HiVYPDtvfjHQlQfJHs0usq7M30XcSBvOotpmH1IgkcXsO/5gEQZD43B//fc7SRT5S+xw==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-brush": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/d3-brush/-/d3-brush-3.0.0.tgz",+ "integrity": "sha512-ALnjWlVYkXsVIGlOsuWH1+3udkYFI48Ljihfnh8FZPF2QS9o+PzGLBslO0PjzVoHLZ2KCVgAM8NVkXPJB2aNnQ==",+ "dependencies": {+ "d3-dispatch": "1 - 3",+ "d3-drag": "2 - 3",+ "d3-interpolate": "1 - 3",+ "d3-selection": "3",+ "d3-transition": "3"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-chord": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-chord/-/d3-chord-3.0.1.tgz",+ "integrity": "sha512-VE5S6TNa+j8msksl7HwjxMHDM2yNK3XCkusIlpX5kwauBfXuyLAtNg9jCp/iHH61tgI4sb6R/EIMWCqEIdjT/g==",+ "dependencies": {+ "d3-path": "1 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-color": {"version": "3.1.0","resolved": "https://registry.npmjs.org/d3-color/-/d3-color-3.1.0.tgz",@@ -5487,6 +5562,17 @@"node": ">=12"}},+ "node_modules/d3-contour": {+ "version": "4.0.2",+ "resolved": "https://registry.npmjs.org/d3-contour/-/d3-contour-4.0.2.tgz",+ "integrity": "sha512-4EzFTRIikzs47RGmdxbeUvLWtGedDUNkTcmzoeyg4sP/dvCexO47AaQL7VKy/gul85TOxw+IBgA8US2xwbToNA==",+ "dependencies": {+ "d3-array": "^3.2.0"+ },+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-delaunay": {"version": "6.0.4","resolved": "https://registry.npmjs.org/d3-delaunay/-/d3-delaunay-6.0.4.tgz",@@ -5498,6 +5584,90 @@"node": ">=12"}},+ "node_modules/d3-dispatch": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-dispatch/-/d3-dispatch-3.0.1.tgz",+ "integrity": "sha512-rzUyPU/S7rwUflMyLc1ETDeBj0NRuHKKAcvukozwhshr6g6c5d8zh4c2gQjY2bZ0dXeGLWc1PF174P2tVvKhfg==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-drag": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/d3-drag/-/d3-drag-3.0.0.tgz",+ "integrity": "sha512-pWbUJLdETVA8lQNJecMxoXfH6x+mO2UQo8rSmZ+QqxcbyA3hfeprFgIT//HW2nlHChWeIIMwS2Fq+gEARkhTkg==",+ "dependencies": {+ "d3-dispatch": "1 - 3",+ "d3-selection": "3"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-dsv": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-dsv/-/d3-dsv-3.0.1.tgz",+ "integrity": "sha512-UG6OvdI5afDIFP9w4G0mNq50dSOsXHJaRE8arAS5o9ApWnIElp8GZw1Dun8vP8OyHOZ/QJUKUJwxiiCCnUwm+Q==",+ "dependencies": {+ "commander": "7",+ "iconv-lite": "0.6",+ "rw": "1"+ },+ "bin": {+ "csv2json": "bin/dsv2json.js",+ "csv2tsv": "bin/dsv2dsv.js",+ "dsv2dsv": "bin/dsv2dsv.js",+ "dsv2json": "bin/dsv2json.js",+ "json2csv": "bin/json2dsv.js",+ "json2dsv": "bin/json2dsv.js",+ "json2tsv": "bin/json2dsv.js",+ "tsv2csv": "bin/dsv2dsv.js",+ "tsv2json": "bin/dsv2json.js"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-dsv/node_modules/commander": {+ "version": "7.2.0",+ "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz",+ "integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==",+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/d3-ease": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-ease/-/d3-ease-3.0.1.tgz",+ "integrity": "sha512-wR/XK3D3XcLIZwpbvQwQ5fK+8Ykds1ip7A2Txe0yxncXSdq1L9skcG7blcedkOX+ZcgxGAmLX1FrRGbADwzi0w==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-fetch": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-fetch/-/d3-fetch-3.0.1.tgz",+ "integrity": "sha512-kpkQIM20n3oLVBKGg6oHrUchHM3xODkTzjMoj7aWQFq5QEM+R6E4WkzT5+tojDY7yjez8KgCBRoj4aEr99Fdqw==",+ "dependencies": {+ "d3-dsv": "1 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-force": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/d3-force/-/d3-force-3.0.0.tgz",+ "integrity": "sha512-zxV/SsA+U4yte8051P4ECydjD/S+qeYtnaIyAs9tgHCqfguma/aAQDjo85A9Z6EKhBirHRJHXIgJUlffT4wdLg==",+ "dependencies": {+ "d3-dispatch": "1 - 3",+ "d3-quadtree": "1 - 3",+ "d3-timer": "1 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-format": {"version": "1.4.5","resolved": "https://registry.npmjs.org/d3-format/-/d3-format-1.4.5.tgz",@@ -5528,6 +5698,14 @@"node": ">=12"}},+ "node_modules/d3-hierarchy": {+ "version": "3.1.2",+ "resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz",+ "integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==",+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-interpolate": {"version": "3.0.1","resolved": "https://registry.npmjs.org/d3-interpolate/-/d3-interpolate-3.0.1.tgz",@@ -5552,6 +5730,30 @@"node": ">=12"}},+ "node_modules/d3-polygon": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-polygon/-/d3-polygon-3.0.1.tgz",+ "integrity": "sha512-3vbA7vXYwfe1SYhED++fPUQlWSYTTGmFmQiany/gdbiWgU/iEyQzyymwL9SkJjFFuCS4902BSzewVGsHHmHtXg==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-quadtree": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-quadtree/-/d3-quadtree-3.0.1.tgz",+ "integrity": "sha512-04xDrxQTDTCFwP5H6hRhsRcb9xxv2RzkcsygFzmkSIOJy3PeRJP7sNk3VRIbKXcog561P9oU0/rVH6vDROAgUw==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-random": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-random/-/d3-random-3.0.1.tgz",+ "integrity": "sha512-FXMe9GfxTxqd5D6jFsQ+DJ8BJS4E/fT5mqqdjovykEB2oFbTMDVdg1MGFxfQW+FBOGoB++k8swBrgwSHT1cUXQ==",+ "engines": {+ "node": ">=12"+ }+ },"node_modules/d3-scale": {"version": "4.0.2","resolved": "https://registry.npmjs.org/d3-scale/-/d3-scale-4.0.2.tgz",@@ -5622,6 +5824,32 @@"d3-time": "1 - 2"}},+ "node_modules/d3-timer": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-timer/-/d3-timer-3.0.1.tgz",+ "integrity": "sha512-ndfJ/JxxMd3nw31uyKoY2naivF+r29V+Lc0svZxe1JvvIRmi8hUsrMvdOwgS1o6uBHmiz91geQ0ylPP0aj1VUA==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3-transition": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/d3-transition/-/d3-transition-3.0.1.tgz",+ "integrity": "sha512-ApKvfjsSR6tg06xrL434C0WydLr7JewBB3V+/39RMHsaXTOG0zmt/OAXeng5M5LBm0ojmxJrpomQVZ1aPvBL4w==",+ "dependencies": {+ "d3-color": "1 - 3",+ "d3-dispatch": "1 - 3",+ "d3-ease": "1 - 3",+ "d3-interpolate": "1 - 3",+ "d3-timer": "1 - 3"+ },+ "engines": {+ "node": ">=12"+ },+ "peerDependencies": {+ "d3-selection": "2 - 3"+ }+ },"node_modules/d3-tricontour": {"version": "1.0.2","resolved": "https://registry.npmjs.org/d3-tricontour/-/d3-tricontour-1.0.2.tgz",@@ -5634,6 +5862,51 @@"node": ">=12"}},+ "node_modules/d3-zoom": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/d3-zoom/-/d3-zoom-3.0.0.tgz",+ "integrity": "sha512-b8AmV3kfQaqWAuacbPuNbL6vahnOJflOhexLzMMNLga62+/nh0JzvJ0aO/5a5MVgUFGS7Hu1P9P03o3fJkDCyw==",+ "dependencies": {+ "d3-dispatch": "1 - 3",+ "d3-drag": "2 - 3",+ "d3-interpolate": "1 - 3",+ "d3-selection": "2 - 3",+ "d3-transition": "2 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3/node_modules/d3-format": {+ "version": "3.1.0",+ "resolved": "https://registry.npmjs.org/d3-format/-/d3-format-3.1.0.tgz",+ "integrity": "sha512-YyUI6AEuY/Wpt8KWLgZHsIU86atmikuoOmCfommt0LYHiQSPjvX2AcFc38PX0CBpr2RCyZhjex+NS/LPOv6YqA==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3/node_modules/d3-time": {+ "version": "3.1.0",+ "resolved": "https://registry.npmjs.org/d3-time/-/d3-time-3.1.0.tgz",+ "integrity": "sha512-VqKjzBLejbSMT4IgbmVgDjpkYrNWUYJnbCGo874u7MMKIWsILRX+OpX/gTk8MqjpT1A/c6HY2dCA77ZN0lkQ2Q==",+ "dependencies": {+ "d3-array": "2 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/d3/node_modules/d3-time-format": {+ "version": "4.1.0",+ "resolved": "https://registry.npmjs.org/d3-time-format/-/d3-time-format-4.1.0.tgz",+ "integrity": "sha512-dJxPBlzC7NugB2PDLwo9Q8JiTR3M3e4/XANkreKSUxF8vvXKqm1Yfq4Q5dl8budlunRVlUUaDUgFt7eA8D6NLg==",+ "dependencies": {+ "d3-time": "1 - 3"+ },+ "engines": {+ "node": ">=12"+ }+ },"node_modules/data-bind-mapper": {"version": "1.0.3","resolved": "https://registry.npmjs.org/data-bind-mapper/-/data-bind-mapper-1.0.3.tgz",@@ -6287,6 +6560,17 @@"node": ">=8.12.0"}},+ "node_modules/iconv-lite": {+ "version": "0.6.3",+ "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",+ "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",+ "dependencies": {+ "safer-buffer": ">= 2.1.2 < 3.0.0"+ },+ "engines": {+ "node": ">=0.10.0"+ }+ },"node_modules/ieee754": {"version": "1.2.1","resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",@@ -7892,6 +8176,16 @@"queue-microtask": "^1.2.2"}},+ "node_modules/rw": {+ "version": "1.3.3",+ "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz",+ "integrity": "sha512-PdhdWy89SiZogBLaw42zdeqtRJ//zFd2PgQavcICDUgJT5oW10QCRKbJ6bg4r0/UY2M6BWd5tkxuGFRvCkgfHQ=="+ },+ "node_modules/safer-buffer": {+ "version": "2.1.2",+ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",+ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="+ },"node_modules/scheduler": {"version": "0.25.0","resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.25.0.tgz",diff --git a/client/package.json b/client/package.jsonindex b75e69279..3d1bd5a70 100644--- a/client/package.json+++ b/client/package.json@@ -46,6 +46,7 @@"cmdk": "^1.0.0","countries-list": "^3.1.1","country-flag-icons": "^1.5.14",+ "d3": "^7.9.0","d3-geo": "^3.1.1","date-fns": "^4.1.0","deps": "^1.0.0",diff --git a/client/src/api/analytics/useJourneys.ts b/client/src/api/analytics/useJourneys.tsnew file mode 100644index 000000000..9af911645--- /dev/null+++ b/client/src/api/analytics/useJourneys.ts@@ -0,0 +1,49 @@+import { useQuery } from "@tanstack/react-query";+import { authedFetch, getStartAndEndDate } from "../utils";+import { BACKEND_URL } from "../../lib/const";+import { Time } from "../../components/DateSelector/types";++export interface JourneyParams {+ siteId: string;+ steps?: number;+ timezone?: string;+ time: Time;+}++export interface Journey {+ path: string[];+ count: number;+ percentage: number;+}++export interface JourneysResponse {+ journeys: Journey[];+}++export const useJourneys = ({+ siteId,+ steps = 3,+ timezone = "UTC",+ time,+}: JourneyParams) => {+ const { startDate, endDate } = getStartAndEndDate(time);++ return useQuery<JourneysResponse>({+ queryKey: ["journeys", siteId, steps, startDate, endDate, timezone],+ queryFn: async () => {+ let url = `${BACKEND_URL}/journeys/${siteId}`;+ const params = new URLSearchParams();++ if (steps) params.append("steps", steps.toString());+ if (startDate) params.append("startDate", startDate);+ if (endDate) params.append("endDate", endDate);+ if (timezone) params.append("timezone", timezone);++ const queryString = params.toString();+ if (queryString) url += `?${queryString}`;++ const response = await authedFetch(url);+ return response.json();+ },+ });+};diff --git a/client/src/app/[site]/components/Sidebar/Sidebar.tsx b/client/src/app/[site]/components/Sidebar/Sidebar.tsxindex 421757af6..3b2c94680 100644--- a/client/src/app/[site]/components/Sidebar/Sidebar.tsx+++ b/client/src/app/[site]/components/Sidebar/Sidebar.tsx@@ -9,6 +9,7 @@ import {Settings,User,Map,+ Split,} from "lucide-react";import Link from "next/link";import { usePathname } from "next/navigation";@@ -88,11 +89,17 @@ export function Sidebar() {icon={<Funnel weight="bold" />}/><SidebarLink+ label="Journeys"+ active={isActiveTab("journeys")}+ href={getTabPath("journeys")}+ icon={<Split className="w-4 h-4" />}+ />+ {/* <SidebarLinklabel="Reports"active={isActiveTab("reports")}href={getTabPath("reports")}icon={<ChartBarDecreasing className="w-4 h-4" />}- />+ /> */}{session.data && (<SiteSettingssiteId={site?.siteId ?? 0}diff --git a/client/src/app/[site]/journeys/page.tsx b/client/src/app/[site]/journeys/page.tsxnew file mode 100644index 000000000..44621d423--- /dev/null+++ b/client/src/app/[site]/journeys/page.tsx@@ -0,0 +1,462 @@+"use client";++import { useParams } from "next/navigation";+import { useJourneys } from "@/api/analytics/useJourneys";+import { useMemo, useRef, useEffect, useState } from "react";+import * as d3 from "d3";+import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";+import { Skeleton } from "@/components/ui/skeleton";+import { AlertCircle } from "lucide-react";+import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import { DateRangeMode, Time } from "../../../components/DateSelector/types";+import { DateTime } from "luxon";+import { DateSelector } from "../../../components/DateSelector/DateSelector";++const MAX_LINK_HEIGHT = 100;++export default function JourneysPage() {+ const params = useParams<{ site: string }>();+ const [steps, setSteps] = useState<number>(3);+ const [maxJourneys, setMaxJourneys] = useState<number>(25);++ const [time, setTime] = useState<Time>({+ mode: "range",+ startDate: DateTime.now().minus({ days: 7 }).toISODate(),+ endDate: DateTime.now().toISODate(),+ wellKnown: "Last 7 days",+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,+ } as DateRangeMode);++ const { data, isLoading, error } = useJourneys({+ siteId: params.site,+ steps,+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,+ time,+ });++ const svgRef = useRef<SVGSVGElement>(null);++ useEffect(() => {+ if (!data?.journeys || !svgRef.current) return;++ const svg = d3.select(svgRef.current);+ svg.selectAll("*").remove();++ const width = 1400;+ const height = 1000;+ const margin = { top: 30, right: 10, bottom: 30, left: 10 };+ const innerWidth = width - margin.left - margin.right;+ const innerHeight = height - margin.top - margin.bottom;++ // Create the main group element+ const g = svg+ .attr("width", width)+ .attr("height", height)+ .append("g")+ .attr("transform", `translate(${margin.left},${margin.top})`);++ // Create a Sankey diagram+ // First, build nodes and links from journey data+ const nodes: any[] = [];+ const links: any[] = [];++ data?.journeys?.slice(0, maxJourneys).forEach((journey) => {+ for (let i = 0; i < journey.path.length; i++) {+ const stepName = journey.path[i];+ const stepKey = `${i}_${stepName}`;++ if (!nodes.find((n) => n.id === stepKey)) {+ nodes.push({+ id: stepKey,+ name: stepName,+ step: i,+ incomingLinks: [],+ outgoingLinks: [],+ });+ }++ if (i < journey.path.length - 1) {+ const sourceKey = stepKey;+ const targetKey = `${i + 1}_${journey.path[i + 1]}`;++ const existingLink = links.find(+ (l) => l.source === sourceKey && l.target === targetKey+ );++ if (existingLink) {+ existingLink.value += journey.count;+ } else {+ links.push({+ source: sourceKey,+ target: targetKey,+ value: journey.count,+ });+ }+ }+ }+ });++ // Track incoming and outgoing links for each node+ links.forEach((link) => {+ const sourceNode = nodes.find((n) => n.id === link.source);+ const targetNode = nodes.find((n) => n.id === link.target);++ if (sourceNode) sourceNode.outgoingLinks.push(link);+ if (targetNode) targetNode.incomingLinks.push(link);+ });++ // Create scales for each step+ const stepWidth = innerWidth / steps;++ // Group nodes by step+ const nodesByStep = d3.group(nodes, (d) => d.step);++ // Position nodes vertically within each step+ nodesByStep.forEach((stepNodes, step) => {+ const stepX = step * stepWidth;+ const stepHeight = innerHeight / stepNodes.length;++ stepNodes.forEach((node, i) => {+ node.x = stepX;+ node.y = i * stepHeight + stepHeight / 2;+ });+ });++ // Find the maximum link value for scaling+ const maxLinkValue = d3.max(links, (link) => link.value) || 1;+ const linkWidthScale = d3+ .scaleLinear()+ .domain([0, maxLinkValue])+ .range([1, MAX_LINK_HEIGHT]);++ // Calculate node heights based on connected links+ nodes.forEach((node) => {+ // Sum the values of incoming and outgoing links+ const incomingValue = node.incomingLinks.reduce(+ (sum: number, link: any) => sum + link.value,+ 0+ );+ const outgoingValue = node.outgoingLinks.reduce(+ (sum: number, link: any) => sum + link.value,+ 0+ );++ // Use the larger value to determine height+ const maxValue = Math.max(incomingValue, outgoingValue);+ node.height = linkWidthScale(maxValue);++ // Minimum height for visibility+ node.height = Math.max(node.height, 3);++ // Store count for this node (use incoming for all except first step)+ node.count = node.step === 0 ? outgoingValue : incomingValue;++ // Store percentage - find the corresponding journey data+ const matchingJourney = data?.journeys?.find((journey) => {+ // Compare path at this step with node name+ return journey.path[node.step] === node.name;+ });++ // Use the percentage from journey data if available+ node.percentage = matchingJourney ? matchingJourney.percentage : 0;+ });++ // Calculate link positions along each node+ nodes.forEach((node) => {+ // Sort links by value (descending) for consistent ordering+ node.incomingLinks.sort((a: any, b: any) => b.value - a.value);+ node.outgoingLinks.sort((a: any, b: any) => b.value - a.value);++ // Calculate positions for outgoing links+ let currentOutY = 0;+ const totalOutgoing = node.outgoingLinks.reduce(+ (sum: number, link: any) => sum + link.value,+ 0+ );++ node.outgoingLinks.forEach((link: any) => {+ const linkHeight = linkWidthScale(link.value);+ // Position at the middle of the allocated segment+ link.sourceY = currentOutY + linkHeight / 2;+ // Update for next link+ currentOutY += linkHeight;+ });++ // Normalize positions to fit within node height+ if (totalOutgoing > 0 && node.outgoingLinks.length > 0) {+ node.outgoingLinks.forEach((link: any) => {+ link.sourceY =+ (link.sourceY / currentOutY) * node.height - node.height / 2;+ link.sourceY += node.y; // Adjust to node position+ });+ }++ // Calculate positions for incoming links+ let currentInY = 0;+ const totalIncoming = node.incomingLinks.reduce(+ (sum: number, link: any) => sum + link.value,+ 0+ );++ node.incomingLinks.forEach((link: any) => {+ const linkHeight = linkWidthScale(link.value);+ // Position at the middle of the allocated segment+ link.targetY = currentInY + linkHeight / 2;+ // Update for next link+ currentInY += linkHeight;+ });++ // Normalize positions to fit within node height+ if (totalIncoming > 0 && node.incomingLinks.length > 0) {+ node.incomingLinks.forEach((link: any) => {+ link.targetY =+ (link.targetY / currentInY) * node.height - node.height / 2;+ link.targetY += node.y; // Adjust to node position+ });+ }+ });++ // Create links as bezier curves+ g.selectAll(".link")+ .data(links)+ .join("path")+ .attr("class", "link")+ .attr("d", (d) => {+ const source = nodes.find((n) => n.id === d.source);+ const target = nodes.find((n) => n.id === d.target);++ if (!source || !target) return "";++ // Get the specific Y positions for this link+ const sourceY = d.sourceY || source.y;+ const targetY = d.targetY || target.y;++ // Make links connect exactly to the bars (no gap)+ const sourceX = source.x + 10; // End of source bar (x + width)+ const targetX = target.x; // Start of target bar++ return `M ${sourceX},${sourceY}+ C ${sourceX + stepWidth / 3},${sourceY}+ ${targetX - stepWidth / 3},${targetY}+ ${targetX},${targetY}`;+ })+ .attr("fill", "none")+ .attr("stroke", "hsl(var(--accent-500))")+ .attr("stroke-width", (d) => linkWidthScale(d.value))+ .attr("opacity", 0.6)+ .attr("data-id", (d, i) => `link-${i}`) // Add unique identifier for each link+ // Add hover effects+ .on("mouseover", function (event, d) {+ const hoveredLink = d3.select(this);+ const hoveredLinkId = hoveredLink.attr("data-id");++ // Find source and target nodes for this link+ const source = nodes.find((n) => n.id === d.source);+ const target = nodes.find((n) => n.id === d.target);++ // Make all other links more transparent+ d3.selectAll(".link")+ .transition()+ .duration(200)+ .attr("opacity", function () {+ return d3.select(this).attr("data-id") === hoveredLinkId+ ? 0.9+ : 0.1;+ });++ // Make all node bars more transparent except the connected ones+ d3.selectAll(".node-bar")+ .transition()+ .duration(200)+ .attr("opacity", function (nodeData: any) {+ return nodeData.id === source?.id || nodeData.id === target?.id+ ? 1+ : 0.2;+ });++ // Make all node cards more transparent except the connected ones+ d3.selectAll(".node-card")+ .transition()+ .duration(200)+ .attr("opacity", function (nodeData: any) {+ return nodeData.id === source?.id || nodeData.id === target?.id+ ? 0.9+ : 0.2;+ });++ // Highlight connected node text+ d3.selectAll(".node-text")+ .transition()+ .duration(200)+ .attr("opacity", function (nodeData: any) {+ return nodeData.id === source?.id || nodeData.id === target?.id+ ? 1+ : 0.3;+ });+ })+ .on("mouseout", function () {+ // Reset all opacities+ d3.selectAll(".link").transition().duration(200).attr("opacity", 0.6);++ d3.selectAll(".node-bar").transition().duration(200).attr("opacity", 1);++ d3.selectAll(".node-card")+ .transition()+ .duration(200)+ .attr("opacity", 0.8);++ d3.selectAll(".node-text")+ .transition()+ .duration(200)+ .attr("opacity", 1);+ })+ // Add tooltips showing the exact count+ .append("title")+ .text((d) => `Count: ${d.value}`);++ const nodeGroups = g+ .selectAll(".node")+ .data(nodes)+ .join("g")+ .attr("class", "node")+ .attr("transform", (d) => `translate(${d.x},${d.y - d.height / 2})`);++ nodeGroups+ .append("rect")+ .attr("class", "node-bar")+ .attr("width", 10)+ .attr("height", (d) => d.height)+ .attr("fill", "hsl(var(--neutral-500))")+ .attr("rx", 2) // Rounded corners+ .attr("ry", 2);++ // Add a card background for text+ const textBackgrounds = nodeGroups+ .append("rect")+ .attr("class", "node-card")+ .attr("x", 18)+ .attr("y", (d) => d.height / 2 - 17) // Position above the vertical center, taller card+ .attr("width", (d) => {+ // Find the width needed for both lines+ const pathText = d.name;+ const statsText = `${d.count} (${d.percentage.toFixed(1)}%)`;+ // Use whichever is longer+ const maxLength = Math.max(pathText.length, statsText.length);+ const textWidth = maxLength * 6.5;+ return textWidth + 10; // Add padding+ })+ .attr("height", 35) // Taller for two lines of text+ .attr("fill", "hsl(var(--neutral-800))")+ .attr("stroke", "hsl(var(--neutral-700))")+ .attr("stroke-width", 1)+ .attr("rx", 2) // Rounded corners+ .attr("ry", 2)+ .attr("opacity", 0.8);++ // Path text (first line)+ nodeGroups+ .append("text")+ .attr("class", "node-text")+ .attr("x", 23) // Add left padding inside card+ .attr("y", (d) => d.height / 2 - 2) // Position for first line+ .text((d) => d.name)+ .attr("font-size", "12px")+ .attr("fill", "white")+ .attr("text-anchor", "start");++ // Count and percentage text (second line)+ nodeGroups+ .append("text")+ .attr("class", "node-text")+ .attr("x", 23) // Same left padding+ .attr("y", (d) => d.height / 2 + 12) // Position for second line+ .text((d) => `${d.count.toLocaleString()}`)+ // .text((d) => `${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`)+ .attr("font-size", "11px") // Slightly smaller font+ .attr("fill", "hsl(var(--neutral-300))")+ .attr("text-anchor", "start");+ }, [data, steps, maxJourneys]);++ return (+ <div className="container mx-auto p-4">+ <Card className="w-full">+ <CardHeader className="flex flex-row items-center justify-between">+ <CardTitle>User Journeys</CardTitle>+ <div className="flex justify-end items-center gap-2 mb-2">+ <DateSelector time={time} setTime={setTime} />+ <Select+ value={steps.toString()}+ onValueChange={(value) => setSteps(Number(value))}+ >+ <SelectTrigger className="w-[120px]">+ <SelectValue placeholder="Number of steps" />+ </SelectTrigger>+ <SelectContent>+ {[2, 3, 4, 5, 6, 7, 8, 9, 10].map((step) => (+ <SelectItem key={step} value={step.toString()}>+ {step} steps+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ <Select+ value={maxJourneys.toString()}+ onValueChange={(value) => setMaxJourneys(Number(value))}+ >+ <SelectTrigger className="w-[140px]">+ <SelectValue placeholder="Max journeys" />+ </SelectTrigger>+ <SelectContent>+ {[10, 25, 50, 100].map((count) => (+ <SelectItem key={count} value={count.toString()}>+ {count} journeys+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div>+ </CardHeader>+ <CardContent>+ {isLoading && (+ <div className="flex flex-col space-y-4">+ <Skeleton className="h-[1000px] w-full rounded-md" />+ </div>+ )}++ {error && (+ <Alert variant="destructive">+ <AlertCircle className="h-4 w-4" />+ <AlertTitle>Error</AlertTitle>+ <AlertDescription>+ Failed to load journey data. Please try again.+ </AlertDescription>+ </Alert>+ )}++ {data?.journeys?.length === 0 && !isLoading && !error && (+ <Alert>+ <AlertCircle className="h-4 w-4" />+ <AlertTitle>No Data</AlertTitle>+ <AlertDescription>+ No journey data found for the selected criteria.+ </AlertDescription>+ </Alert>+ )}++ {data?.journeys?.length && data?.journeys?.length > 0 && (+ <div className="overflow-x-auto">+ <svg ref={svgRef} className="w-full h-[1000px]" />+ </div>+ )}+ </CardContent>+ </Card>+ </div>+ );+}diff --git a/server/src/api/analytics/getJourneys.ts b/server/src/api/analytics/getJourneys.tsnew file mode 100644index 000000000..27abf06bf--- /dev/null+++ b/server/src/api/analytics/getJourneys.ts@@ -0,0 +1,105 @@+import { FastifyRequest, FastifyReply } from "fastify";+import clickhouse from "../../db/clickhouse/clickhouse.js";+import { DateTime } from "luxon";+import { getTimeStatement } from "./utils.js";++export const getJourneys = async (+ request: FastifyRequest<{+ Params: { site: string };+ Querystring: {+ steps?: string;+ startDate?: string;+ endDate?: string;+ timezone?: string;+ };+ }>,+ reply: FastifyReply+) => {+ try {+ const { site } = request.params;+ const { steps = "3", startDate, endDate, timezone = "UTC" } = request.query;++ const maxSteps = parseInt(steps, 10);++ if (isNaN(maxSteps) || maxSteps < 2 || maxSteps > 10) {+ return reply.status(400).send({+ error: "Steps parameter must be a number between 2 and 10",+ });+ }++ // Time conditions using getTimeStatement+ const timeStatement = getTimeStatement({+ date:+ startDate || endDate+ ? {+ startDate:+ startDate || DateTime.now().minus({ days: 30 }).toISODate(),+ endDate: endDate || DateTime.now().toISODate(),+ timezone,+ table: "events",+ }+ : undefined,+ });++ // Query to find sequences of events (journeys) for each user+ const result = await clickhouse.query({+ query: `+ WITH user_paths AS (+ SELECT+ session_id,+ arrayCompact(groupArray(pathname)) AS path_sequence+ FROM (+ SELECT+ session_id,+ pathname,+ timestamp+ FROM events+ WHERE+ site_id = ${site}+ ${timeStatement || ""}+ AND type = 'pageview'+ ORDER BY session_id, timestamp+ )+ GROUP BY session_id+ HAVING length(path_sequence) >= 2+ ),++ journey_segments AS (+ SELECT+ arraySlice(path_sequence, 1, ${maxSteps}) AS journey,+ count() AS sessions_count+ FROM user_paths+ GROUP BY journey+ ORDER BY sessions_count DESC+ LIMIT 100+ )++ SELECT+ journey,+ sessions_count,+ sessions_count * 100 / (+ SELECT count(DISTINCT session_id)+ FROM events+ WHERE site_id = ${site} ${timeStatement || ""}+ ) AS percentage+ FROM journey_segments+ `,+ // query_params: {+ // site: parseInt(site, 10),+ // },+ });++ const data = await result.json();++ return reply.send({+ journeys: data.data.map((item: any) => ({+ path: item.journey,+ count: Number(item.sessions_count),+ percentage: Number(item.percentage),+ })),+ });+ } catch (error) {+ console.error("Error getting journeys:", error);+ return reply.status(500).send({ error: "Failed to get journeys" });+ }+};diff --git a/server/src/index.ts b/server/src/index.tsindex ca14b95a3..c5958dcc5 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -10,6 +10,7 @@ import { deleteReport } from "./api/analytics/deleteReport.js";import { getEvents } from "./api/analytics/getEvents.js";import { getFunnel } from "./api/analytics/getFunnel.js";import { getFunnels } from "./api/analytics/getFunnels.js";+import { getJourneys } from "./api/analytics/getJourneys.js";import { getLiveSessionLocations } from "./api/analytics/getLiveSessionLocations.js";import { getLiveUsercount } from "./api/analytics/getLiveUsercount.js";import { getOverview } from "./api/analytics/getOverview.js";@@ -132,6 +133,7 @@ const ANALYTICS_ROUTES = ["/live-session-locations/","/funnels/","/funnel/",+ "/journeys/","/get-site",];@@ -190,13 +192,14 @@ server.get("/site-has-data/:site", getSiteHasData);server.get("/site-is-public/:site", getSiteIsPublic);server.get("/sessions/:site", getSessions);server.get("/session/:sessionId/:site", getSession);+server.get("/recent-events/:site", getEvents);server.get("/users/:site", getUsers);server.get("/user/:userId/sessions/:site", getUserSessions);server.get("/user/session-count/:site", getUserSessionCount);server.get("/user/info/:userId/:site", getUserInfo);server.get("/live-session-locations/:site", getLiveSessionLocations);server.get("/funnels/:site", getFunnels);-server.get("/recent-events/:site", getEvents);+server.get("/journeys/:site", getJourneys);server.post("/funnel/:site", getFunnel);server.post("/funnel/create/:site", createFunnel);server.delete("/report/:reportId", deleteReport);