This commit is contained in:
Mike Cao 2025-08-15 10:08:41 -07:00
commit 0558563d35
11 changed files with 59 additions and 56 deletions

View file

@ -57,7 +57,7 @@ CREATE TABLE umami.event_data
event_name String, event_name String,
data_key String, data_key String,
string_value Nullable(String), string_value Nullable(String),
number_value Nullable(Decimal64(22, 4)), number_value Nullable(Decimal(22, 4)),
date_value Nullable(DateTime('UTC')), date_value Nullable(DateTime('UTC')),
data_type UInt32, data_type UInt32,
created_at DateTime('UTC'), created_at DateTime('UTC'),
@ -73,7 +73,7 @@ CREATE TABLE umami.session_data
session_id UUID, session_id UUID,
data_key String, data_key String,
string_value Nullable(String), string_value Nullable(String),
number_value Nullable(Decimal64(22, 4)), number_value Nullable(Decimal(22, 4)),
date_value Nullable(DateTime('UTC')), date_value Nullable(DateTime('UTC')),
data_type UInt32, data_type UInt32,
distinct_id String, distinct_id String,

View file

@ -1,12 +1,10 @@
import { Grid, Column, Heading, Text } from '@umami/react-zen';
import { firstBy } from 'thenby';
import { CHART_COLORS, UTM_PARAMS } from '@/lib/constants';
import { useResultQuery } from '@/components/hooks';
import { PieChart } from '@/components/charts/PieChart'; import { PieChart } from '@/components/charts/PieChart';
import { ListTable } from '@/components/metrics/ListTable';
import { useMessages } from '@/components/hooks';
import { Panel } from '@/components/common/Panel';
import { LoadingPanel } from '@/components/common/LoadingPanel'; import { LoadingPanel } from '@/components/common/LoadingPanel';
import { Panel } from '@/components/common/Panel';
import { useMessages, useResultQuery } from '@/components/hooks';
import { ListTable } from '@/components/metrics/ListTable';
import { CHART_COLORS, UTM_PARAMS } from '@/lib/constants';
import { Column, Grid, Heading, Text } from '@umami/react-zen';
export interface UTMProps { export interface UTMProps {
websiteId: string; websiteId: string;
@ -27,19 +25,19 @@ export function UTM({ websiteId, startDate, endDate }: UTMProps) {
{data && ( {data && (
<Column gap> <Column gap>
{UTM_PARAMS.map(param => { {UTM_PARAMS.map(param => {
const items = toArray(data?.[param]); const items = data?.[param];
const chartData = { const chartData = {
labels: items.map(({ name }) => name), labels: items.map(({ utm }) => utm),
datasets: [ datasets: [
{ {
data: items.map(({ value }) => value), data: items.map(({ views }) => views),
backgroundColor: CHART_COLORS, backgroundColor: CHART_COLORS,
borderWidth: 0, borderWidth: 0,
}, },
], ],
}; };
const total = items.reduce((sum, { value }) => { const total = items.reduce((sum, { views }) => {
return +sum + +value; return +sum + +views;
}, 0); }, 0);
return ( return (
@ -51,10 +49,10 @@ export function UTM({ websiteId, startDate, endDate }: UTMProps) {
</Heading> </Heading>
<ListTable <ListTable
metric={formatMessage(labels.views)} metric={formatMessage(labels.views)}
data={items.map(({ name, value }) => ({ data={items.map(({ utm, views }) => ({
x: name, x: utm,
y: value, y: views,
z: (value / total) * 100, z: (views / total) * 100,
}))} }))}
/> />
</Column> </Column>
@ -70,11 +68,3 @@ export function UTM({ websiteId, startDate, endDate }: UTMProps) {
</LoadingPanel> </LoadingPanel>
); );
} }
function toArray(data: Record<string, number> = {}) {
return Object.keys(data)
.map(key => {
return { name: key, value: data[key] };
})
.sort(firstBy('value', -1));
}

View file

@ -10,7 +10,7 @@ export function WebsiteLayout({ websiteId, children }: { websiteId: string; chil
return ( return (
<WebsiteProvider websiteId={websiteId}> <WebsiteProvider websiteId={websiteId}>
<Grid columns="auto 1fr" width="100%" height="100%"> <Grid columns="auto 1fr" width="100%" height="100%">
<Column height="100%" border="right" backgroundColor> <Column height="100%" border="right" backgroundColor marginRight="2">
<WebsiteNav websiteId={websiteId} /> <WebsiteNav websiteId={websiteId} />
</Column> </Column>
<PageBody gap> <PageBody gap>

View file

@ -36,7 +36,7 @@ export function EventProperties({ websiteId }: { websiteId: string }) {
gap="6" gap="6"
> >
{data && ( {data && (
<Grid columns="repeat(auto-fill, minmax(300px, 1fr))" gap> <Grid columns="repeat(auto-fill, minmax(300px, 1fr))" marginBottom="3" gap>
<Select <Select
label={formatMessage(labels.event)} label={formatMessage(labels.event)}
value={eventName} value={eventName}

View file

@ -3,6 +3,7 @@ import { unauthorized, json } from '@/lib/response';
import { getQueryFilters, parseRequest, setWebsiteDate } from '@/lib/request'; import { getQueryFilters, parseRequest, setWebsiteDate } from '@/lib/request';
import { getUTM, UTMParameters } from '@/queries'; import { getUTM, UTMParameters } from '@/queries';
import { reportResultSchema } from '@/lib/schema'; import { reportResultSchema } from '@/lib/schema';
import { UTM_PARAMS } from '@/lib/constants';
export async function POST(request: Request) { export async function POST(request: Request) {
const { auth, body, error } = await parseRequest(request, reportResultSchema); const { auth, body, error } = await parseRequest(request, reportResultSchema);
@ -20,7 +21,17 @@ export async function POST(request: Request) {
const filters = await getQueryFilters(body.filters, websiteId); const filters = await getQueryFilters(body.filters, websiteId);
const parameters = await setWebsiteDate(websiteId, body.parameters); const parameters = await setWebsiteDate(websiteId, body.parameters);
const data = await getUTM(websiteId, parameters as UTMParameters, filters); const data = {
utm_source: [],
utm_medium: [],
utm_campaign: [],
utm_term: [],
utm_content: [],
};
for (const key of UTM_PARAMS) {
data[key] = await getUTM(websiteId, { column: key, ...parameters } as UTMParameters, filters);
}
return json(data); return json(data);
} }

View file

@ -62,7 +62,7 @@ export async function GET(
} }
if (type === 'channel') { if (type === 'channel') {
const data = await getChannelExpandedMetrics(websiteId, { limit, offset }, filters); const data = await getChannelExpandedMetrics(websiteId, filters);
return json(data); return json(data);
} }

View file

@ -58,7 +58,7 @@ async function relationalQuery(
limit ${limit} limit ${limit}
offset ${offset} offset ${offset}
`, `,
queryParams, { ...queryParams, ...parameters },
); );
} }
@ -89,6 +89,6 @@ async function clickhouseQuery(
limit ${limit} limit ${limit}
offset ${offset} offset ${offset}
`, `,
queryParams, { ...queryParams, ...parameters },
); );
} }

View file

@ -27,7 +27,7 @@ export interface ChannelExpandedMetricsData {
} }
export async function getChannelExpandedMetrics( export async function getChannelExpandedMetrics(
...args: [websiteId: string, parameters: ChannelExpandedMetricsParameters, filters?: QueryFilters] ...args: [websiteId: string, filters?: QueryFilters]
): Promise<ChannelExpandedMetricsData[]> { ): Promise<ChannelExpandedMetricsData[]> {
return runQuery({ return runQuery({
[PRISMA]: () => relationalQuery(...args), [PRISMA]: () => relationalQuery(...args),
@ -37,7 +37,6 @@ export async function getChannelExpandedMetrics(
async function relationalQuery( async function relationalQuery(
websiteId: string, websiteId: string,
parameters: ChannelExpandedMetricsParameters,
filters: QueryFilters, filters: QueryFilters,
): Promise<ChannelExpandedMetricsData[]> { ): Promise<ChannelExpandedMetricsData[]> {
const { rawQuery, parseFilters } = prisma; const { rawQuery, parseFilters } = prisma;
@ -79,16 +78,14 @@ async function relationalQuery(
group by x group by x
order by y desc; order by y desc;
`, `,
{ ...queryParams, ...parameters }, queryParams,
); );
} }
async function clickhouseQuery( async function clickhouseQuery(
websiteId: string, websiteId: string,
parameters: ChannelExpandedMetricsParameters,
filters: QueryFilters, filters: QueryFilters,
): Promise<ChannelExpandedMetricsData[]> { ): Promise<ChannelExpandedMetricsData[]> {
const { limit = 500, offset = 0 } = parameters;
const { rawQuery, parseFilters } = clickhouse; const { rawQuery, parseFilters } = clickhouse;
const { queryParams, filterQuery, cohortQuery } = parseFilters({ const { queryParams, filterQuery, cohortQuery } = parseFilters({
...filters, ...filters,
@ -145,11 +142,9 @@ async function clickhouseQuery(
group by prefix, name, session_id, visit_id group by prefix, name, session_id, visit_id
) as t ) as t
group by name group by name
order by visitors desc, visits desc order by visitors desc, visits desc;
limit ${limit}
offset ${offset}
`, `,
{ ...queryParams, ...parameters }, queryParams,
); );
} }

View file

@ -86,7 +86,7 @@ async function relationalQuery(
limit ${limit} limit ${limit}
offset ${offset} offset ${offset}
`, `,
queryParams, { ...queryParams, ...parameters },
); );
} }
@ -184,5 +184,5 @@ async function clickhouseQuery(
`; `;
} }
return rawQuery(sql, queryParams); return rawQuery(sql, { ...queryParams, ...parameters });
} }

View file

@ -1,9 +1,11 @@
import clickhouse from '@/lib/clickhouse'; import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma'; import prisma from '@/lib/prisma';
import { QueryFilters } from '@/lib/types'; import { QueryFilters } from '@/lib/types';
export interface UTMParameters { export interface UTMParameters {
column: string;
startDate: Date; startDate: Date;
endDate: Date; endDate: Date;
} }
@ -22,26 +24,28 @@ async function relationalQuery(
parameters: UTMParameters, parameters: UTMParameters,
filters: QueryFilters, filters: QueryFilters,
) { ) {
const { startDate, endDate } = parameters; const { column, startDate, endDate } = parameters;
const { parseFilters, rawQuery } = prisma; const { parseFilters, rawQuery } = prisma;
const { filterQuery, queryParams } = parseFilters({ const { filterQuery, cohortQuery, queryParams } = parseFilters({
...filters, ...filters,
websiteId, websiteId,
startDate, startDate,
endDate, endDate,
eventType: EVENT_TYPE.pageView,
}); });
return rawQuery( return rawQuery(
` `
select url_query, count(*) as "num" select ${column} utm, count(*) as views
from website_event from website_event
${cohortQuery}
where website_id = {{websiteId::uuid}} where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and created_at between {{startDate}} and {{endDate}}
and coalesce(url_query, '') != '' and coalesce(${column}, '') != ''
and event_type = 1
${filterQuery} ${filterQuery}
group by 1 group by 1
order by 2 desc
`, `,
queryParams, queryParams,
); );
@ -52,25 +56,27 @@ async function clickhouseQuery(
parameters: UTMParameters, parameters: UTMParameters,
filters: QueryFilters, filters: QueryFilters,
) { ) {
const { startDate, endDate } = parameters; const { column, startDate, endDate } = parameters;
const { parseFilters, rawQuery } = clickhouse; const { parseFilters, rawQuery } = clickhouse;
const { filterQuery, queryParams } = parseFilters({ const { filterQuery, cohortQuery, queryParams } = parseFilters({
...filters, ...filters,
websiteId, websiteId,
startDate, startDate,
endDate, endDate,
eventType: EVENT_TYPE.pageView,
}); });
return rawQuery( return rawQuery(
` `
select url_query, count(*) as "num" select ${column} utm, count(*) as views
from website_event from website_event
${cohortQuery}
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and url_query != '' and ${column} != ''
and event_type = 1
${filterQuery} ${filterQuery}
group by 1 group by 1
order by 2 desc
`, `,
queryParams, queryParams,
); );

View file

@ -81,6 +81,7 @@ async function clickhouseQuery(
${getDateSQL('website_event.created_at', unit, timezone)} as t, ${getDateSQL('website_event.created_at', unit, timezone)} as t,
uniq(session_id) as y uniq(session_id) as y
from website_event_stats_hourly as website_event from website_event_stats_hourly as website_event
${cohortQuery}
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery} ${filterQuery}