mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
implement sessions metric expanded queries
This commit is contained in:
parent
c8b4ee8ca5
commit
c1cad16cb9
10 changed files with 352 additions and 13 deletions
|
|
@ -28,6 +28,7 @@ export * from '@/queries/sql/sessions/getSessionData';
|
|||
export * from '@/queries/sql/sessions/getSessionDataProperties';
|
||||
export * from '@/queries/sql/sessions/getSessionDataValues';
|
||||
export * from '@/queries/sql/sessions/getSessionMetrics';
|
||||
export * from '@/queries/sql/sessions/getSessionExpandedMetrics';
|
||||
export * from '@/queries/sql/sessions/getWebsiteSessions';
|
||||
export * from '@/queries/sql/sessions/getWebsiteSessionStats';
|
||||
export * from '@/queries/sql/sessions/getWebsiteSessionsWeekly';
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ async function relationalQuery(
|
|||
else string_value
|
||||
end as "value",
|
||||
count(distinct session_data.session_id) as "total"
|
||||
from website_event e
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
join session_data d
|
||||
join session_data
|
||||
on session_data.session_id = website_event.session_id
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
|
|
@ -58,9 +58,9 @@ async function clickhouseQuery(
|
|||
data_type = 4, toString(date_trunc('hour', date_value)),
|
||||
string_value) as "value",
|
||||
uniq(session_data.session_id) as "total"
|
||||
from website_event e
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
join session_data d final
|
||||
join session_data final
|
||||
on session_data.session_id = website_event.session_id
|
||||
where website_event.website_id = {websiteId:UUID}
|
||||
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
|
|
|
|||
126
src/queries/sql/sessions/getSessionExpandedMetrics.ts
Normal file
126
src/queries/sql/sessions/getSessionExpandedMetrics.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export interface SessionExpandedMetricsParameters {
|
||||
type: string;
|
||||
limit?: number | string;
|
||||
offset?: number | string;
|
||||
}
|
||||
|
||||
export interface SessionExpandedMetricsData {
|
||||
label: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
bounces: number;
|
||||
totaltime: number;
|
||||
}
|
||||
|
||||
export async function getSessionExpandedMetrics(
|
||||
...args: [websiteId: string, parameters: SessionExpandedMetricsParameters, filters: QueryFilters]
|
||||
): Promise<SessionExpandedMetricsData[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
parameters: SessionExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<SessionExpandedMetricsData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
},
|
||||
{
|
||||
joinSession: SESSION_COLUMNS.includes(type),
|
||||
},
|
||||
);
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
${column} x,
|
||||
count(distinct website_event.session_id) y
|
||||
${includeCountry ? ', country' : ''}
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and website_event.event_type = {{eventType}}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
${includeCountry ? ', 3' : ''}
|
||||
order by 2 desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
parameters: SessionExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<SessionExpandedMetricsData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
});
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
label,
|
||||
${includeCountry ? 'country,' : ''}
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(t.visit_id) as "visits",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select
|
||||
${column} label,
|
||||
${includeCountry ? 'country,' : ''}
|
||||
session_id,
|
||||
visit_id,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
and label != ''
|
||||
${filterQuery}
|
||||
group by label, session_id, visit_id
|
||||
${includeCountry ? ', country' : ''}
|
||||
) as t
|
||||
group by label
|
||||
${includeCountry ? ', country' : ''}
|
||||
order by visitors desc, visits desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue