Skip to content

Commit f002031

Browse files
committed
fix: custom events funnels and sessions
1 parent 8e538cc commit f002031

File tree

2 files changed

+92
-25
lines changed

2 files changed

+92
-25
lines changed

apps/api/src/query/builders/sessions.ts

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,60 @@ export const SessionsBuilders: Record<string, SimpleQueryConfig> = {
136136
ORDER BY first_visit DESC
137137
LIMIT {limit:Int32} OFFSET {offset:Int32}
138138
),
139-
session_events AS (
139+
all_events AS (
140140
SELECT
141+
e.id,
141142
e.session_id,
143+
e.time,
144+
e.event_name,
145+
e.path,
146+
CASE
147+
WHEN e.event_name NOT IN ('screen_view', 'page_exit', 'web_vitals', 'link_out')
148+
AND e.properties IS NOT NULL
149+
AND e.properties != '{}'
150+
THEN CAST(e.properties AS String)
151+
ELSE NULL
152+
END as properties
153+
FROM analytics.events e
154+
INNER JOIN session_list sl ON e.session_id = sl.session_id
155+
WHERE e.client_id = {websiteId:String}
156+
157+
UNION ALL
158+
159+
SELECT
160+
ce.id,
161+
ce.session_id,
162+
ce.timestamp as time,
163+
ce.event_name,
164+
'' as path,
165+
CASE
166+
WHEN ce.properties IS NOT NULL
167+
AND ce.properties != '{}'
168+
THEN CAST(ce.properties AS String)
169+
ELSE NULL
170+
END as properties
171+
FROM analytics.custom_events ce
172+
INNER JOIN session_list sl ON ce.session_id = sl.session_id
173+
WHERE ce.client_id = {websiteId:String}
174+
),
175+
session_events AS (
176+
SELECT
177+
session_id,
142178
groupArray(
143179
tuple(
144-
e.id,
145-
e.time,
146-
e.event_name,
147-
e.path,
148-
CASE
149-
WHEN e.event_name NOT IN ('screen_view', 'page_exit', 'web_vitals', 'link_out')
150-
AND e.properties IS NOT NULL
151-
AND e.properties != '{}'
152-
THEN CAST(e.properties AS String)
153-
ELSE NULL
154-
END
180+
id,
181+
time,
182+
event_name,
183+
path,
184+
properties
155185
)
156186
) as events
157-
FROM analytics.events e
158-
INNER JOIN session_list sl ON e.session_id = sl.session_id
159-
WHERE e.client_id = {websiteId:String}
160-
${combinedWhereClause}
161-
GROUP BY e.session_id
187+
FROM (
188+
SELECT * FROM all_events
189+
ORDER BY time ASC
190+
)
191+
${combinedWhereClause}
192+
GROUP BY session_id
162193
)
163194
SELECT
164195
sl.session_id,
@@ -210,4 +241,4 @@ export const SessionsBuilders: Record<string, SimpleQueryConfig> = {
210241
timeField: 'time',
211242
customizable: true,
212243
} satisfies SimpleQueryConfig,
213-
};
244+
};

packages/rpc/src/lib/analytics-utils.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,53 @@ const buildStepQuery = (
111111
const whereCondition = buildWhereCondition(step, params);
112112
const referrerSelect = includeReferrer ? ', any(referrer) as referrer' : '';
113113

114+
// For PAGE_VIEW, only query analytics.events
115+
if (step.type === 'PAGE_VIEW') {
116+
return `
117+
SELECT
118+
${stepIndex + 1} as step_number,
119+
{${stepNameKey}:String} as step_name,
120+
session_id,
121+
MIN(time) as first_occurrence${referrerSelect}
122+
FROM analytics.events
123+
WHERE client_id = {websiteId:String}
124+
AND time >= parseDateTimeBestEffort({startDate:String})
125+
AND time <= parseDateTimeBestEffort({endDate:String})
126+
AND ${whereCondition}${filterConditions}
127+
GROUP BY session_id`;
128+
}
129+
130+
// For custom EVENT, query both analytics.events and analytics.custom_events
131+
const targetKey = `target_${step.step_number - 1}`;
132+
const referrerSelectCustom = includeReferrer ? ", '' as referrer" : '';
133+
114134
return `
115135
SELECT
116136
${stepIndex + 1} as step_number,
117137
{${stepNameKey}:String} as step_name,
118138
session_id,
119-
MIN(time) as first_occurrence${referrerSelect}
120-
FROM analytics.events
121-
WHERE client_id = {websiteId:String}
122-
AND time >= parseDateTimeBestEffort({startDate:String})
123-
AND time <= parseDateTimeBestEffort({endDate:String})
124-
AND ${whereCondition}${filterConditions}
139+
MIN(first_occurrence) as first_occurrence${referrerSelect}
140+
FROM (
141+
SELECT
142+
session_id,
143+
time as first_occurrence${includeReferrer ? ', referrer' : ''}
144+
FROM analytics.events
145+
WHERE client_id = {websiteId:String}
146+
AND time >= parseDateTimeBestEffort({startDate:String})
147+
AND time <= parseDateTimeBestEffort({endDate:String})
148+
AND event_name = {${targetKey}:String}${filterConditions}
149+
150+
UNION ALL
151+
152+
SELECT
153+
session_id,
154+
timestamp as first_occurrence${referrerSelectCustom}
155+
FROM analytics.custom_events
156+
WHERE client_id = {websiteId:String}
157+
AND timestamp >= parseDateTimeBestEffort({startDate:String})
158+
AND timestamp <= parseDateTimeBestEffort({endDate:String})
159+
AND event_name = {${targetKey}:String}
160+
)
125161
GROUP BY session_id`;
126162
};
127163

@@ -857,4 +893,4 @@ export const processFunnelAnalyticsByReferrer = async (
857893
const referrer_analytics = aggregateReferrerAnalytics(referrerAnalytics);
858894

859895
return { referrer_analytics };
860-
};
896+
};

0 commit comments

Comments
 (0)