Skip to content

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

Enhance journey filtering capabilities in useJourneys and JourneysPage components

Bill Yange26ace80ca59
diff --git a/client/src/api/analytics/useJourneys.ts b/client/src/api/analytics/useJourneys.ts
index cce56a99c..d81b0ab94 100644
--- a/client/src/api/analytics/useJourneys.ts
+++ b/client/src/api/analytics/useJourneys.ts
@@ -10,6 +10,7 @@ export interface JourneyParams {
timeZone?: string;
time: Time;
limit?: number;
+ stepFilters?: Record<number, string>;
}
export interface Journey {
@@ -22,13 +23,13 @@ export interface JourneysResponse {
journeys: Journey[];
}
-export const useJourneys = ({ siteId, steps = 3, time, limit = 100 }: JourneyParams) => {
+export const useJourneys = ({ siteId, steps = 3, time, limit = 100, stepFilters }: JourneyParams) => {
const { startDate, endDate } = getStartAndEndDate(time);
const filteredFilters = getFilteredFilters(JOURNEY_PAGE_FILTERS);
return useQuery<JourneysResponse>({
- queryKey: ["journeys", siteId, steps, startDate, endDate, timeZone, limit, filteredFilters],
+ queryKey: ["journeys", siteId, steps, startDate, endDate, timeZone, limit, filteredFilters, stepFilters],
queryFn: async () => {
const params: Record<string, any> = {};
@@ -38,6 +39,9 @@ export const useJourneys = ({ siteId, steps = 3, time, limit = 100 }: JourneyPar
if (timeZone) params.timeZone = timeZone;
if (limit) params.limit = limit;
if (filteredFilters) params.filters = filteredFilters;
+ if (stepFilters && Object.keys(stepFilters).length > 0) {
+ params.stepFilters = JSON.stringify(stepFilters);
+ }
return authedFetch<JourneysResponse>(`/journeys/${siteId}`, params);
},
diff --git a/client/src/app/[site]/journeys/page.tsx b/client/src/app/[site]/journeys/page.tsx
index 450bbc32c..585447140 100644
--- a/client/src/app/[site]/journeys/page.tsx
+++ b/client/src/app/[site]/journeys/page.tsx
@@ -2,6 +2,7 @@
import { useJourneys } from "@/api/analytics/useJourneys";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
+import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { AlertCircle } from "lucide-react";
import { useState } from "react";
@@ -18,6 +19,7 @@ export default function JourneysPage() {
const [steps, setSteps] = useState<number>(3);
const [maxJourneys, setMaxJourneys] = useState<number>(25);
+ const [stepFilters, setStepFilters] = useState<Record<number, string>>({});
const { data: siteMetadata } = useGetSite();
const { time } = useStore();
@@ -28,6 +30,7 @@ export default function JourneysPage() {
timeZone: timeZone,
time,
limit: maxJourneys,
+ stepFilters,
});
return (
@@ -40,7 +43,7 @@ export default function JourneysPage() {
<SelectValue placeholder="Number of steps" />
</SelectTrigger>
<SelectContent>
- {[2, 3, 4, 5, 6, 7, 8, 9, 10].map(step => (
+ {[2, 3, 4, 5, 6].map(step => (
<SelectItem key={step} value={step.toString()}>
{step} steps
</SelectItem>
@@ -61,23 +64,7 @@ export default function JourneysPage() {
</Select>
</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 && siteMetadata?.domain ? (
+ {siteMetadata?.domain ? (
<div className="relative">
{isLoading && (
<div className="absolute inset-0 bg-neutral-900/30 backdrop-blur-sm z-10 flex items-center justify-center">
@@ -91,21 +78,53 @@ export default function JourneysPage() {
{Array.from({ length: steps }, (_, i) => (
<div
key={i}
- className="flex-1 h-10 bg-neutral-800 flex items-center text-white text-sm relative"
+ className="flex-1 h-10 bg-neutral-800 flex items-center text-white text-sm relative px-3"
style={{
clipPath: "polygon(0 0, 10px 50%, 0 100%, calc(100% - 10px) 100%, 100% 50%, calc(100% - 10px) 0)",
}}
>
- <span className="pl-6">Step {i + 1}</span>
+ <span className="ml-2 whitespace-nowrap text-neutral-200">Step {i + 1}</span>
+ <Input
+ inputSize="sm"
+ placeholder="path filter"
+ value={stepFilters[i] || ""}
+ onChange={e => {
+ const newFilters = { ...stepFilters };
+ if (e.target.value) {
+ newFilters[i] = e.target.value;
+ } else {
+ delete newFilters[i];
+ }
+ setStepFilters(newFilters);
+ }}
+ className="h-7 bg-neutral-900 border-neutral-700 text-white placeholder:text-neutral-500 ml-3 mr-3"
+ />
</div>
))}
</div>
- <SankeyDiagram
- journeys={data.journeys}
- steps={steps}
- maxJourneys={maxJourneys}
- domain={siteMetadata.domain}
- />
+ {data?.journeys?.length && data?.journeys?.length > 0 ? (
+ <SankeyDiagram
+ journeys={data.journeys}
+ steps={steps}
+ maxJourneys={maxJourneys}
+ domain={siteMetadata.domain}
+ />
+ ) : null}
+ {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>
+ )}
</div>
) : null}
</div>
diff --git a/server/src/api/analytics/getJourneys.ts b/server/src/api/analytics/getJourneys.ts
index 8417d16a6..398d2954d 100644
--- a/server/src/api/analytics/getJourneys.ts
+++ b/server/src/api/analytics/getJourneys.ts
@@ -10,13 +10,14 @@ export const getJourneys = async (
Querystring: FilterParams<{
steps?: string;
limit?: string;
+ stepFilters?: string;
}>;
}>,
reply: FastifyReply
) => {
try {
const { site } = request.params;
- const { steps = "3", startDate, endDate, timeZone = "UTC", limit = "100", filters } = request.query;
+ const { steps = "3", startDate, endDate, timeZone = "UTC", limit = "100", filters, stepFilters } = request.query;
const maxSteps = parseInt(steps, 10);
const journeyLimit = parseInt(limit, 10);
@@ -37,6 +38,26 @@ export const getJourneys = async (
const timeStatement = getTimeStatement(request.query);
const filterStatement = getFilterStatement(filters);
+ // Parse step filters
+ let parsedStepFilters: Record<number, string> = {};
+ if (stepFilters) {
+ try {
+ parsedStepFilters = JSON.parse(stepFilters);
+ } catch (error) {
+ return reply.status(400).send({
+ error: "Invalid stepFilters format",
+ });
+ }
+ }
+
+ // Build step filter conditions for the HAVING clause
+ const stepFilterConditions = Object.entries(parsedStepFilters)
+ .map(([step, path]) => {
+ const stepIndex = parseInt(step, 10) + 1; // ClickHouse arrays are 1-indexed
+ return `journey[${stepIndex}] = '${path.replace(/'/g, "''")}'`;
+ })
+ .join(" AND ");
+
// Query to find sequences of events (journeys) for each user
const result = await clickhouse.query({
query: `
@@ -67,6 +88,7 @@ export const getJourneys = async (
count() AS sessions_count
FROM user_paths
GROUP BY journey
+ ${stepFilterConditions ? `HAVING ${stepFilterConditions}` : ""}
ORDER BY sessions_count DESC
LIMIT {journeyLimit:Int32}
)