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:
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 { EVENT_COLUMNS } from '@/lib/constants';
|
||||
import { BOUNCE_THRESHOLD, EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
@@ -35,7 +35,9 @@ async function relationalQuery(
|
||||
});
|
||||
|
||||
const { excludeBounce } = filters;
|
||||
const bounceQuery = excludeBounce ? '0' : 'coalesce(sum(case when t.c = 1 then 1 else 0 end), 0)';
|
||||
const bounceQuery = excludeBounce
|
||||
? '0'
|
||||
: `coalesce(sum(case when t.c = 1 and t.events_count < ${BOUNCE_THRESHOLD} then 1 else 0 end), 0)`;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
@@ -51,11 +53,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)
|
||||
@@ -81,7 +92,9 @@ async function clickhouseQuery(
|
||||
|
||||
let sql = '';
|
||||
const { excludeBounce } = filters;
|
||||
const bounceQuery = excludeBounce ? '0' : 'sumIf(1, t.c = 1)';
|
||||
const bounceQuery = excludeBounce
|
||||
? '0'
|
||||
: `sumIf(1, t.c = 1 and ifNull(e.events_count, 0) < ${BOUNCE_THRESHOLD})`;
|
||||
|
||||
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||
sql = `
|
||||
@@ -106,7 +119,15 @@ async function clickhouseQuery(
|
||||
and event_type NOT IN (2, 5)
|
||||
${filterQuery}
|
||||
group by session_id, visit_id
|
||||
) as t;
|
||||
) 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);
|
||||
`;
|
||||
} else {
|
||||
sql = `
|
||||
@@ -130,7 +151,14 @@ async function clickhouseQuery(
|
||||
and event_type NOT IN (2, 5)
|
||||
${filterQuery}
|
||||
group by session_id, visit_id
|
||||
) as t;
|
||||
) as t
|
||||
left join (
|
||||
select session_id, visit_id, toUInt32(sumIf(views, event_type = ${EVENT_TYPE.customEvent})) as events_count
|
||||
from website_event_stats_hourly
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
group by session_id, visit_id
|
||||
) as e using (session_id, visit_id);
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user