clean-up session property string charts and insights.
This commit is contained in:
1 parent
1f2bd7cad9
commit
d5c48ee75f
15 files changed
+381
-260
No files matched your search
@@ -1,133 +0,0 @@
|
||||
'use client';
|
||||
import { PieChart } from '@/components/charts/PieChart';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { useMessages, useMobile } from '@/components/hooks';
|
||||
import { ListTable } from '@/components/metrics/ListTable';
|
||||
import { CHART_COLORS } from '@/lib/constants';
|
||||
import { formatLongNumber, formatShortTime } from '@/lib/format';
|
||||
import type { PropertyLeaderboardRow } from '@/lib/types';
|
||||
import { Column, DataColumn, DataTable, Grid, Row, Text } from '@umami/react-zen';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
export function HighCardinalityPropertyChart({ rows }: { rows: PropertyLeaderboardRow[] }) {
|
||||
const { t, labels } = useMessages();
|
||||
const { isPhone } = useMobile();
|
||||
const activeTable = useMemo(() => rows.filter(row => row.activity > 0).slice(0, 10), [rows]);
|
||||
const userMix = useMemo(() => {
|
||||
const totalNew = rows.reduce((sum, row) => sum + row.newSessions, 0);
|
||||
const totalReturning = rows.reduce((sum, row) => sum + row.returningSessions, 0);
|
||||
const total = totalNew + totalReturning;
|
||||
|
||||
return {
|
||||
newCount: totalNew,
|
||||
returningCount: totalReturning,
|
||||
newRate: total ? (totalNew / total) * 100 : 0,
|
||||
returningRate: total ? (totalReturning / total) * 100 : 0,
|
||||
};
|
||||
}, [rows]);
|
||||
const sessionTypeTable = useMemo(() => {
|
||||
return [
|
||||
{
|
||||
label: 'New',
|
||||
count: userMix.newCount,
|
||||
percent: userMix.newRate,
|
||||
},
|
||||
{
|
||||
label: 'Returning',
|
||||
count: userMix.returningCount,
|
||||
percent: userMix.returningRate,
|
||||
},
|
||||
].filter(row => row.count > 0);
|
||||
}, [userMix]);
|
||||
const sessionTypeChartData = useMemo(() => {
|
||||
if (!sessionTypeTable.length) return null;
|
||||
|
||||
return {
|
||||
labels: sessionTypeTable.map(row => row.label),
|
||||
datasets: [
|
||||
{
|
||||
data: sessionTypeTable.map(row => row.count),
|
||||
backgroundColor: [CHART_COLORS[0], CHART_COLORS[1]],
|
||||
borderWidth: 0,
|
||||
},
|
||||
],
|
||||
};
|
||||
}, [sessionTypeTable]);
|
||||
|
||||
return (
|
||||
<Column gap="6">
|
||||
<Column minHeight="400px">
|
||||
<Grid columns="1fr" gap padding="2" alignItems="start">
|
||||
<Column gap padding="2">
|
||||
{activeTable.length === 0 ? (
|
||||
<Empty />
|
||||
) : isPhone ? (
|
||||
<Column minWidth="0" width="100%">
|
||||
<DataTable data={activeTable} style={{ width: '100%' }}>
|
||||
<DataColumn
|
||||
id="label"
|
||||
label="Most Active"
|
||||
width="1fr"
|
||||
align="start"
|
||||
>
|
||||
{row => (
|
||||
<Row overflow="hidden" minWidth="0" width="100%">
|
||||
<Text truncate style={{ maxWidth: '100%', width: '100%' }}>
|
||||
{row.label}
|
||||
</Text>
|
||||
</Row>
|
||||
)}
|
||||
</DataColumn>
|
||||
</DataTable>
|
||||
</Column>
|
||||
) : (
|
||||
<Column overflow="auto" minHeight="0">
|
||||
<Column style={{ minWidth: '700px' }}>
|
||||
<DataTable data={activeTable}>
|
||||
<DataColumn
|
||||
id="label"
|
||||
label="Most Active"
|
||||
width="minmax(220px, 2fr)"
|
||||
align="start"
|
||||
>
|
||||
{row => (
|
||||
<Row overflow="hidden">
|
||||
<Text truncate style={{ maxWidth: '320px' }}>
|
||||
{row.label}
|
||||
</Text>
|
||||
</Row>
|
||||
)}
|
||||
</DataColumn>
|
||||
<DataColumn id="sessions" label="Visitors" align="end" width="90px">
|
||||
{row => formatLongNumber(row.sessions)}
|
||||
</DataColumn>
|
||||
<DataColumn id="visits" label="Visits" align="end" width="90px">
|
||||
{row => formatLongNumber(row.visits)}
|
||||
</DataColumn>
|
||||
<DataColumn id="views" label="Views" align="end" width="90px">
|
||||
{row => formatLongNumber(row.views)}
|
||||
</DataColumn>
|
||||
<DataColumn id="events" label="Events" align="end" width="90px">
|
||||
{row => formatLongNumber(row.events)}
|
||||
</DataColumn>
|
||||
<DataColumn id="duration" label="Duration" align="end" width="120px">
|
||||
{row =>
|
||||
formatShortTime(Math.abs(~~Number(row.totaltime)), ['h', 'm', 's'], ' ')
|
||||
}
|
||||
</DataColumn>
|
||||
</DataTable>
|
||||
</Column>
|
||||
</Column>
|
||||
)}
|
||||
</Column>
|
||||
</Grid>
|
||||
</Column>
|
||||
<Column minHeight="300px">
|
||||
<Grid columns={{ base: '1fr', md: '1fr 1fr' }} gap padding="2" alignItems="start">
|
||||
<ListTable title="Users" metric={t(labels.count)} data={sessionTypeTable} />
|
||||
{sessionTypeChartData && <PieChart type="doughnut" chartData={sessionTypeChartData} />}
|
||||
</Grid>
|
||||
</Column>
|
||||
</Column>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
'use client';
|
||||
import { DistributionBarChart } from '@/components/charts/DistributionBarChart';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||
import { useMessages, useMobile, useSessionDataActivityStatsQuery } from '@/components/hooks';
|
||||
import { formatLongNumber, formatShortTime } from '@/lib/format';
|
||||
import type { PropertyFilter } from '@/lib/types';
|
||||
import { Column, DataColumn, DataTable, Grid, Row, Text } from '@umami/react-zen';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
export function SessionPropertyChart({
|
||||
websiteId,
|
||||
propertyName,
|
||||
propertyFilters = [],
|
||||
}: {
|
||||
websiteId: string;
|
||||
propertyName: string;
|
||||
propertyFilters?: PropertyFilter[];
|
||||
}) {
|
||||
const statsQuery = useSessionDataActivityStatsQuery(websiteId, propertyName, propertyFilters);
|
||||
const rows = statsQuery.data ?? [];
|
||||
const { t, labels } = useMessages();
|
||||
const { isPhone } = useMobile();
|
||||
const activeTable = useMemo(() => rows.filter(row => row.activity > 0).slice(0, 10), [rows]);
|
||||
const chartData = useMemo(
|
||||
() =>
|
||||
activeTable.map(row => ({
|
||||
label: row.label,
|
||||
values: {
|
||||
sessions: row.sessions,
|
||||
visits: row.visits,
|
||||
views: row.views,
|
||||
events: row.events,
|
||||
},
|
||||
})),
|
||||
[activeTable],
|
||||
);
|
||||
|
||||
return (
|
||||
<LoadingPanel isLoading={statsQuery.isLoading} error={statsQuery.error} minHeight="300px">
|
||||
<Column gap="6">
|
||||
<Column minHeight="320px">
|
||||
{chartData.length ? (
|
||||
<Column gap="3">
|
||||
<Text weight="bold">{t(labels.activityByProperty)}</Text>
|
||||
<DistributionBarChart
|
||||
labels={chartData.map(row => row.label)}
|
||||
datasets={[
|
||||
{
|
||||
label: t(labels.visitors),
|
||||
values: chartData.map(row => row.values.sessions),
|
||||
backgroundColor: 'rgba(59, 130, 246, 0.80)',
|
||||
},
|
||||
{
|
||||
label: t(labels.visits),
|
||||
values: chartData.map(row => row.values.visits),
|
||||
backgroundColor: 'rgba(16, 185, 129, 0.80)',
|
||||
},
|
||||
{
|
||||
label: t(labels.views),
|
||||
values: chartData.map(row => row.values.views),
|
||||
backgroundColor: 'rgba(245, 158, 11, 0.80)',
|
||||
},
|
||||
{
|
||||
label: t(labels.events),
|
||||
values: chartData.map(row => row.values.events),
|
||||
backgroundColor: 'rgba(244, 114, 182, 0.80)',
|
||||
},
|
||||
]}
|
||||
stacked={true}
|
||||
/>
|
||||
</Column>
|
||||
) : (
|
||||
<Empty />
|
||||
)}
|
||||
</Column>
|
||||
<Column minHeight="400px">
|
||||
<Grid columns="1fr" gap padding="2" alignItems="start">
|
||||
<Column gap padding="2">
|
||||
{activeTable.length === 0 ? (
|
||||
<Empty />
|
||||
) : isPhone ? (
|
||||
<Column minWidth="0" width="100%">
|
||||
<DataTable data={activeTable} style={{ width: '100%' }}>
|
||||
<DataColumn id="label" label={propertyName} width="1fr" align="start">
|
||||
{row => (
|
||||
<Row overflow="hidden" minWidth="0" width="100%">
|
||||
<Text truncate style={{ maxWidth: '100%', width: '100%' }}>
|
||||
{row.label}
|
||||
</Text>
|
||||
</Row>
|
||||
)}
|
||||
</DataColumn>
|
||||
</DataTable>
|
||||
</Column>
|
||||
) : (
|
||||
<Column overflow="auto" minHeight="0">
|
||||
<Column style={{ minWidth: '700px' }}>
|
||||
<DataTable data={activeTable}>
|
||||
<DataColumn id="label" label={propertyName} width="minmax(220px, 2fr)" align="start">
|
||||
{row => (
|
||||
<Row overflow="hidden">
|
||||
<Text truncate style={{ maxWidth: '320px' }}>
|
||||
{row.label}
|
||||
</Text>
|
||||
</Row>
|
||||
)}
|
||||
</DataColumn>
|
||||
<DataColumn id="sessions" label={t(labels.visitors)} align="end" width="90px">
|
||||
{row => formatLongNumber(row.sessions)}
|
||||
</DataColumn>
|
||||
<DataColumn id="visits" label={t(labels.visits)} align="end" width="90px">
|
||||
{row => formatLongNumber(row.visits)}
|
||||
</DataColumn>
|
||||
<DataColumn id="views" label={t(labels.views)} align="end" width="90px">
|
||||
{row => formatLongNumber(row.views)}
|
||||
</DataColumn>
|
||||
<DataColumn id="events" label={t(labels.events)} align="end" width="90px">
|
||||
{row => formatLongNumber(row.events)}
|
||||
</DataColumn>
|
||||
<DataColumn id="duration" label={t(labels.duration)} align="end" width="120px">
|
||||
{row =>
|
||||
formatShortTime(Math.abs(~~Number(row.totaltime)), ['h', 'm', 's'], ' ')
|
||||
}
|
||||
</DataColumn>
|
||||
</DataTable>
|
||||
</Column>
|
||||
</Column>
|
||||
)}
|
||||
</Column>
|
||||
</Grid>
|
||||
</Column>
|
||||
</Column>
|
||||
</LoadingPanel>
|
||||
);
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
'use client';
|
||||
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||
import { useSessionDataStatsQuery } from '@/components/hooks';
|
||||
import type { PropertyFilter } from '@/lib/types';
|
||||
import { PropertyChart } from '@/components/property-data/PropertyChart';
|
||||
import { HighCardinalityPropertyChart } from './HighCardinalityPropertyChart';
|
||||
|
||||
const HIGH_CARDINALITY_NAME_PATTERN =
|
||||
/(email|e-mail|name|(^|_)(id|uuid|token)($|_)|distinct_id|user_id|customer_id|client_id|member_id)/i;
|
||||
|
||||
function isHighCardinalityProperty(propertyName: string, totalValues: number, uniqueValues: number) {
|
||||
if (!totalValues) return false;
|
||||
|
||||
const uniquenessRate = uniqueValues / totalValues;
|
||||
const hasHighCardinalityName = HIGH_CARDINALITY_NAME_PATTERN.test(propertyName);
|
||||
|
||||
return totalValues >= 20 && (hasHighCardinalityName || uniqueValues >= 20 || uniquenessRate >= 0.6);
|
||||
}
|
||||
|
||||
export function SessionStringPropertyView({
|
||||
websiteId,
|
||||
propertyName,
|
||||
propertyFilters = [],
|
||||
}: {
|
||||
websiteId: string;
|
||||
propertyName: string;
|
||||
propertyFilters?: PropertyFilter[];
|
||||
}) {
|
||||
const statsQuery = useSessionDataStatsQuery(websiteId, propertyName, propertyFilters);
|
||||
const rows = statsQuery.data ?? [];
|
||||
const totalValues = rows.reduce((sum, row) => sum + row.sessions, 0);
|
||||
const uniqueValues = rows.length;
|
||||
const isHighCardinality = isHighCardinalityProperty(
|
||||
propertyName,
|
||||
totalValues,
|
||||
uniqueValues,
|
||||
);
|
||||
|
||||
return (
|
||||
<LoadingPanel isLoading={statsQuery.isLoading} error={statsQuery.error} minHeight="300px">
|
||||
{isHighCardinality ? (
|
||||
<HighCardinalityPropertyChart rows={rows} />
|
||||
) : (
|
||||
<PropertyChart
|
||||
source="session"
|
||||
websiteId={websiteId}
|
||||
propertyName={propertyName}
|
||||
propertyFilters={propertyFilters}
|
||||
/>
|
||||
)}
|
||||
</LoadingPanel>
|
||||
);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import { PropertyFilterButton } from '@/components/property-data/PropertyFilterB
|
||||
import { PropertyNumericChart } from '@/components/property-data/PropertyNumericChart';
|
||||
import { Panel } from '@/components/common/Panel';
|
||||
import { SessionDataPivotTable } from '../session-data/SessionDataPivotTable';
|
||||
import { SessionStringPropertyView } from '../session-data/SessionStringPropertyView';
|
||||
import { SessionPropertyChart } from '../session-data/SessionPropertyChart';
|
||||
|
||||
export function SessionProperties({ websiteId }: { websiteId: string }) {
|
||||
const [propertyName, setPropertyName] = useState('');
|
||||
@@ -129,7 +129,7 @@ export function SessionProperties({ websiteId }: { websiteId: string }) {
|
||||
/>
|
||||
)}
|
||||
{propertyName && selectedProperty?.dataType === DATA_TYPE.string && (
|
||||
<SessionStringPropertyView
|
||||
<SessionPropertyChart
|
||||
websiteId={websiteId}
|
||||
propertyName={propertyName}
|
||||
propertyFilters={propertyFilters}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||
import { json, unauthorized } from '@/lib/response';
|
||||
import { filterParams, timezoneParam, unitParam } from '@/lib/schema';
|
||||
import { canViewWebsite } from '@/permissions';
|
||||
import { getSessionDataStats } from '@/queries/sql/sessions/getSessionDataStats';
|
||||
import { getSessionDataActivityStats } from '@/queries/sql/sessions/getSessionDataActivityStats';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
@@ -34,7 +34,7 @@ export async function GET(
|
||||
const { propertyName, ...rest } = query;
|
||||
const filters = await getQueryFilters(rest, websiteId);
|
||||
const propertyFilters = parsePropertyFilters(query);
|
||||
const result = await getSessionDataStats(websiteId, propertyName, filters, propertyFilters);
|
||||
const result = await getSessionDataActivityStats(websiteId, propertyName, filters, propertyFilters);
|
||||
|
||||
return json(result);
|
||||
}
|
||||
Reference in new issue
Block a user