mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
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:
parent
6e9ffd9c4b
commit
e93636e1d8
4 changed files with 22 additions and 17 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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';
|
||||||
|
|
|
||||||
|
|
@ -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}
|
||||||
|
|
|
||||||
|
|
@ -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}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue