Fix excludeBounce query, simplify bounce definition for SPA websites, query routing for performance, add tests

This commit is contained in:
Francis Cao committed 2026-07-30 09:34:35 -07:00
1 parent 0a80d09d0b
commit b3aec54bb3
12 files changed
+410 -150

No files matched your search

+44 -23
View File
@@ -1,5 +1,9 @@
import clickhouse from '@/lib/clickhouse';
import { BOUNCE_THRESHOLD, EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
import {
EVENT_TYPE,
FILTER_COLUMNS,
SESSION_COLUMNS,
} from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -31,7 +35,7 @@ async function relationalQuery(
): Promise<BreakdownData[]> {
const { getTimestampDiffSQL, parseFilters, rawQuery } = prisma;
const { startDate, endDate, fields } = parameters;
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters(
const { filterQuery, joinSessionQuery, cohortQuery, excludeBounceQuery, queryParams } = parseFilters(
{
...filters,
websiteId,
@@ -43,6 +47,22 @@ async function relationalQuery(
joinSession: !!fields.find((name: string) => SESSION_COLUMNS.includes(name)),
},
);
const needsBounceEvents = filters.excludeBounce !== true;
const bounceQuery = needsBounceEvents
? `sum(case when t.c = 1 and coalesce(e.has_custom_event, 0) = 0 then 1 else 0 end) as "bounces",`
: '0 as "bounces",';
const visitEventsJoin = needsBounceEvents
? `left join (
select session_id, visit_id, 1 as "has_custom_event"
from website_event
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and event_type = ${EVENT_TYPE.customEvent}
group by 1, 2
) as e
on e.session_id = t.session_id
and e.visit_id = t.visit_id`
: '';
return rawQuery(
`
@@ -50,7 +70,7 @@ async function relationalQuery(
sum(t.c) as "views",
count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits",
sum(case when t.c = 1 and t.events_count < ${BOUNCE_THRESHOLD} then 1 else 0 end) as "bounces",
${bounceQuery}
sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime",
${parseFieldsByName(fields)}
from (
@@ -60,18 +80,10 @@ async function relationalQuery(
website_event.visit_id,
count(*) as "c",
min(website_event.created_at) as "min_time",
max(website_event.created_at) as "max_time",
max((
select count(*)
from website_event we2
where we2.website_id = website_event.website_id
and we2.session_id = website_event.session_id
and we2.visit_id = website_event.visit_id
and we2.created_at between {{startDate}} and {{endDate}}
and we2.event_type = ${EVENT_TYPE.customEvent}
)) as "events_count"
max(website_event.created_at) as "max_time"
from website_event
${cohortQuery}
${excludeBounceQuery}
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
@@ -79,6 +91,7 @@ async function relationalQuery(
group by ${parseFieldsByName(fields)},
website_event.session_id, website_event.visit_id
) as t
${visitEventsJoin}
group by ${parseFieldsByName(fields)}
order by 2 desc, 1 desc
limit 500
@@ -94,13 +107,27 @@ async function clickhouseQuery(
): Promise<BreakdownData[]> {
const { parseFilters, rawQuery } = clickhouse;
const { startDate, endDate, fields } = parameters;
const { filterQuery, cohortQuery, queryParams } = parseFilters({
const { filterQuery, cohortQuery, excludeBounceQuery, queryParams } = parseFilters({
...filters,
websiteId,
startDate,
endDate,
eventType: EVENT_TYPE.pageView,
});
const needsBounceEvents = filters.excludeBounce !== true;
const bounceQuery = needsBounceEvents
? `sumIf(1, t.c = 1 and ifNull(e.has_custom_event, 0) = 0) as "bounces",`
: '0 as "bounces",';
const visitEventsJoin = needsBounceEvents
? `left join (
select session_id, visit_id, toUInt8(1) as has_custom_event
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = ${EVENT_TYPE.customEvent}
group by session_id, visit_id
) as e using (session_id, visit_id)`
: '';
return rawQuery(
`
@@ -108,7 +135,7 @@ async function clickhouseQuery(
sum(t.c) as "views",
count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits",
sumIf(1, t.c = 1 and ifNull(e.events_count, 0) < ${BOUNCE_THRESHOLD}) as "bounces",
${bounceQuery}
sum(max_time-min_time) as "totaltime",
${parseFieldsByName(fields)}
from (
@@ -121,20 +148,14 @@ async function clickhouseQuery(
max(created_at) max_time
from website_event
${cohortQuery}
${excludeBounceQuery}
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
group by ${parseFieldsByName(fields)},
session_id, visit_id
) as t
left join (
select session_id, visit_id, toUInt32(count()) as events_count
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = ${EVENT_TYPE.customEvent}
group by session_id, visit_id
) as e using (session_id, visit_id)
${visitEventsJoin}
group by ${parseFieldsByName(fields)}
order by 2 desc, 1 desc
limit 500