umami/src/queries/analytics/getWebsiteStats.ts

142 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-08-26 05:04:32 +00:00
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
2023-08-04 20:18:30 +00:00
import { QueryFilters } from 'lib/types';
import { EVENT_COLUMNS } from 'lib/constants';
2022-07-12 21:14:36 +00:00
export async function getWebsiteStats(
...args: [websiteId: string, filters: QueryFilters]
): Promise<
2024-03-27 18:17:00 +00:00
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> {
2022-08-28 04:38:35 +00:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
2022-07-25 16:47:11 +00:00
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-07-21 04:31:26 +00:00
});
}
async function relationalQuery(
websiteId: string,
filters: QueryFilters,
): Promise<
2024-03-27 18:17:00 +00:00
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> {
const { getTimestampDiffSQL, parseFilters, rawQuery } = prisma;
2023-08-04 20:18:30 +00:00
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
...filters,
});
2022-07-12 21:14:36 +00:00
return rawQuery(
2023-07-25 06:06:16 +00:00
`
select
2024-03-28 00:07:03 +00:00
sum(t.c) as "pageviews",
count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits",
count(distinct t.country) as "countries",
2024-08-08 05:39:36 +00:00
count(t.event_name) as "countries",
sum(case when t.event_type = 2 then 1 else 0 end) as "events",
2023-07-25 06:06:16 +00:00
sum(case when t.c = 1 then 1 else 0 end) as "bounces",
sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime",
2023-07-25 06:06:16 +00:00
from (
select
website_event.session_id,
website_event.visit_id,
2024-08-08 05:39:36 +00:00
website_event.event_type,
session.country,
2023-11-02 17:34:06 +00:00
count(*) as "c",
min(website_event.created_at) as "min_time",
max(website_event.created_at) as "max_time"
2023-07-25 06:06:16 +00:00
from website_event
${joinSession}
2024-04-04 05:48:57 +00:00
where website_event.website_id = {{websiteId::uuid}}
2023-07-25 06:06:16 +00:00
and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery}
2024-08-08 05:39:36 +00:00
group by 1, 2, 3, 4
2023-07-25 06:06:16 +00:00
) as t
`,
2023-08-04 20:18:30 +00:00
params,
2022-07-12 21:14:36 +00:00
);
}
2022-07-21 04:31:26 +00:00
2023-09-29 18:00:06 +00:00
async function clickhouseQuery(
websiteId: string,
filters: QueryFilters,
): Promise<
2024-03-27 18:17:00 +00:00
{ pageviews: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> {
const { rawQuery, parseFilters } = clickhouse;
2023-08-04 20:18:30 +00:00
const { filterQuery, params } = await parseFilters(websiteId, {
...filters,
});
2022-07-21 04:31:26 +00:00
let sql = '';
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
sql = `
select
sum(t.c) as "pageviews",
2024-08-02 05:57:54 +00:00
uniq(t.session_id) as "visitors",
uniq(t.visit_id) as "visits",
uniq(t.country) as "countries",
2024-08-08 05:39:36 +00:00
sumIf(1, t.event_type = 2) as "events",
sumIf(1, t.c = 1) as "bounces",
sum(max_time-min_time) as "totaltime"
from (
select
session_id,
visit_id,
2024-08-08 05:39:36 +00:00
event_type,
country,
count(*) c,
min(created_at) min_time,
max(created_at) max_time
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
2024-08-08 05:39:36 +00:00
group by session_id, visit_id, event_type, country
) as t;
`;
} else {
sql = `
select
2024-08-02 05:57:54 +00:00
sum(t.c) as "pageviews",
uniq(session_id) as "visitors",
uniq(visit_id) as "visits",
uniq(country) as "countries",
2024-08-08 05:39:36 +00:00
sumIf(1, t.event_type = 2) as "events",
2024-08-02 05:57:54 +00:00
sumIf(1, t.c = 1) as "bounces",
sum(max_time-min_time) as "totaltime"
from (
select
session_id,
visit_id,
2024-08-08 05:39:36 +00:00
event_type,
country,
sum(views) c,
min(min_time) min_time,
max(max_time) max_time
from umami.website_event_stats_hourly "website_event"
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
2024-08-08 05:39:36 +00:00
group by session_id, visit_id, event_type, country
2024-08-02 05:57:54 +00:00
) as t;
`;
}
return rawQuery(sql, params).then(result => {
return Object.values(result).map((a: any) => {
2023-09-29 18:00:06 +00:00
return {
pageviews: Number(a.pageviews),
visitors: Number(a.visitors),
visits: Number(a.visits),
bounces: Number(a.bounces),
totaltime: Number(a.totaltime),
countries: Number(a.countries),
2024-08-08 05:39:36 +00:00
events: Number(a.events),
2023-09-29 18:00:06 +00:00
};
});
});
2022-07-21 04:31:26 +00:00
}