implement visit duration

This commit is contained in:
Francis Cao 2024-08-13 08:45:52 -07:00
parent fc758745dc
commit f9c7129a3e
2 changed files with 34 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import { useMessages } from 'components/hooks';
import MetricCard from 'components/metrics/MetricCard';
import MetricsBar from 'components/metrics/MetricsBar';
import { formatShortTime } from 'lib/format';
export function SessionStats({ data }) {
const { formatMessage, labels } = useMessages();
@ -10,6 +11,11 @@ export function SessionStats({ data }) {
<MetricCard label={formatMessage(labels.visits)} value={data?.visits} />
<MetricCard label={formatMessage(labels.views)} value={data?.views} />
<MetricCard label={formatMessage(labels.events)} value={data?.events} />
<MetricCard
label={formatMessage(labels.visitDuration)}
value={data?.totaltime / data?.visits}
formatValue={n => `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`}
/>
</MetricsBar>
);
}

View File

@ -23,9 +23,8 @@ async function clickhouseQuery(websiteId: string, sessionId: string) {
return rawQuery(
`
select
session_id as id,
website_id as websiteId,
select id,
websiteId,
hostname,
browser,
os,
@ -37,13 +36,32 @@ async function clickhouseQuery(websiteId: string, sessionId: string) {
city,
min(min_time) as firstAt,
max(max_time) as lastAt,
uniq(visit_id) as visits,
sumIf(views, event_type = 1) as views,
length(groupArrayArray(event_name)) as events
from website_event_stats_hourly
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
group by session_id, website_id, hostname, browser, os, device, screen, language, country, subdivision1, city;
uniq(visit_id) visits,
sum(views) as views,
sum(events) as events,
sum(max_time-min_time) as totaltime
from (select
session_id as id,
visit_id,
website_id as websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
min(min_time) as min_time,
max(max_time) as max_time,
sum(views) as views,
length(groupArrayArray(event_name)) as events
from website_event_stats_hourly
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
group by session_id, visit_id, website_id, hostname, browser, os, device, screen, language, country, subdivision1, city) t
group by id, websiteId, hostname, browser, os, device, screen, language, country, subdivision1, city;
`,
{ websiteId, sessionId },
).then(result => result?.[0]);