Skip to content

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

Improve journeys (#204)

Bill Yang313463982c54
diff --git a/client/src/api/analytics/useJourneys.ts b/client/src/api/analytics/useJourneys.ts
index b59be3ddc..f3db401ca 100644
--- a/client/src/api/analytics/useJourneys.ts
+++ b/client/src/api/analytics/useJourneys.ts
@@ -8,6 +8,7 @@ export interface JourneyParams {
steps?: number;
timezone?: string;
time: Time;
+ limit?: number;
}
export interface Journey {
@@ -25,11 +26,12 @@ export const useJourneys = ({
steps = 3,
timezone = "UTC",
time,
+ limit = 100,
}: JourneyParams) => {
const { startDate, endDate } = getStartAndEndDate(time);
return useQuery<JourneysResponse>({
- queryKey: ["journeys", siteId, steps, startDate, endDate, timezone],
+ queryKey: ["journeys", siteId, steps, startDate, endDate, timezone, limit],
queryFn: async () => {
let url = `${BACKEND_URL}/journeys/${siteId}`;
const params = new URLSearchParams();
@@ -38,6 +40,7 @@ export const useJourneys = ({
if (startDate) params.append("startDate", startDate);
if (endDate) params.append("endDate", endDate);
if (timezone) params.append("timezone", timezone);
+ if (limit) params.append("limit", limit.toString());
const queryString = params.toString();
if (queryString) url += `?${queryString}`;
diff --git a/client/src/app/[site]/journeys/page.tsx b/client/src/app/[site]/journeys/page.tsx
index 3921579a2..2118b15a3 100644
--- a/client/src/app/[site]/journeys/page.tsx
+++ b/client/src/app/[site]/journeys/page.tsx
@@ -44,6 +44,7 @@ export default function JourneysPage() {
steps,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
time,
+ limit: maxJourneys,
});
const svgRef = useRef<SVGSVGElement>(null);
@@ -54,21 +55,10 @@ export default function JourneysPage() {
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})`);
+ // Get container width for responsive sizing
+ const containerWidth = svgRef.current.parentElement?.clientWidth || 1000;
- // Create a Sankey diagram
- // First, build nodes and links from journey data
+ // Build nodes first to calculate dimensions properly
const nodes: any[] = [];
const links: any[] = [];
@@ -108,6 +98,48 @@ export default function JourneysPage() {
}
});
+ // Calculate dimensions based on node distribution
+ const nodesByStep = d3.group(nodes, (d) => d.step);
+
+ // Calculate max nodes per step for height calculation
+ const maxNodesInAnyStep = Math.max(
+ ...Array.from(nodesByStep.values()).map((stepNodes) => stepNodes.length)
+ );
+
+ // Width calculation that fills available space but doesn't shrink below minimum
+ const minStepWidth = 300; // Minimum width per step
+ const minTotalWidth = minStepWidth * steps; // Minimum total width
+
+ // Calculate step width based on available space
+ const stepWidth = Math.max(
+ minStepWidth,
+ containerWidth / steps // Use full container width if it's large enough
+ );
+
+ // Calculate total width based on step width
+ const width = stepWidth * steps;
+
+ const minHeight = 500;
+
+ // Calculate height based on maximum node cardinality in any step
+ const baseNodeHeight = 60; // Height per node
+ const nodeSpacing = 20; // Spacing between nodes
+ const height = Math.max(
+ minHeight,
+ (baseNodeHeight + nodeSpacing) * maxNodesInAnyStep + 100 // 100px for margins
+ );
+
+ const margin = { top: 30, right: 30, bottom: 30, left: 30 };
+ 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})`);
+
// Track incoming and outgoing links for each node
links.forEach((link) => {
const sourceNode = nodes.find((n) => n.id === link.source);
@@ -117,12 +149,6 @@ export default function JourneysPage() {
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;
@@ -247,9 +273,13 @@ export default function JourneysPage() {
const sourceX = source.x + 10; // End of source bar (x + width)
const targetX = target.x; // Start of target bar
+ // Control points at 1/3 and 2/3 of the distance between nodes
+ const controlPoint1X = sourceX + stepWidth / 3;
+ const controlPoint2X = targetX - stepWidth / 3;
+
return `M ${sourceX},${sourceY}
- C ${sourceX + stepWidth / 3},${sourceY}
- ${targetX - stepWidth / 3},${targetY}
+ C ${controlPoint1X},${sourceY}
+ ${controlPoint2X},${targetY}
${targetX},${targetY}`;
})
.attr("fill", "none")
@@ -429,7 +459,7 @@ export default function JourneysPage() {
<SelectValue placeholder="Max journeys" />
</SelectTrigger>
<SelectContent>
- {[10, 25, 50, 100].map((count) => (
+ {[10, 25, 50, 100, 150, 200].map((count) => (
<SelectItem key={count} value={count.toString()}>
{count} journeys
</SelectItem>
@@ -469,8 +499,9 @@ export default function JourneysPage() {
)}
{data?.journeys?.length && data?.journeys?.length > 0 ? (
- <div className="overflow-x-auto">
- <svg ref={svgRef} className="w-full h-[1000px]" />
+ <div className="overflow-x-auto w-full">
+ <svg ref={svgRef} className="min-w-full" />
+ {/* <svg ref={svgRef} className="w-full h-[1000px]" /> */}
</div>
) : null}
</CardContent>
diff --git a/server/src/api/analytics/getJourneys.ts b/server/src/api/analytics/getJourneys.ts
index 552adde9d..55b3f76d8 100644
--- a/server/src/api/analytics/getJourneys.ts
+++ b/server/src/api/analytics/getJourneys.ts
@@ -11,15 +11,23 @@ export const getJourneys = async (
startDate?: string;
endDate?: string;
timezone?: string;
+ limit?: string;
};
}>,
reply: FastifyReply
) => {
try {
const { site } = request.params;
- const { steps = "3", startDate, endDate, timezone = "UTC" } = request.query;
+ const {
+ steps = "3",
+ startDate,
+ endDate,
+ timezone = "UTC",
+ limit = "100",
+ } = request.query;
const maxSteps = parseInt(steps, 10);
+ const journeyLimit = parseInt(limit, 10);
if (isNaN(maxSteps) || maxSteps < 2 || maxSteps > 10) {
return reply.status(400).send({
@@ -27,6 +35,12 @@ export const getJourneys = async (
});
}
+ if (isNaN(journeyLimit) || journeyLimit < 1 || journeyLimit > 500) {
+ return reply.status(400).send({
+ error: "Limit parameter must be a number between 1 and 500",
+ });
+ }
+
// Time conditions using getTimeStatement
const timeStatement = getTimeStatement({
date:
@@ -71,7 +85,7 @@ export const getJourneys = async (
FROM user_paths
GROUP BY journey
ORDER BY sessions_count DESC
- LIMIT 100
+ LIMIT {journeyLimit:Int32}
)
SELECT
@@ -87,7 +101,8 @@ export const getJourneys = async (
`,
query_params: {
siteId: parseInt(site, 10),
- maxSteps: parseInt(steps, 10),
+ maxSteps: maxSteps,
+ journeyLimit: journeyLimit,
},
});