🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance journey data fetching and visualization in JourneysPage component
Bill Yang4322ab38dcc6
diff --git a/client/src/api/analytics/useJourneys.ts b/client/src/api/analytics/useJourneys.tsindex d3697a692..cce56a99c 100644--- a/client/src/api/analytics/useJourneys.ts+++ b/client/src/api/analytics/useJourneys.ts@@ -1,7 +1,8 @@import { useQuery } from "@tanstack/react-query";-import { authedFetch, getStartAndEndDate } from "../utils";import { Time } from "../../components/DateSelector/types";import { timeZone } from "../../lib/dateTimeUtils";+import { getFilteredFilters, JOURNEY_PAGE_FILTERS } from "../../lib/store";+import { authedFetch, getStartAndEndDate } from "../utils";export interface JourneyParams {siteId?: number;@@ -24,8 +25,10 @@ export interface JourneysResponse {export const useJourneys = ({ siteId, steps = 3, time, limit = 100 }: JourneyParams) => {const { startDate, endDate } = getStartAndEndDate(time);+ const filteredFilters = getFilteredFilters(JOURNEY_PAGE_FILTERS);+return useQuery<JourneysResponse>({- queryKey: ["journeys", siteId, steps, startDate, endDate, timeZone, limit],+ queryKey: ["journeys", siteId, steps, startDate, endDate, timeZone, limit, filteredFilters],queryFn: async () => {const params: Record<string, any> = {};@@ -34,9 +37,11 @@ export const useJourneys = ({ siteId, steps = 3, time, limit = 100 }: JourneyParif (endDate) params.endDate = endDate;if (timeZone) params.timeZone = timeZone;if (limit) params.limit = limit;+ if (filteredFilters) params.filters = filteredFilters;return authedFetch<JourneysResponse>(`/journeys/${siteId}`, params);},enabled: !!siteId,+ placeholderData: (previousData) => previousData,});};diff --git a/client/src/api/analytics/users.ts b/client/src/api/analytics/users.tsindex 2300beb59..5cff18a4a 100644--- a/client/src/api/analytics/users.ts+++ b/client/src/api/analytics/users.ts@@ -1,10 +1,9 @@import { Filter } from "@rybbit/shared";import { useQuery } from "@tanstack/react-query";import { timeZone } from "../../lib/dateTimeUtils";-import { useStore, USER_PAGE_FILTERS, getFilteredFilters } from "../../lib/store";-import { authedFetch, getStartAndEndDate } from "../utils";+import { USER_PAGE_FILTERS, getFilteredFilters, useStore } from "../../lib/store";import { APIResponse } from "../types";-import { getQueryParams } from "../utils";+import { authedFetch, getQueryParams } from "../utils";export type UsersResponse = {user_id: string;diff --git a/client/src/app/[site]/journeys/components/SankeyDiagram.tsx b/client/src/app/[site]/journeys/components/SankeyDiagram.tsxnew file mode 100644index 000000000..30feb93c5--- /dev/null+++ b/client/src/app/[site]/journeys/components/SankeyDiagram.tsx@@ -0,0 +1,425 @@+"use client";++import * as d3 from "d3";+import { useEffect, useRef } from "react";++const MAX_LINK_HEIGHT = 100;++interface Journey {+ path: string[];+ count: number;+ percentage: number;+}++interface SankeyDiagramProps {+ journeys: Journey[];+ steps: number;+ maxJourneys: number;+ domain: string;+}++export function SankeyDiagram({ journeys, steps, maxJourneys, domain }: SankeyDiagramProps) {+ const svgRef = useRef<SVGSVGElement>(null);++ useEffect(() => {+ if (!journeys || !svgRef.current || !domain) return;++ const svg = d3.select(svgRef.current);+ svg.selectAll("*").remove();++ const containerWidth = svgRef.current.parentElement?.clientWidth || 1000;++ // Build nodes and links+ const nodes: any[] = [];+ const links: any[] = [];++ 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,+ });+ }+ }+ }+ });++ // Calculate dimensions+ const nodesByStep = d3.group(nodes, d => d.step);+ const maxNodesInAnyStep = Math.max(...Array.from(nodesByStep.values()).map(stepNodes => stepNodes.length));++ const nodeWidth = 30;+ const stepSpacing = 300;+ const stepWidth = nodeWidth + stepSpacing;+ const width = stepWidth * steps + nodeWidth;+ const minHeight = 500;+ const baseNodeHeight = 60;+ const nodeSpacing = 20;+ const height = Math.max(minHeight, (baseNodeHeight + nodeSpacing) * maxNodesInAnyStep + 100);++ const margin = { top: 30, right: 0, bottom: 30, left: 0 };+ const innerHeight = height - margin.top - margin.bottom;++ const g = svg+ .attr("width", width)+ .attr("height", height)+ .append("g")+ .attr("transform", `translate(${margin.left},${margin.top})`);++ // 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);+ });++ // Position nodes+ 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;+ });+ });++ // Calculate node heights and metadata+ const maxLinkValue = d3.max(links, link => link.value) || 1;+ const linkWidthScale = d3.scaleLinear().domain([0, maxLinkValue]).range([1, MAX_LINK_HEIGHT]);++ nodes.forEach(node => {+ 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);+ const maxValue = Math.max(incomingValue, outgoingValue);++ node.height = linkWidthScale(maxValue);+ node.count = node.step === 0 ? outgoingValue : incomingValue;++ const matchingJourney = journeys.find(journey => journey.path[node.step] === node.name);+ node.percentage = matchingJourney ? matchingJourney.percentage : 0;+ });++ // Calculate link positions+ nodes.forEach(node => {+ node.incomingLinks.sort((a: any, b: any) => b.value - a.value);+ node.outgoingLinks.sort((a: any, b: any) => b.value - a.value);++ let currentOutY = 0;+ node.outgoingLinks.forEach((link: any) => {+ const linkHeight = linkWidthScale(link.value);+ link.sourceY = currentOutY + linkHeight / 2;+ currentOutY += linkHeight;+ });++ if (currentOutY > 0 && node.outgoingLinks.length > 0) {+ node.outgoingLinks.forEach((link: any) => {+ link.sourceY = (link.sourceY / currentOutY) * node.height - node.height / 2;+ link.sourceY += node.y;+ });+ }++ let currentInY = 0;+ node.incomingLinks.forEach((link: any) => {+ const linkHeight = linkWidthScale(link.value);+ link.targetY = currentInY + linkHeight / 2;+ currentInY += linkHeight;+ });++ if (currentInY > 0 && node.incomingLinks.length > 0) {+ node.incomingLinks.forEach((link: any) => {+ link.targetY = (link.targetY / currentInY) * node.height - node.height / 2;+ link.targetY += node.y;+ });+ }+ });++ // Helper function to find all connected paths (all reachable nodes/edges)+ const findAllConnectedPaths = (startLink: any, direction: "forward" | "backward"): any[] => {+ const connectedLinks: any[] = [];+ const queue: any[] = [startLink];+ const visited = new Set<string>();++ while (queue.length > 0) {+ const currentLink = queue.shift();+ const linkId = `${currentLink.source}|||${currentLink.target}`;++ if (visited.has(linkId)) continue;+ visited.add(linkId);+ connectedLinks.push(currentLink);++ if (direction === "forward") {+ const targetNode = nodes.find(n => n.id === currentLink.target);+ // Follow all outgoing links+ if (targetNode && targetNode.outgoingLinks.length > 0) {+ targetNode.outgoingLinks.forEach((link: any) => queue.push(link));+ }+ } else {+ const sourceNode = nodes.find(n => n.id === currentLink.source);+ // Follow all incoming links+ if (sourceNode && sourceNode.incomingLinks.length > 0) {+ sourceNode.incomingLinks.forEach((link: any) => queue.push(link));+ }+ }+ }++ return connectedLinks;+ };++ // Draw links+ const linkElements = 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 "";++ const sourceY = d.sourceY || source.y;+ const targetY = d.targetY || target.y;+ const sourceX = source.x + nodeWidth;+ const targetX = target.x;++ const controlPoint1X = sourceX + stepSpacing / 3;+ const controlPoint2X = targetX - stepSpacing / 3;++ return `M ${sourceX},${sourceY}+ C ${controlPoint1X},${sourceY}+ ${controlPoint2X},${targetY}+ ${targetX},${targetY}`;+ })+ .attr("fill", "none")+ .attr("stroke", "hsl(var(--neutral-500))")+ .attr("stroke-width", d => linkWidthScale(d.value))+ .attr("opacity", 0.2)+ .attr("data-source", d => d.source)+ .attr("data-target", d => d.target)+ .style("cursor", "pointer")+ .style("pointer-events", "visibleStroke")+ .on("mouseenter", function (event, d) {+ const forwardPaths = findAllConnectedPaths(d, "forward");+ const backwardPaths = findAllConnectedPaths(d, "backward");+ const allConnectedLinks = [d, ...forwardPaths, ...backwardPaths];++ const connectedLinkIds = new Set<string>();+ const connectedNodeIds = new Set<string>();++ allConnectedLinks.forEach(link => {+ const linkId = `${link.source}|||${link.target}`;+ connectedLinkIds.add(linkId);+ connectedNodeIds.add(link.source);+ connectedNodeIds.add(link.target);+ });++ d3.selectAll(".link")+ .attr("opacity", function () {+ const linkSource = d3.select(this).attr("data-source");+ const linkTarget = d3.select(this).attr("data-target");+ const thisLinkId = `${linkSource}|||${linkTarget}`;+ return connectedLinkIds.has(thisLinkId) ? 0.5 : 0.1;+ })+ .attr("stroke", function () {+ const linkSource = d3.select(this).attr("data-source");+ const linkTarget = d3.select(this).attr("data-target");+ const thisLinkId = `${linkSource}|||${linkTarget}`;+ return connectedLinkIds.has(thisLinkId) ? "hsl(var(--emerald-600))" : "hsl(var(--neutral-500))";+ });++ d3.selectAll(".node-rect").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 1 : 0.2;+ });++ d3.selectAll(".node-bubble").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 0.9 : 0.2;+ });++ d3.selectAll(".node-text").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 1 : 0.3;+ });+ })+ .on("mouseleave", function () {+ d3.selectAll(".link").attr("opacity", 0.2).attr("stroke", "hsl(var(--neutral-500))");+ d3.selectAll(".node-rect").attr("opacity", 1);+ d3.selectAll(".node-bubble").attr("opacity", 0.8);+ d3.selectAll(".node-text").attr("opacity", 1);+ })+ .append("title")+ .text(d => `Count: ${d.value}`);++ // Draw nodes+ const nodeGroups = g+ .selectAll(".node")+ .data(nodes)+ .join("g")+ .attr("class", "node")+ .attr("transform", d => `translate(${d.x},${d.y - d.height / 2})`)+ .style("cursor", "pointer");++ // Thin bar+ nodeGroups+ .append("rect")+ .attr("class", "node-rect")+ .attr("width", nodeWidth)+ .attr("height", d => d.height)+ .attr("fill", "hsl(var(--emerald-600))")+ .attr("rx", 2)+ .attr("ry", 2);++ // Bubble background+ nodeGroups+ .append("rect")+ .attr("class", "node-bubble")+ .attr("x", 12)+ .attr("y", 4)+ .attr("width", d => {+ const pathText = d.name;+ const statsText = `${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`;+ const maxLength = Math.max(pathText.length, statsText.length);+ const textWidth = maxLength * 6.5;+ return textWidth + 10;+ })+ .attr("height", 41)+ .attr("fill", "hsl(var(--neutral-800))")+ .attr("stroke", "hsl(var(--neutral-700))")+ .attr("stroke-width", 1)+ .attr("rx", 2)+ .attr("ry", 2);++ // Path text (clickable)+ const pathLinks = nodeGroups+ .append("a")+ .attr("xlink:href", d => `https://${domain}${d.name}`)+ .attr("target", "_blank")+ .attr("rel", "noopener noreferrer");++ pathLinks+ .append("text")+ .attr("class", "node-text node-link-text")+ .attr("x", 19)+ .attr("y", 21)+ .text(d => d.name)+ .attr("font-size", "12px")+ .attr("fill", "white")+ .attr("text-anchor", "start")+ .style("text-decoration", "none");++ // Add hover effect to show underline on link text+ pathLinks+ .on("mouseenter", function () {+ d3.select(this).select(".node-link-text").style("text-decoration", "underline");+ })+ .on("mouseleave", function () {+ d3.select(this).select(".node-link-text").style("text-decoration", "none");+ });++ // Count text+ nodeGroups+ .append("text")+ .attr("class", "node-text")+ .attr("x", 19)+ .attr("y", 37)+ .text(d => `${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`)+ .attr("font-size", "11px")+ .attr("fill", "hsl(var(--neutral-300))")+ .attr("text-anchor", "start");++ // Node hover effects+ nodeGroups+ .on("mouseenter", function (event, d) {+ const nodeId = d.id;+ const connectedNodeIds = new Set<string>([nodeId]);++ // Find all directly connected links+ const directLinks = links.filter(link => link.source === nodeId || link.target === nodeId);++ const allConnectedLinks: any[] = [];++ // For each direct link, find all connected paths+ directLinks.forEach(link => {+ allConnectedLinks.push(link);++ const forwardPaths = findAllConnectedPaths(link, "forward");+ const backwardPaths = findAllConnectedPaths(link, "backward");++ allConnectedLinks.push(...forwardPaths);+ allConnectedLinks.push(...backwardPaths);+ });++ const connectedLinkIds = new Set<string>();++ // Collect all connected link and node IDs+ allConnectedLinks.forEach(link => {+ const linkId = `${link.source}|||${link.target}`;+ connectedLinkIds.add(linkId);+ connectedNodeIds.add(link.source);+ connectedNodeIds.add(link.target);+ });++ d3.selectAll(".link")+ .attr("opacity", function () {+ const linkSource = d3.select(this).attr("data-source");+ const linkTarget = d3.select(this).attr("data-target");+ const thisLinkId = `${linkSource}|||${linkTarget}`;+ return connectedLinkIds.has(thisLinkId) ? 0.5 : 0.1;+ })+ .attr("stroke", function () {+ const linkSource = d3.select(this).attr("data-source");+ const linkTarget = d3.select(this).attr("data-target");+ const thisLinkId = `${linkSource}|||${linkTarget}`;+ return connectedLinkIds.has(thisLinkId) ? "hsl(var(--emerald-600))" : "hsl(var(--neutral-500))";+ });++ d3.selectAll(".node-rect").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 1 : 0.2;+ });++ d3.selectAll(".node-bubble").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 0.9 : 0.2;+ });++ d3.selectAll(".node-text").attr("opacity", function (nodeData: any) {+ return connectedNodeIds.has(nodeData.id) ? 1 : 0.3;+ });+ })+ .on("mouseleave", function () {+ d3.selectAll(".link").attr("opacity", 0.2).attr("stroke", "hsl(var(--neutral-500))");+ d3.selectAll(".node-rect").attr("opacity", 1);+ d3.selectAll(".node-bubble").attr("opacity", 0.8);+ d3.selectAll(".node-text").attr("opacity", 1);+ });+ }, [journeys, steps, maxJourneys, domain]);++ return (+ <div className="overflow-x-auto w-full">+ <svg ref={svgRef} className="min-w-full" />+ </div>+ );+}diff --git a/client/src/app/[site]/journeys/page.tsx b/client/src/app/[site]/journeys/page.tsxindex 23f791db7..5cb9b4960 100644--- a/client/src/app/[site]/journeys/page.tsx+++ b/client/src/app/[site]/journeys/page.tsx@@ -2,22 +2,17 @@import { useJourneys } from "@/api/analytics/useJourneys";import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";-import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";import { Skeleton } from "@/components/ui/skeleton";-import * as d3 from "d3";import { AlertCircle } from "lucide-react";-import { DateTime } from "luxon";-import { useEffect, useRef, useState } from "react";+import { useState } from "react";import { useGetSite } from "../../../api/admin/sites";-import { DateSelector } from "../../../components/DateSelector/DateSelector";-import { DateRangeMode, Time } from "../../../components/DateSelector/types";import { DisabledOverlay } from "../../../components/DisabledOverlay";import { useSetPageTitle } from "../../../hooks/useSetPageTitle";import { timeZone } from "../../../lib/dateTimeUtils";-import { MobileSidebar } from "../components/Sidebar/MobileSidebar";--const MAX_LINK_HEIGHT = 100;+import { JOURNEY_PAGE_FILTERS, useStore } from "../../../lib/store";+import { SubHeader } from "../components/SubHeader/SubHeader";+import { SankeyDiagram } from "./components/SankeyDiagram";export default function JourneysPage() {useSetPageTitle("Rybbit · Journeys");@@ -26,16 +21,9 @@ export default function JourneysPage() {const [maxJourneys, setMaxJourneys] = useState<number>(25);const { data: siteMetadata } = useGetSite();+ const { time } = useStore();- const [time, setTime] = useState<Time>({- mode: "range",- startDate: DateTime.now().minus({ days: 7 }).toISODate(),- endDate: DateTime.now().toISODate(),- wellKnown: "Last 7 days",- timeZone: timeZone,- } as DateRangeMode);-- const { data, isLoading, error } = useJourneys({+ const { data, isLoading, isFetching, error } = useJourneys({siteId: siteMetadata?.siteId,steps,timeZone: timeZone,@@ -43,412 +31,11 @@ export default function JourneysPage() {limit: maxJourneys,});- const svgRef = useRef<SVGSVGElement>(null);-- useEffect(() => {- if (!data?.journeys || !svgRef.current || !siteMetadata?.domain) return;-- const svg = d3.select(svgRef.current);- svg.selectAll("*").remove();-- const containerWidth = svgRef.current.parentElement?.clientWidth || 1000;-- // Build nodes and links- 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,- });- }- }- }- });-- // Calculate dimensions- const nodesByStep = d3.group(nodes, d => d.step);- const maxNodesInAnyStep = Math.max(...Array.from(nodesByStep.values()).map(stepNodes => stepNodes.length));-- const nodeWidth = 30;- const stepSpacing = 300;- const stepWidth = nodeWidth + stepSpacing;- const width = stepWidth * steps + nodeWidth;- const minHeight = 500;- const baseNodeHeight = 60;- const nodeSpacing = 20;- const height = Math.max(minHeight, (baseNodeHeight + nodeSpacing) * maxNodesInAnyStep + 100);-- const margin = { top: 30, right: 30, bottom: 30, left: 30 };- const innerHeight = height - margin.top - margin.bottom;-- const g = svg- .attr("width", width)- .attr("height", height)- .append("g")- .attr("transform", `translate(${margin.left},${margin.top})`);-- // 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);- });-- // Position nodes- 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;- });- });-- // Calculate node heights and metadata- const maxLinkValue = d3.max(links, link => link.value) || 1;- const linkWidthScale = d3.scaleLinear().domain([0, maxLinkValue]).range([1, MAX_LINK_HEIGHT]);-- nodes.forEach(node => {- 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);- const maxValue = Math.max(incomingValue, outgoingValue);-- node.height = linkWidthScale(maxValue);- node.count = node.step === 0 ? outgoingValue : incomingValue;-- const matchingJourney = data?.journeys?.find(journey => journey.path[node.step] === node.name);- node.percentage = matchingJourney ? matchingJourney.percentage : 0;- });-- // Calculate link positions- nodes.forEach(node => {- node.incomingLinks.sort((a: any, b: any) => b.value - a.value);- node.outgoingLinks.sort((a: any, b: any) => b.value - a.value);-- let currentOutY = 0;- node.outgoingLinks.forEach((link: any) => {- const linkHeight = linkWidthScale(link.value);- link.sourceY = currentOutY + linkHeight / 2;- currentOutY += linkHeight;- });-- if (currentOutY > 0 && node.outgoingLinks.length > 0) {- node.outgoingLinks.forEach((link: any) => {- link.sourceY = (link.sourceY / currentOutY) * node.height - node.height / 2;- link.sourceY += node.y;- });- }-- let currentInY = 0;- node.incomingLinks.forEach((link: any) => {- const linkHeight = linkWidthScale(link.value);- link.targetY = currentInY + linkHeight / 2;- currentInY += linkHeight;- });-- if (currentInY > 0 && node.incomingLinks.length > 0) {- node.incomingLinks.forEach((link: any) => {- link.targetY = (link.targetY / currentInY) * node.height - node.height / 2;- link.targetY += node.y;- });- }- });-- // Helper function to find all connected paths (all reachable nodes/edges)- const findAllConnectedPaths = (startLink: any, direction: "forward" | "backward"): any[] => {- const connectedLinks: any[] = [];- const queue: any[] = [startLink];- const visited = new Set<string>();-- while (queue.length > 0) {- const currentLink = queue.shift();- const linkId = `${currentLink.source}|||${currentLink.target}`;-- if (visited.has(linkId)) continue;- visited.add(linkId);- connectedLinks.push(currentLink);-- if (direction === "forward") {- const targetNode = nodes.find(n => n.id === currentLink.target);- // Follow all outgoing links- if (targetNode && targetNode.outgoingLinks.length > 0) {- targetNode.outgoingLinks.forEach((link: any) => queue.push(link));- }- } else {- const sourceNode = nodes.find(n => n.id === currentLink.source);- // Follow all incoming links- if (sourceNode && sourceNode.incomingLinks.length > 0) {- sourceNode.incomingLinks.forEach((link: any) => queue.push(link));- }- }- }-- return connectedLinks;- };-- // Draw links- const linkElements = 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 "";-- const sourceY = d.sourceY || source.y;- const targetY = d.targetY || target.y;- const sourceX = source.x + nodeWidth;- const targetX = target.x;-- const controlPoint1X = sourceX + stepSpacing / 3;- const controlPoint2X = targetX - stepSpacing / 3;-- return `M ${sourceX},${sourceY}- C ${controlPoint1X},${sourceY}- ${controlPoint2X},${targetY}- ${targetX},${targetY}`;- })- .attr("fill", "none")- .attr("stroke", "hsl(var(--neutral-500))")- .attr("stroke-width", d => linkWidthScale(d.value))- .attr("opacity", 0.2)- .attr("data-source", d => d.source)- .attr("data-target", d => d.target)- .style("cursor", "pointer")- .style("pointer-events", "visibleStroke")- .on("mouseenter", function (event, d) {- const forwardPaths = findAllConnectedPaths(d, "forward");- const backwardPaths = findAllConnectedPaths(d, "backward");- const allConnectedLinks = [d, ...forwardPaths, ...backwardPaths];-- const connectedLinkIds = new Set<string>();- const connectedNodeIds = new Set<string>();-- allConnectedLinks.forEach(link => {- const linkId = `${link.source}|||${link.target}`;- connectedLinkIds.add(linkId);- connectedNodeIds.add(link.source);- connectedNodeIds.add(link.target);- });-- d3.selectAll(".link")- .attr("opacity", function () {- const linkSource = d3.select(this).attr("data-source");- const linkTarget = d3.select(this).attr("data-target");- const thisLinkId = `${linkSource}|||${linkTarget}`;- return connectedLinkIds.has(thisLinkId) ? 0.5 : 0.1;- })- .attr("stroke", function () {- const linkSource = d3.select(this).attr("data-source");- const linkTarget = d3.select(this).attr("data-target");- const thisLinkId = `${linkSource}|||${linkTarget}`;- return connectedLinkIds.has(thisLinkId) ? "hsl(var(--emerald-600))" : "hsl(var(--neutral-500))";- });-- d3.selectAll(".node-rect").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 1 : 0.2;- });-- d3.selectAll(".node-bubble").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 0.9 : 0.2;- });-- d3.selectAll(".node-text").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 1 : 0.3;- });- })- .on("mouseleave", function () {- d3.selectAll(".link").attr("opacity", 0.2).attr("stroke", "hsl(var(--neutral-500))");- d3.selectAll(".node-rect").attr("opacity", 1);- d3.selectAll(".node-bubble").attr("opacity", 0.8);- d3.selectAll(".node-text").attr("opacity", 1);- })- .append("title")- .text(d => `Count: ${d.value}`);-- // Draw nodes- const nodeGroups = g- .selectAll(".node")- .data(nodes)- .join("g")- .attr("class", "node")- .attr("transform", d => `translate(${d.x},${d.y - d.height / 2})`)- .style("cursor", "pointer");-- // Thin bar- nodeGroups- .append("rect")- .attr("class", "node-rect")- .attr("width", nodeWidth)- .attr("height", d => d.height)- .attr("fill", "hsl(var(--emerald-600))")- .attr("rx", 2)- .attr("ry", 2);-- // Bubble background- nodeGroups- .append("rect")- .attr("class", "node-bubble")- .attr("x", 12)- .attr("y", 4)- .attr("width", d => {- const pathText = d.name;- const statsText = `${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`;- const maxLength = Math.max(pathText.length, statsText.length);- const textWidth = maxLength * 6.5;- return textWidth + 10;- })- .attr("height", 41)- .attr("fill", "hsl(var(--neutral-800))")- .attr("stroke", "hsl(var(--neutral-700))")- .attr("stroke-width", 1)- .attr("rx", 2)- .attr("ry", 2);-- // Path text (clickable)- const pathLinks = nodeGroups- .append("a")- .attr("xlink:href", d => `https://${siteMetadata.domain}${d.name}`)- .attr("target", "_blank")- .attr("rel", "noopener noreferrer");-- pathLinks- .append("text")- .attr("class", "node-text node-link-text")- .attr("x", 19)- .attr("y", 21)- .text(d => d.name)- .attr("font-size", "12px")- .attr("fill", "white")- .attr("text-anchor", "start")- .style("text-decoration", "none");-- // Add hover effect to show underline on link text- pathLinks- .on("mouseenter", function () {- d3.select(this).select(".node-link-text").style("text-decoration", "underline");- })- .on("mouseleave", function () {- d3.select(this).select(".node-link-text").style("text-decoration", "none");- });-- // Count text- nodeGroups- .append("text")- .attr("class", "node-text")- .attr("x", 19)- .attr("y", 37)- .text(d => `${d.count.toLocaleString()} (${d.percentage.toFixed(1)}%)`)- .attr("font-size", "11px")- .attr("fill", "hsl(var(--neutral-300))")- .attr("text-anchor", "start");-- // Node hover effects- nodeGroups- .on("mouseenter", function (event, d) {- const nodeId = d.id;- const connectedNodeIds = new Set<string>([nodeId]);-- // Find all directly connected links- const directLinks = links.filter(link => link.source === nodeId || link.target === nodeId);-- const allConnectedLinks: any[] = [];-- // For each direct link, find all connected paths- directLinks.forEach(link => {- allConnectedLinks.push(link);-- const forwardPaths = findAllConnectedPaths(link, "forward");- const backwardPaths = findAllConnectedPaths(link, "backward");-- allConnectedLinks.push(...forwardPaths);- allConnectedLinks.push(...backwardPaths);- });-- const connectedLinkIds = new Set<string>();-- // Collect all connected link and node IDs- allConnectedLinks.forEach(link => {- const linkId = `${link.source}|||${link.target}`;- connectedLinkIds.add(linkId);- connectedNodeIds.add(link.source);- connectedNodeIds.add(link.target);- });-- d3.selectAll(".link")- .attr("opacity", function () {- const linkSource = d3.select(this).attr("data-source");- const linkTarget = d3.select(this).attr("data-target");- const thisLinkId = `${linkSource}|||${linkTarget}`;- return connectedLinkIds.has(thisLinkId) ? 0.5 : 0.1;- })- .attr("stroke", function () {- const linkSource = d3.select(this).attr("data-source");- const linkTarget = d3.select(this).attr("data-target");- const thisLinkId = `${linkSource}|||${linkTarget}`;- return connectedLinkIds.has(thisLinkId) ? "hsl(var(--emerald-600))" : "hsl(var(--neutral-500))";- });-- d3.selectAll(".node-rect").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 1 : 0.2;- });-- d3.selectAll(".node-bubble").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 0.9 : 0.2;- });-- d3.selectAll(".node-text").attr("opacity", function (nodeData: any) {- return connectedNodeIds.has(nodeData.id) ? 1 : 0.3;- });- })- .on("mouseleave", function () {- d3.selectAll(".link").attr("opacity", 0.2).attr("stroke", "hsl(var(--neutral-500))");- d3.selectAll(".node-rect").attr("opacity", 1);- d3.selectAll(".node-bubble").attr("opacity", 0.8);- d3.selectAll(".node-text").attr("opacity", 1);- });- }, [data, steps, maxJourneys, siteMetadata]);-return (<DisabledOverlay message="User Journeys" featurePath="journeys"><div className="container mx-auto p-2 md:p-4">- <div className="md:hidden mb-2">- <MobileSidebar />- </div>+ <SubHeader availableFilters={JOURNEY_PAGE_FILTERS} /><div className="flex justify-end items-center gap-2 mb-2">- <DateSelector time={time} setTime={setTime} pastMinutesEnabled={false} /><Select value={steps.toString()} onValueChange={value => setSteps(Number(value))}><SelectTrigger className="w-[120px]"><SelectValue placeholder="Number of steps" />@@ -474,41 +61,41 @@ export default function JourneysPage() {</SelectContent></Select></div>- <Card className="w-full">- <CardHeader className="flex flex-row items-center justify-between">- <CardTitle>User Journeys</CardTitle>- </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 w-full">- <svg ref={svgRef} className="min-w-full" />- {/* <svg ref={svgRef} className="w-full h-[1000px]" /> */}+ {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 && siteMetadata?.domain ? (+ <div className="relative">+ {(isLoading || isFetching) && (+ <div className="absolute inset-0 bg-neutral-900/30 backdrop-blur-sm z-10 flex items-center justify-center">+ <div className="flex flex-col items-center gap-2">+ <div className="h-8 w-8 rounded-full border-2 border-accent-400 border-t-transparent animate-spin"></div>+ <span className="text-sm text-neutral-300">Loading journey data...</span>+ </div></div>- ) : null}- </CardContent>- </Card>+ )}+ <SankeyDiagram+ journeys={data.journeys}+ steps={steps}+ maxJourneys={maxJourneys}+ domain={siteMetadata.domain}+ />+ </div>+ ) : null}</div></DisabledOverlay>);diff --git a/client/src/lib/store.ts b/client/src/lib/store.tsindex d0812c5d8..7d3639ea4 100644--- a/client/src/lib/store.ts+++ b/client/src/lib/store.ts@@ -26,6 +26,7 @@ export const SESSION_PAGE_FILTERS: FilterParameter[] = ["utm_campaign","utm_term","utm_content",+ "user_id",];export const SESSION_REPLAY_PAGE_FILTERS: FilterParameter[] = [@@ -41,6 +42,7 @@ export const SESSION_REPLAY_PAGE_FILTERS: FilterParameter[] = ["device_type","referrer","channel",+ "user_id",];export const EVENT_FILTERS: FilterParameter[] = [@@ -74,6 +76,7 @@ export const EVENT_FILTERS: FilterParameter[] = ["entry_page","exit_page","dimensions",+ "user_id",];export const GOALS_PAGE_FILTERS: FilterParameter[] = [@@ -106,6 +109,31 @@ export const USER_PAGE_FILTERS: FilterParameter[] = ["city","device_type","referrer",+ "user_id",+];++export const JOURNEY_PAGE_FILTERS: FilterParameter[] = [+ "browser",+ "operating_system",+ "language",+ "country",+ "region",+ "city",+ "device_type",+ "referrer",+ "hostname",+ // "channel",+ // "utm_source",+ // "utm_medium",+ // "utm_campaign",+ // "utm_term",+ // "utm_content",+ "entry_page",+ "exit_page",+ "dimensions",+ "browser_version",+ "operating_system_version",+ "user_id",];type Store = {diff --git a/server/src/api/analytics/getJourneys.ts b/server/src/api/analytics/getJourneys.tsindex a8f07badc..8417d16a6 100644--- a/server/src/api/analytics/getJourneys.ts+++ b/server/src/api/analytics/getJourneys.ts@@ -1,7 +1,7 @@import { FastifyRequest, FastifyReply } from "fastify";import { clickhouse } from "../../db/clickhouse/clickhouse.js";import { DateTime } from "luxon";-import { getTimeStatement } from "./utils.js";+import { getTimeStatement, getFilterStatement } from "./utils.js";import { FilterParams } from "@rybbit/shared";export const getJourneys = async (@@ -16,7 +16,7 @@ export const getJourneys = async () => {try {const { site } = request.params;- const { steps = "3", startDate, endDate, timeZone = "UTC", limit = "100" } = request.query;+ const { steps = "3", startDate, endDate, timeZone = "UTC", limit = "100", filters } = request.query;const maxSteps = parseInt(steps, 10);const journeyLimit = parseInt(limit, 10);@@ -35,6 +35,7 @@ export const getJourneys = async (// Time conditions using getTimeStatementconst timeStatement = getTimeStatement(request.query);+ const filterStatement = getFilterStatement(filters);// Query to find sequences of events (journeys) for each userconst result = await clickhouse.query({@@ -49,16 +50,17 @@ export const getJourneys = async (pathname,timestampFROM events- WHERE+ WHEREsite_id = {siteId:Int32}${timeStatement || ""}+ ${filterStatement || ""}AND type = 'pageview'ORDER BY session_id, timestamp)GROUP BY session_idHAVING length(path_sequence) >= 2),-+journey_segments AS (SELECTarraySlice(path_sequence, 1, {maxSteps:Int32}) AS journey,@@ -68,15 +70,16 @@ export const getJourneys = async (ORDER BY sessions_count DESCLIMIT {journeyLimit:Int32})-+SELECTjourney,sessions_count,sessions_count * 100 / (- SELECT count(DISTINCT session_id)- FROM events- WHERE site_id = {siteId:Int32}+ SELECT count(DISTINCT session_id)+ FROM events+ WHERE site_id = {siteId:Int32}${timeStatement || ""}+ ${filterStatement || ""}) AS percentageFROM journey_segments`,