update terminology, add metric cards to new metrics

This commit is contained in:
Francis Cao 2024-03-26 11:15:39 -07:00
parent 51139c1918
commit dcf2457e45
3 changed files with 45 additions and 30 deletions

View File

@ -21,11 +21,12 @@ export function WebsiteMetricsBar({
const { ref, isSticky } = useSticky({ enabled: sticky }); const { ref, isSticky } = useSticky({ enabled: sticky });
const { data, isLoading, isFetched, error } = useWebsiteStats(websiteId); const { data, isLoading, isFetched, error } = useWebsiteStats(websiteId);
const { pageviews, uniques, bounces, totaltime } = data || {}; const { views, visitors, visits, bounces, totaltime } = data || {};
const num = Math.min(data && uniques.value, data && bounces.value); const num = Math.min(data && visitors.value, data && bounces.value);
const diffs = data && { const diffs = data && {
pageviews: pageviews.value - pageviews.change, views: views.value - views.change,
uniques: uniques.value - uniques.change, visitors: visitors.value - visitors.change,
visits: visits.value - visits.change,
bounces: bounces.value - bounces.change, bounces: bounces.value - bounces.change,
totaltime: totaltime.value - totaltime.change, totaltime: totaltime.value - totaltime.change,
}; };
@ -39,25 +40,39 @@ export function WebsiteMetricsBar({
})} })}
> >
<MetricsBar isLoading={isLoading} isFetched={isFetched} error={error}> <MetricsBar isLoading={isLoading} isFetched={isFetched} error={error}>
{pageviews && uniques && ( {views && visitors && (
<> <>
<MetricCard <MetricCard
label={formatMessage(labels.views)} label={formatMessage(labels.views)}
value={pageviews.value} value={views.value}
change={pageviews.change} change={views.change}
/> />
<MetricCard <MetricCard
label={formatMessage(labels.visitors)} label={formatMessage(labels.visitors)}
value={uniques.value} value={visitors.value}
change={uniques.change} change={visitors.change}
/>
<MetricCard
label={formatMessage(labels.visits)}
value={visits.value}
change={visits.change}
/>
<MetricCard
label={formatMessage(labels.viewsPerVisit)}
value={visits.value ? views.value / visits.value : 0}
change={
visits.value && visits.change
? views.value / visits.value - diffs.views / diffs.visits
: 0
}
/> />
<MetricCard <MetricCard
label={formatMessage(labels.bounceRate)} label={formatMessage(labels.bounceRate)}
value={uniques.value ? (num / uniques.value) * 100 : 0} value={visitors.value ? (num / visitors.value) * 100 : 0}
change={ change={
uniques.value && uniques.change visitors.value && visitors.change
? (num / uniques.value) * 100 - ? (num / visitors.value) * 100 -
(Math.min(diffs.uniques, diffs.bounces) / diffs.uniques) * 100 || 0 (Math.min(diffs.visitors, diffs.bounces) / diffs.visitors) * 100 || 0
: 0 : 0
} }
format={n => Number(n).toFixed(0) + '%'} format={n => Number(n).toFixed(0) + '%'}
@ -66,14 +81,12 @@ export function WebsiteMetricsBar({
<MetricCard <MetricCard
label={formatMessage(labels.averageVisitTime)} label={formatMessage(labels.averageVisitTime)}
value={ value={
totaltime.value && pageviews.value totaltime.value && views.value ? totaltime.value / (views.value - bounces.value) : 0
? totaltime.value / (pageviews.value - bounces.value)
: 0
} }
change={ change={
totaltime.value && pageviews.value totaltime.value && views.value
? (diffs.totaltime / (diffs.pageviews - diffs.bounces) - ? (diffs.totaltime / (diffs.views - diffs.bounces) -
totaltime.value / (pageviews.value - bounces.value)) * totaltime.value / (views.value - bounces.value)) *
-1 || 0 -1 || 0
: 0 : 0
} }

View File

@ -100,6 +100,7 @@ export const labels = defineMessages({
queryParameters: { id: 'label.query-parameters', defaultMessage: 'Query parameters' }, queryParameters: { id: 'label.query-parameters', defaultMessage: 'Query parameters' },
back: { id: 'label.back', defaultMessage: 'Back' }, back: { id: 'label.back', defaultMessage: 'Back' },
visitors: { id: 'label.visitors', defaultMessage: 'Visitors' }, visitors: { id: 'label.visitors', defaultMessage: 'Visitors' },
visits: { id: 'label.visits', defaultMessage: 'Visits' },
filterCombined: { id: 'label.filter-combined', defaultMessage: 'Combined' }, filterCombined: { id: 'label.filter-combined', defaultMessage: 'Combined' },
filterRaw: { id: 'label.filter-raw', defaultMessage: 'Raw' }, filterRaw: { id: 'label.filter-raw', defaultMessage: 'Raw' },
views: { id: 'label.views', defaultMessage: 'Views' }, views: { id: 'label.views', defaultMessage: 'Views' },
@ -127,6 +128,7 @@ export const labels = defineMessages({
pageViews: { id: 'label.page-views', defaultMessage: 'Page views' }, pageViews: { id: 'label.page-views', defaultMessage: 'Page views' },
uniqueVisitors: { id: 'label.unique-visitors', defaultMessage: 'Unique visitors' }, uniqueVisitors: { id: 'label.unique-visitors', defaultMessage: 'Unique visitors' },
bounceRate: { id: 'label.bounce-rate', defaultMessage: 'Bounce rate' }, bounceRate: { id: 'label.bounce-rate', defaultMessage: 'Bounce rate' },
viewsPerVisit: { id: 'label.views-per-visit', defaultMessage: 'Views per visit' },
averageVisitTime: { id: 'label.average-visit-time', defaultMessage: 'Average visit time' }, averageVisitTime: { id: 'label.average-visit-time', defaultMessage: 'Average visit time' },
desktop: { id: 'label.desktop', defaultMessage: 'Desktop' }, desktop: { id: 'label.desktop', defaultMessage: 'Desktop' },
laptop: { id: 'label.laptop', defaultMessage: 'Laptop' }, laptop: { id: 'label.laptop', defaultMessage: 'Laptop' },

View File

@ -7,7 +7,7 @@ import { QueryFilters } from 'lib/types';
export async function getWebsiteStats( export async function getWebsiteStats(
...args: [websiteId: string, filters: QueryFilters] ...args: [websiteId: string, filters: QueryFilters]
): Promise< ): Promise<
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[] { views: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> { > {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
@ -19,7 +19,7 @@ async function relationalQuery(
websiteId: string, websiteId: string,
filters: QueryFilters, filters: QueryFilters,
): Promise< ): Promise<
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[] { views: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> { > {
const { getTimestampDiffQuery, parseFilters, rawQuery } = prisma; const { getTimestampDiffQuery, parseFilters, rawQuery } = prisma;
const { filterQuery, joinSession, params } = await parseFilters(websiteId, { const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
@ -30,9 +30,9 @@ async function relationalQuery(
return rawQuery( return rawQuery(
` `
select select
sum(t.c) as "pageviews", sum(t.c) as "views",
count(distinct t.session_id) as "uniques", count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visitors", count(distinct t.visit_id) as "visits",
sum(case when t.c = 1 then 1 else 0 end) as "bounces", sum(case when t.c = 1 then 1 else 0 end) as "bounces",
sum(${getTimestampDiffQuery('t.min_time', 't.max_time')}) as "totaltime" sum(${getTimestampDiffQuery('t.min_time', 't.max_time')}) as "totaltime"
from ( from (
@ -61,7 +61,7 @@ async function clickhouseQuery(
websiteId: string, websiteId: string,
filters: QueryFilters, filters: QueryFilters,
): Promise< ): Promise<
{ pageviews: number; uniques: number; visitors: number; bounces: number; totaltime: number }[] { views: number; visitors: number; visits: number; bounces: number; totaltime: number }[]
> { > {
const { rawQuery, parseFilters } = clickhouse; const { rawQuery, parseFilters } = clickhouse;
const { filterQuery, params } = await parseFilters(websiteId, { const { filterQuery, params } = await parseFilters(websiteId, {
@ -72,9 +72,9 @@ async function clickhouseQuery(
return rawQuery( return rawQuery(
` `
select select
sum(t.c) as "pageviews", sum(t.c) as "views",
count(distinct t.session_id) as "uniques", count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visitors", count(distinct t.visit_id) as "visits",
sum(if(t.c = 1, 1, 0)) as "bounces", sum(if(t.c = 1, 1, 0)) as "bounces",
sum(max_time-min_time) as "totaltime" sum(max_time-min_time) as "totaltime"
from ( from (
@ -96,9 +96,9 @@ async function clickhouseQuery(
).then(a => { ).then(a => {
return Object.values(a).map(a => { return Object.values(a).map(a => {
return { return {
pageviews: Number(a.pageviews), views: Number(a.views),
uniques: Number(a.uniques),
visitors: Number(a.visitors), visitors: Number(a.visitors),
visits: Number(a.visits),
bounces: Number(a.bounces), bounces: Number(a.bounces),
totaltime: Number(a.totaltime), totaltime: Number(a.totaltime),
}; };