Skip to content

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

Enhance funnel step functionality with event property filtering

Bill Yangf8b579511bc4
diff --git a/client/src/api/analytics/useGetFunnel.ts b/client/src/api/analytics/useGetFunnel.ts
index 5edd4fddf..deff39dfe 100644
--- a/client/src/api/analytics/useGetFunnel.ts
+++ b/client/src/api/analytics/useGetFunnel.ts
@@ -8,6 +8,8 @@ export type FunnelStep = {
value: string;
name?: string;
type: "page" | "event";
+ eventPropertyKey?: string;
+ eventPropertyValue?: string | number | boolean;
};
export type FunnelRequest = {
diff --git a/client/src/app/[site]/funnels/components/FunnelForm.tsx b/client/src/app/[site]/funnels/components/FunnelForm.tsx
index 2a81c1d37..939de5155 100644
--- a/client/src/app/[site]/funnels/components/FunnelForm.tsx
+++ b/client/src/app/[site]/funnels/components/FunnelForm.tsx
@@ -17,6 +17,8 @@ import { Filter } from "../../../../lib/store";
import { FilterComponent } from "../../components/shared/Filters/FilterComponent";
import { Funnel } from "./Funnel";
import { SavedFunnel } from "../../../../api/analytics/useGetFunnels";
+import { Switch } from "../../../../components/ui/switch";
+import { Label } from "../../../../components/ui/label";
interface FunnelFormProps {
name: string;
@@ -61,10 +63,17 @@ export function FunnelForm({
}: FunnelFormProps) {
console.info(funnelData);
const [showFilters, setShowFilters] = useState(filters.length > 0);
+ // State to track which event steps have property filtering enabled
+ const [useProperties, setUseProperties] = useState<boolean[]>(() =>
+ steps.map(
+ (step) => !!step.eventPropertyKey && step.eventPropertyValue !== undefined
+ )
+ );
// Handle adding a new step
const addStep = () => {
setSteps([...steps, { type: "page", value: "", name: "" }]);
+ setUseProperties([...useProperties, false]);
};
// Handle removing a step
@@ -73,6 +82,10 @@ export function FunnelForm({
const newSteps = [...steps];
newSteps.splice(index, 1);
setSteps(newSteps);
+
+ const newUseProperties = [...useProperties];
+ newUseProperties.splice(index, 1);
+ setUseProperties(newUseProperties);
};
// Handle step input changes
@@ -89,8 +102,40 @@ export function FunnelForm({
// Handle step type changes
const updateStepType = (index: number, type: "page" | "event") => {
const newSteps = [...steps];
- newSteps[index] = { ...newSteps[index], type };
+ newSteps[index] = {
+ ...newSteps[index],
+ type,
+ // Clear property fields if switching from event to page
+ ...(type === "page"
+ ? { eventPropertyKey: undefined, eventPropertyValue: undefined }
+ : {}),
+ };
setSteps(newSteps);
+
+ // Disable property filtering if switching to page type
+ if (type === "page" && useProperties[index]) {
+ const newUseProperties = [...useProperties];
+ newUseProperties[index] = false;
+ setUseProperties(newUseProperties);
+ }
+ };
+
+ // Handle property filtering toggle
+ const togglePropertyFiltering = (index: number, enabled: boolean) => {
+ const newUseProperties = [...useProperties];
+ newUseProperties[index] = enabled;
+ setUseProperties(newUseProperties);
+
+ // Clear property fields if disabling
+ if (!enabled) {
+ const newSteps = [...steps];
+ newSteps[index] = {
+ ...newSteps[index],
+ eventPropertyKey: undefined,
+ eventPropertyValue: undefined,
+ };
+ setSteps(newSteps);
+ }
};
// Handle filter operations
@@ -230,6 +275,57 @@ export function FunnelForm({
updateStep(index, "name", e.target.value)
}
/>
+
+ {/* Property filtering for event steps */}
+ {step.type === "event" && (
+ <div className="mt-2 space-y-2">
+ <div className="flex items-center space-x-2">
+ <Switch
+ checked={useProperties[index]}
+ onCheckedChange={(checked) =>
+ togglePropertyFiltering(index, checked)
+ }
+ id={`use-properties-${index}`}
+ />
+ <Label htmlFor={`use-properties-${index}`}>
+ Filter by event property
+ </Label>
+ </div>
+
+ {useProperties[index] && (
+ <div className="grid grid-cols-2 gap-2 mt-2">
+ <Input
+ placeholder="Property key"
+ className="dark:border-neutral-700"
+ value={step.eventPropertyKey || ""}
+ onChange={(e) =>
+ updateStep(
+ index,
+ "eventPropertyKey",
+ e.target.value
+ )
+ }
+ />
+ <Input
+ placeholder="Property value"
+ className="dark:border-neutral-700"
+ value={
+ step.eventPropertyValue !== undefined
+ ? String(step.eventPropertyValue)
+ : ""
+ }
+ onChange={(e) =>
+ updateStep(
+ index,
+ "eventPropertyValue",
+ e.target.value
+ )
+ }
+ />
+ </div>
+ )}
+ </div>
+ )}
</div>
<Button
diff --git a/client/src/app/[site]/funnels/components/FunnelRow.tsx b/client/src/app/[site]/funnels/components/FunnelRow.tsx
index 0ebcba88f..68daeac66 100644
--- a/client/src/app/[site]/funnels/components/FunnelRow.tsx
+++ b/client/src/app/[site]/funnels/components/FunnelRow.tsx
@@ -127,6 +127,11 @@ export function FunnelRow({ funnel }: FunnelRowProps) {
)}
<span className="max-w-[120px] overflow-hidden text-ellipsis inline-block">
{step.name || step.value}
+ {step.type === "event" && step.eventPropertyKey && (
+ <span className="text-xs text-yellow-400 ml-1">
+ *
+ </span>
+ )}
</span>
</span>
</TooltipTrigger>
@@ -143,6 +148,15 @@ export function FunnelRow({ funnel }: FunnelRowProps) {
{step.name}
</div>
)}
+ {step.type === "event" &&
+ step.eventPropertyKey &&
+ step.eventPropertyValue !== undefined && (
+ <div>
+ <span className="font-semibold">Property:</span>{" "}
+ {step.eventPropertyKey} ={" "}
+ {String(step.eventPropertyValue)}
+ </div>
+ )}
</TooltipContent>
</Tooltip>
</TooltipProvider>
diff --git a/client/src/components/ui/button.tsx b/client/src/components/ui/button.tsx
index 8949c84ca..f4b6f3196 100644
--- a/client/src/components/ui/button.tsx
+++ b/client/src/components/ui/button.tsx
@@ -16,7 +16,7 @@ const buttonVariants = cva(
accent:
"bg-neutral-500 text-neutral-900 shadow-sm hover:bg-neutral-500/80 dark:bg-accent-600 dark:text-neutral-50 dark:hover:bg-accent-600/90",
success:
- "bg-green-500 text-neutral-50 border border-green-500 shadow-sm hover:bg-green-500/90 dark:bg-green-800 dark:border-green-600 dark:text-neutral-50 dark:hover:bg-green-800/90 dark:hover:border-green-500",
+ "bg-accent-500 text-neutral-50 border border-accent-600 shadow-sm hover:bg-accent-500/90 dark:bg-accent-800 dark:border-accent-600 dark:text-neutral-50 dark:hover:bg-accent-800/90 dark:hover:border-accent-500",
destructive:
"bg-red-500 text-neutral-50 border border-red-500 shadow-sm hover:bg-red-500/90 dark:bg-red-900 dark:border-red-700 dark:text-neutral-50 dark:hover:bg-red-900/90 dark:hover:border-red-500",
warning:
diff --git a/server/src/api/analytics/getFunnel.ts b/server/src/api/analytics/getFunnel.ts
index 4c3c91af6..bd8412df5 100644
--- a/server/src/api/analytics/getFunnel.ts
+++ b/server/src/api/analytics/getFunnel.ts
@@ -14,6 +14,8 @@ type FunnelStep = {
value: string;
name?: string;
type: "page" | "event";
+ eventPropertyKey?: string;
+ eventPropertyValue?: string | number | boolean;
};
type Filter = {
@@ -84,9 +86,39 @@ export async function getFunnel(
patternToRegex(step.value)
)})`;
} else {
- return `type = 'custom_event' AND event_name = ${SqlString.escape(
+ // Start with the base event match condition
+ let eventClause = `type = 'custom_event' AND event_name = ${SqlString.escape(
step.value
)}`;
+
+ // Add property matching if both key and value are provided
+ if (step.eventPropertyKey && step.eventPropertyValue !== undefined) {
+ // Access the sub-column directly for native JSON type
+ const propValueAccessor = `props.${SqlString.escapeId(
+ step.eventPropertyKey
+ )}`;
+
+ // Comparison needs to handle the dynamic type returned
+ // Let ClickHouse handle the comparison based on the provided value type
+ if (typeof step.eventPropertyValue === "string") {
+ eventClause += ` AND toString(${propValueAccessor}) = ${SqlString.escape(
+ step.eventPropertyValue
+ )}`;
+ } else if (typeof step.eventPropertyValue === "number") {
+ // Use toFloat64 or toInt* depending on expected number type
+ eventClause += ` AND toFloat64OrNull(${propValueAccessor}) = ${SqlString.escape(
+ step.eventPropertyValue
+ )}`;
+ } else if (typeof step.eventPropertyValue === "boolean") {
+ // Booleans might be stored as 0/1 or true/false in JSON
+ // Comparing toUInt8 seems robust
+ eventClause += ` AND toUInt8OrNull(${propValueAccessor}) = ${
+ step.eventPropertyValue ? 1 : 0
+ }`;
+ }
+ }
+
+ return eventClause;
}
});
@@ -100,7 +132,8 @@ export async function getFunnel(
timestamp,
pathname,
event_name,
- type
+ type,
+ props
FROM events
WHERE
site_id = {siteId:Int32}