Skip to content

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

Add user traits to events and funnel step sessions (#925)

LuRy4194c97ce352
diff --git a/client/src/api/analytics/endpoints/events.ts b/client/src/api/analytics/endpoints/events.ts
index 1c30d16dc..7192f524f 100644
--- a/client/src/api/analytics/endpoints/events.ts
+++ b/client/src/api/analytics/endpoints/events.ts
@@ -34,6 +34,7 @@ export type Event = {
screen_height: number;
device_type: string;
type: string;
+ traits?: Record<string, unknown> | null;
};
// Response types for cursor-based API
diff --git a/client/src/app/[site]/events/components/EventLog/EventDetailsSheet.tsx b/client/src/app/[site]/events/components/EventLog/EventDetailsSheet.tsx
index 76f6a6283..9d8e920b7 100644
--- a/client/src/app/[site]/events/components/EventLog/EventDetailsSheet.tsx
+++ b/client/src/app/[site]/events/components/EventLog/EventDetailsSheet.tsx
@@ -88,6 +88,7 @@ export function EventDetailsSheet({ open, onOpenChange, event, site }: EventDeta
{getUserDisplayName({
identified_user_id: event.identified_user_id || undefined,
user_id: event.user_id,
+ traits: event.traits,
})}
</Link>
</div>
diff --git a/client/src/app/[site]/events/components/EventLog/EventRow.tsx b/client/src/app/[site]/events/components/EventLog/EventRow.tsx
index 26246e321..29d9b44a8 100644
--- a/client/src/app/[site]/events/components/EventLog/EventRow.tsx
+++ b/client/src/app/[site]/events/components/EventLog/EventRow.tsx
@@ -37,6 +37,7 @@ export function EventRow({ event, site, onClick }: EventRowProps) {
const displayName = getUserDisplayName({
identified_user_id: event.identified_user_id || undefined,
user_id: event.user_id,
+ traits: event.traits,
});
return (
diff --git a/server/src/api/analytics/events/getEvents.ts b/server/src/api/analytics/events/getEvents.ts
index 0412afc16..4f057b908 100644
--- a/server/src/api/analytics/events/getEvents.ts
+++ b/server/src/api/analytics/events/getEvents.ts
@@ -1,6 +1,6 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { processResults, getTimeStatement } from "../utils/utils.js";
+import { enrichWithTraits, processResults, getTimeStatement } from "../utils/utils.js";
import { FilterParams } from "@rybbit/shared";
import { getFilterStatement } from "../utils/getFilterStatement.js";
@@ -115,7 +115,8 @@ export async function getEvents(
});
const events = await processResults<GetEventsResponse[number]>(result);
- return res.send({ data: events });
+ const eventsWithTraits = await enrichWithTraits(events, Number(siteId));
+ return res.send({ data: eventsWithTraits });
}
// Mode B: Cursor-based pagination (initial load or scrolling back)
@@ -155,10 +156,10 @@ export async function getEvents(
});
const events = await processResults<GetEventsResponse[number]>(result);
-
+ const eventsWithTraits = await enrichWithTraits(events, Number(siteId));
return res.send({
- data: events,
+ data: eventsWithTraits,
cursor: {
hasMore: events.length === limit,
oldestTimestamp:
diff --git a/server/src/api/analytics/funnels/getFunnelStepSessions.ts b/server/src/api/analytics/funnels/getFunnelStepSessions.ts
index df6d146b8..db597ed31 100644
--- a/server/src/api/analytics/funnels/getFunnelStepSessions.ts
+++ b/server/src/api/analytics/funnels/getFunnelStepSessions.ts
@@ -2,7 +2,7 @@ import { FilterParams } from "@rybbit/shared";
import { FastifyReply, FastifyRequest } from "fastify";
import SqlString from "sqlstring";
import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
-import { getTimeStatement, patternToRegex, processResults } from "../utils/utils.js";
+import { enrichWithTraits, getTimeStatement, patternToRegex, processResults } from "../utils/utils.js";
import { GetSessionsResponse } from "../sessions/getSessions.js";
import { getFilterStatement } from "../utils/getFilterStatement.js";
@@ -203,6 +203,7 @@ export async function getFunnelStepSessions(req: FastifyRequest<GetFunnelStepSes
SELECT
e.session_id,
e.user_id,
+ argMax(e.identified_user_id, e.timestamp) AS identified_user_id,
argMax(e.country, e.timestamp) AS country,
argMax(e.region, e.timestamp) AS region,
argMax(e.city, e.timestamp) AS city,
@@ -262,8 +263,9 @@ export async function getFunnelStepSessions(req: FastifyRequest<GetFunnelStepSes
},
});
- const data = await processResults<GetSessionsResponse[number]>(result);
- return res.send({ data });
+ const data = await processResults<Omit<GetSessionsResponse[number], "traits">>(result);
+ const dataWithTraits = await enrichWithTraits(data, Number(siteId));
+ return res.send({ data: dataWithTraits });
} catch (error) {
console.error("Error fetching funnel step sessions:", error);
return res.status(500).send({ error: "Failed to fetch funnel step sessions" });