From 92e9b276d2a0b379aa43a7a2d78e4d74f8c3dce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20D=27Eredit=C3=A0?= Date: Sun, 26 Jul 2026 18:36:48 +0200 Subject: [PATCH] fix(queries): count custom events correctly on the hourly rollup, trying to address greptile findings `website_event_stats_hourly` sets `views` to `sumIf(1, event_type NOT IN (2, 5))`, so `views` is 0 on custom event rows and the `events_count` join was summing zeroes. Switched it to `sum(length(event_name))`, which is how `getWebsiteSessionStats` already counts custom events on that table. Also dropped the `getEventExpandedMetrics` change from the previous commit. That report is filtered to custom events, so every visit it lists has already fired at least one, and its `bounces` column counts visits where the event fired exactly once rather than visits with a single pageview. The threshold rule does not apply there, so the query is left untouched. --- .../sql/events/getEventExpandedMetrics.ts | 29 ++++--------------- src/queries/sql/getWebsiteStats.ts | 2 +- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/queries/sql/events/getEventExpandedMetrics.ts b/src/queries/sql/events/getEventExpandedMetrics.ts index 0e4840a11..86bda8509 100644 --- a/src/queries/sql/events/getEventExpandedMetrics.ts +++ b/src/queries/sql/events/getEventExpandedMetrics.ts @@ -1,5 +1,5 @@ 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'; @@ -54,7 +54,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 and (t.events_count - t.c) < ${BOUNCE_THRESHOLD} then 1 else 0 end) as "bounces", + sum(case when t.c = 1 then 1 else 0 end) as "bounces", sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime" from ( select @@ -63,19 +63,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} - ${joinSessionQuery} + ${joinSessionQuery} where website_event.website_id = {{websiteId::uuid}} and website_event.created_at between {{startDate}} and {{endDate}} ${filterQuery} @@ -113,7 +104,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 (toInt64(ifNull(e.events_count, 0)) - toInt64(t.c)) < ${BOUNCE_THRESHOLD}) as "bounces", + sum(if(t.c = 1, 1, 0)) as "bounces", sum(max_time-min_time) as "totaltime" from ( select @@ -131,15 +122,7 @@ async function clickhouseQuery( ${filterQuery} group by name, 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) - group by name + group by name order by visitors desc, visits desc limit ${limit} offset ${offset} diff --git a/src/queries/sql/getWebsiteStats.ts b/src/queries/sql/getWebsiteStats.ts index 0f74bc373..6ca085db4 100644 --- a/src/queries/sql/getWebsiteStats.ts +++ b/src/queries/sql/getWebsiteStats.ts @@ -153,7 +153,7 @@ async function clickhouseQuery( group by session_id, visit_id ) as t left join ( - select session_id, visit_id, toUInt32(sumIf(views, event_type = ${EVENT_TYPE.customEvent})) as events_count + select session_id, visit_id, toUInt32(sum(length(event_name))) as events_count from website_event_stats_hourly where website_id = {websiteId:UUID} and created_at between {startDate:DateTime64} and {endDate:DateTime64}