Fix excludeBounce query, simplify bounce definition for SPA websites, query routing for performance, add tests
This commit is contained in:
1 parent
0a80d09d0b
commit
b3aec54bb3
12 files changed
+410
-150
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import clickhouse from '@/lib/clickhouse';
|
||||
import {
|
||||
BOUNCE_THRESHOLD,
|
||||
EMAIL_DOMAINS,
|
||||
EVENT_TYPE,
|
||||
LLM_DOMAINS,
|
||||
@@ -49,6 +48,29 @@ async function relationalQuery(
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
const needsBounceEvents = filters.excludeBounce !== true;
|
||||
const bounceQuery = needsBounceEvents
|
||||
? `sum(case when visit_stats.c = 1 and coalesce(visit_events.has_custom_event, 0) = 0 then 1 else 0 end) as "bounces",`
|
||||
: '0 as "bounces",';
|
||||
const visitEventsQuery = needsBounceEvents
|
||||
? `,
|
||||
|
||||
visit_events as (
|
||||
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 session_id, visit_id)`
|
||||
: '';
|
||||
const visitEventsJoin = needsBounceEvents
|
||||
? `left join visit_events
|
||||
on visit_events.session_id = visit_stats.session_id
|
||||
and visit_events.visit_id = visit_stats.visit_id`
|
||||
: '';
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
@@ -122,33 +144,21 @@ async function relationalQuery(
|
||||
min(created_at) as min_time,
|
||||
max(created_at) as max_time
|
||||
from prefix
|
||||
group by session_id, visit_id),
|
||||
|
||||
visit_events as (
|
||||
select
|
||||
session_id,
|
||||
visit_id,
|
||||
count(*) as events_count
|
||||
from website_event
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and created_at between {{startDate}} and {{endDate}}
|
||||
and event_type = ${EVENT_TYPE.customEvent}
|
||||
group by session_id, visit_id)
|
||||
${visitEventsQuery}
|
||||
|
||||
select
|
||||
visit_channels.name,
|
||||
sum(visit_stats.c) as "pageviews",
|
||||
count(distinct visit_stats.session_id) as "visitors",
|
||||
count(distinct visit_stats.visit_id) as "visits",
|
||||
sum(case when visit_stats.c = 1 and coalesce(visit_events.events_count, 0) < ${BOUNCE_THRESHOLD} then 1 else 0 end) as "bounces",
|
||||
${bounceQuery}
|
||||
sum(${getTimestampDiffSQL('visit_stats.min_time', 'visit_stats.max_time')}) as "totaltime"
|
||||
from visit_stats
|
||||
join visit_channels
|
||||
on visit_channels.session_id = visit_stats.session_id
|
||||
and visit_channels.visit_id = visit_stats.visit_id
|
||||
left join visit_events
|
||||
on visit_events.session_id = visit_stats.session_id
|
||||
and visit_events.visit_id = visit_stats.visit_id
|
||||
${visitEventsJoin}
|
||||
group by visit_channels.name
|
||||
order by visitors desc, visits desc
|
||||
`,
|
||||
@@ -166,6 +176,20 @@ async function clickhouseQuery(
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
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(
|
||||
`
|
||||
@@ -174,7 +198,7 @@ async function clickhouseQuery(
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(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"
|
||||
from (
|
||||
select
|
||||
@@ -229,14 +253,7 @@ async function clickhouseQuery(
|
||||
)
|
||||
group by 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 name
|
||||
order by visitors desc, visits desc;
|
||||
`,
|
||||
|
||||
Reference in new issue
Block a user