feat(queries): add BOUNCE_THRESHOLD and enhance bounce calculation logic

Updated bounce calculations to account for visits with custom events
(`EVENT_TYPE.customEvent`). A visit counts as a bounce only when its
custom event count is below `BOUNCE_THRESHOLD`, so engaged single
pageview visits are no longer misreported.

Since #3614 was opened v3 moved `getInsights` into `getBreakdown` and
copied the old formula into the channel, pageview and session
breakdowns, so those are updated too.
This commit is contained in:
Federico D'Eredità committed 2026-07-26 18:49:59 +02:00
1 parent 19fcde2c9c
commit cb035fdc1a
6 files changed
+157 -37

No files matched your search

@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
import { BOUNCE_THRESHOLD, 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';
@@ -62,7 +62,7 @@ async function relationalQuery(
sum(t.c) as "pageviews",
count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits",
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
sum(case when t.c = 1 and t.events_count < ${BOUNCE_THRESHOLD} then 1 else 0 end) as "bounces",
sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime"
from (
select
@@ -72,11 +72,20 @@ 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(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"
from website_event
${cohortQuery}
${excludeBounceQuery}
${joinSessionQuery}
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type NOT IN (2, 5)
@@ -122,7 +131,7 @@ async function clickhouseQuery(
sum(t.c) as "pageviews",
uniq(t.session_id) as "visitors",
uniq(t.visit_id) as "visits",
sum(if(t.c = 1, 1, 0)) as "bounces",
sumIf(1, t.c = 1 and ifNull(e.events_count, 0) < ${BOUNCE_THRESHOLD}) as "bounces",
sum(max_time-min_time) as "totaltime"
from (
select
@@ -144,7 +153,15 @@ async function clickhouseQuery(
group by name, session_id, visit_id
${includeCountry ? ', country' : ''}
) as t
group by name
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)
group by name
${includeCountry ? ', country' : ''}
order by visitors desc, visits desc
limit ${limit}