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

Introduced `BOUNCE_THRESHOLD` constant for configurable bounce definitions. Updated bounce calculation logic in `getWebsiteStats` and `getInsights` to use `events_count`.
This commit is contained in:
Federico D'Eredità 2025-09-09 09:13:57 +02:00
parent 6e9ffd9c4b
commit e93636e1d8
No known key found for this signature in database
GPG key ID: 87AF255298D68522
4 changed files with 22 additions and 17 deletions

View file

@ -8,6 +8,7 @@ services:
DATABASE_URL: postgresql://umami:umami@db:5432/umami DATABASE_URL: postgresql://umami:umami@db:5432/umami
DATABASE_TYPE: postgresql DATABASE_TYPE: postgresql
APP_SECRET: replace-me-with-a-random-string APP_SECRET: replace-me-with-a-random-string
# BOUNCE_THRESHOLD: 2 # OPTIONAL: set the minimum amount custom events to trigger a bounce
depends_on: depends_on:
db: db:
condition: service_healthy condition: service_healthy

View file

@ -1,4 +1,8 @@
export const CURRENT_VERSION = process.env.currentVersion; export const CURRENT_VERSION = process.env.currentVersion;
export const BOUNCE_THRESHOLD = Math.max(
1,
Number.parseInt(process.env.BOUNCE_THRESHOLD || '1', 10) || 1,
);
export const AUTH_TOKEN = 'umami.auth'; export const AUTH_TOKEN = 'umami.auth';
export const LOCALE_CONFIG = 'umami.locale'; export const LOCALE_CONFIG = 'umami.locale';
export const TIMEZONE_CONFIG = 'umami.timezone'; export const TIMEZONE_CONFIG = 'umami.timezone';

View file

@ -2,7 +2,7 @@ import clickhouse from '@/lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma'; import prisma from '@/lib/prisma';
import { QueryFilters } from '@/lib/types'; import { QueryFilters } from '@/lib/types';
import { EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants'; import { BOUNCE_THRESHOLD, EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
export async function getWebsiteStats( export async function getWebsiteStats(
...args: [websiteId: string, filters: QueryFilters] ...args: [websiteId: string, filters: QueryFilters]
@ -33,7 +33,7 @@ async function relationalQuery(
sum(t.c) as "pageviews", sum(t.c) as "pageviews",
count(distinct t.session_id) as "visitors", count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits", count(distinct t.visit_id) as "visits",
sum(case when t.c = 1 and t.has_event = 0 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" sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime"
from ( from (
select select
@ -42,14 +42,14 @@ async function relationalQuery(
sum(case when website_event.event_type = ${EVENT_TYPE.pageView} then 1 else 0 end) as "c", sum(case when website_event.event_type = ${EVENT_TYPE.pageView} then 1 else 0 end) as "c",
min(website_event.created_at) as "min_time", 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(case when exists ( max((
select 1 select count(*)
from website_event we2 from website_event we2
where we2.website_id = website_event.website_id where we2.website_id = website_event.website_id
and we2.session_id = website_event.session_id and we2.session_id = website_event.session_id
and we2.created_at between {{startDate}} and {{endDate}} and we2.created_at between {{startDate}} and {{endDate}}
and we2.event_type = ${EVENT_TYPE.customEvent} and we2.event_type = ${EVENT_TYPE.customEvent}
) then 1 else 0 end) as "has_event" )) as "events_count"
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSession} ${joinSession}
@ -83,7 +83,7 @@ async function clickhouseQuery(
sum(t.c) as "pageviews", sum(t.c) as "pageviews",
uniq(t.session_id) as "visitors", uniq(t.session_id) as "visitors",
uniq(t.visit_id) as "visits", uniq(t.visit_id) as "visits",
sumIf(1, t.c = 1 and ifNull(e.has_event, 0) = 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" sum(max_time-min_time) as "totaltime"
from ( from (
select select
@ -100,7 +100,7 @@ async function clickhouseQuery(
group by session_id, visit_id group by session_id, visit_id
) as t ) as t
left join ( left join (
select session_id, toUInt8(count() > 0) as has_event select session_id, toUInt32(count()) as events_count
from website_event from website_event
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}
@ -114,7 +114,7 @@ async function clickhouseQuery(
sum(t.c) as "pageviews", sum(t.c) as "pageviews",
uniq(session_id) as "visitors", uniq(session_id) as "visitors",
uniq(visit_id) as "visits", uniq(visit_id) as "visits",
sumIf(1, t.c = 1 and ifNull(e.has_event, 0) = 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" sum(max_time-min_time) as "totaltime"
from (select from (select
session_id, session_id,
@ -130,7 +130,7 @@ async function clickhouseQuery(
group by session_id, visit_id group by session_id, visit_id
) as t ) as t
left join ( left join (
select session_id, toUInt8(sumIf(views, event_type = ${EVENT_TYPE.customEvent}) > 0) as has_event select session_id, toUInt32(sumIf(views, event_type = ${EVENT_TYPE.customEvent})) as events_count
from website_event_stats_hourly from website_event_stats_hourly
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}

View file

@ -1,7 +1,7 @@
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma'; import prisma from '@/lib/prisma';
import clickhouse from '@/lib/clickhouse'; import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants'; import { BOUNCE_THRESHOLD, EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
import { QueryFilters } from '@/lib/types'; import { QueryFilters } from '@/lib/types';
export async function getInsights( export async function getInsights(
@ -41,7 +41,7 @@ async function relationalQuery(
sum(t.c) as "views", sum(t.c) as "views",
count(distinct t.session_id) as "visitors", count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits", count(distinct t.visit_id) as "visits",
sum(case when t.c = 1 and t.has_events = 0 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", sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime",
${parseFieldsByName(fields)} ${parseFieldsByName(fields)}
from ( from (
@ -52,14 +52,14 @@ async function relationalQuery(
sum(case when website_event.event_type = ${EVENT_TYPE.pageView} then 1 else 0 end) as "c", sum(case when website_event.event_type = ${EVENT_TYPE.pageView} then 1 else 0 end) as "c",
min(website_event.created_at) as "min_time", 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(case when exists ( max((
select 1 select count(*)
from website_event we2 from website_event we2
where we2.website_id = website_event.website_id where we2.website_id = website_event.website_id
and we2.session_id = website_event.session_id and we2.session_id = website_event.session_id
and we2.created_at between {{startDate}} and {{endDate}} and we2.created_at between {{startDate}} and {{endDate}}
and we2.event_type = ${EVENT_TYPE.customEvent} and we2.event_type = ${EVENT_TYPE.customEvent}
) then 1 else 0 end) as "has_events" )) as "events_count"
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSession} ${joinSession}
@ -98,7 +98,7 @@ async function clickhouseQuery(
sum(t.c) as "views", sum(t.c) as "views",
count(distinct t.session_id) as "visitors", count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits", count(distinct t.visit_id) as "visits",
sumIf(1, t.c = 1 and ifNull(e.has_event, 0) = 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", sum(max_time-min_time) as "totaltime",
${parseFieldsByName(fields)} ${parseFieldsByName(fields)}
from ( from (
@ -118,7 +118,7 @@ async function clickhouseQuery(
session_id, visit_id session_id, visit_id
) as t ) as t
left join ( left join (
select session_id, toUInt8(count() > 0) as has_event select session_id, toUInt32(count()) as events_count
from website_event from website_event
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}