Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
fix(queries): count custom events correctly on the hourly rollup, trying to address greptile findings
Federico D'Eredità92e9b276d2a0
diff --git a/src/queries/sql/events/getEventExpandedMetrics.ts b/src/queries/sql/events/getEventExpandedMetrics.tsindex 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 nameorder by visitors desc, visits desclimit ${limit}offset ${offset}diff --git a/src/queries/sql/getWebsiteStats.ts b/src/queries/sql/getWebsiteStats.tsindex 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 tleft 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_countfrom website_event_stats_hourlywhere website_id = {websiteId:UUID}and created_at between {startDate:DateTime64} and {endDate:DateTime64}