mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Compare commits
5 commits
1498da2d02
...
a37de757a0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a37de757a0 | ||
|
|
f9f9125532 | ||
|
|
2f998ff9d8 | ||
|
|
57eef5866b | ||
|
|
a1a092dc19 |
14 changed files with 309 additions and 40 deletions
|
|
@ -30,7 +30,11 @@ export function UserEditForm({ userId, onSave }: { userId: string; onSave?: () =
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form onSubmit={handleSubmit} error={getMessage(error?.code)} values={user}>
|
<Form
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
error={getMessage(error?.code)}
|
||||||
|
values={{ ...user, password: '' }}
|
||||||
|
>
|
||||||
<FormField name="username" label={formatMessage(labels.username)}>
|
<FormField name="username" label={formatMessage(labels.username)}>
|
||||||
<TextField data-test="input-username" />
|
<TextField data-test="input-username" />
|
||||||
</FormField>
|
</FormField>
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ import { DataGrid } from '@/components/common/DataGrid';
|
||||||
import { useLinksQuery, useNavigation } from '@/components/hooks';
|
import { useLinksQuery, useNavigation } from '@/components/hooks';
|
||||||
import { LinksTable } from './LinksTable';
|
import { LinksTable } from './LinksTable';
|
||||||
|
|
||||||
export function LinksDataTable() {
|
export function LinksDataTable({ showActions = false }: { showActions?: boolean }) {
|
||||||
const { teamId } = useNavigation();
|
const { teamId } = useNavigation();
|
||||||
const query = useLinksQuery({ teamId });
|
const query = useLinksQuery({ teamId });
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataGrid query={query} allowSearch={true} autoFocus={false} allowPaging={true}>
|
<DataGrid query={query} allowSearch={true} autoFocus={false} allowPaging={true}>
|
||||||
{({ data }) => <LinksTable data={data} />}
|
{({ data }) => <LinksTable data={data} showActions={showActions} />}
|
||||||
</DataGrid>
|
</DataGrid>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,30 @@ import { LinksDataTable } from '@/app/(main)/links/LinksDataTable';
|
||||||
import { PageBody } from '@/components/common/PageBody';
|
import { PageBody } from '@/components/common/PageBody';
|
||||||
import { PageHeader } from '@/components/common/PageHeader';
|
import { PageHeader } from '@/components/common/PageHeader';
|
||||||
import { Panel } from '@/components/common/Panel';
|
import { Panel } from '@/components/common/Panel';
|
||||||
import { useMessages, useNavigation } from '@/components/hooks';
|
import { useLoginQuery, useMessages, useNavigation, useTeamMembersQuery } from '@/components/hooks';
|
||||||
|
import { ROLES } from '@/lib/constants';
|
||||||
import { LinkAddButton } from './LinkAddButton';
|
import { LinkAddButton } from './LinkAddButton';
|
||||||
|
|
||||||
export function LinksPage() {
|
export function LinksPage() {
|
||||||
|
const { user } = useLoginQuery();
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { teamId } = useNavigation();
|
const { teamId } = useNavigation();
|
||||||
|
const { data } = useTeamMembersQuery(teamId);
|
||||||
|
|
||||||
|
const showActions =
|
||||||
|
(teamId &&
|
||||||
|
data?.data.filter(team => team.userId === user.id && team.role !== ROLES.teamViewOnly)
|
||||||
|
.length > 0) ||
|
||||||
|
(!teamId && user.role !== ROLES.viewOnly);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<Column gap="6" margin="2">
|
<Column gap="6" margin="2">
|
||||||
<PageHeader title={formatMessage(labels.links)}>
|
<PageHeader title={formatMessage(labels.links)}>
|
||||||
<LinkAddButton teamId={teamId} />
|
{showActions && <LinkAddButton teamId={teamId} />}
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
<Panel>
|
<Panel>
|
||||||
<LinksDataTable />
|
<LinksDataTable showActions={showActions} />
|
||||||
</Panel>
|
</Panel>
|
||||||
</Column>
|
</Column>
|
||||||
</PageBody>
|
</PageBody>
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,11 @@ import { useMessages, useNavigation, useSlug } from '@/components/hooks';
|
||||||
import { LinkDeleteButton } from './LinkDeleteButton';
|
import { LinkDeleteButton } from './LinkDeleteButton';
|
||||||
import { LinkEditButton } from './LinkEditButton';
|
import { LinkEditButton } from './LinkEditButton';
|
||||||
|
|
||||||
export function LinksTable(props: DataTableProps) {
|
export interface LinksTableProps extends DataTableProps {
|
||||||
|
showActions?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function LinksTable({ showActions, ...props }: LinksTableProps) {
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { websiteId, renderUrl } = useNavigation();
|
const { websiteId, renderUrl } = useNavigation();
|
||||||
const { getSlugUrl } = useSlug('link');
|
const { getSlugUrl } = useSlug('link');
|
||||||
|
|
@ -36,6 +40,7 @@ export function LinksTable(props: DataTableProps) {
|
||||||
<DataColumn id="created" label={formatMessage(labels.created)} width="200px">
|
<DataColumn id="created" label={formatMessage(labels.created)} width="200px">
|
||||||
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
|
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
|
||||||
</DataColumn>
|
</DataColumn>
|
||||||
|
{showActions && (
|
||||||
<DataColumn id="action" align="end" width="100px">
|
<DataColumn id="action" align="end" width="100px">
|
||||||
{({ id, name }: any) => {
|
{({ id, name }: any) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -46,6 +51,7 @@ export function LinksTable(props: DataTableProps) {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
</DataColumn>
|
</DataColumn>
|
||||||
|
)}
|
||||||
</DataTable>
|
</DataTable>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,13 @@ import { DataGrid } from '@/components/common/DataGrid';
|
||||||
import { useNavigation, usePixelsQuery } from '@/components/hooks';
|
import { useNavigation, usePixelsQuery } from '@/components/hooks';
|
||||||
import { PixelsTable } from './PixelsTable';
|
import { PixelsTable } from './PixelsTable';
|
||||||
|
|
||||||
export function PixelsDataTable() {
|
export function PixelsDataTable({ showActions = false }: { showActions?: boolean }) {
|
||||||
const { teamId } = useNavigation();
|
const { teamId } = useNavigation();
|
||||||
const query = usePixelsQuery({ teamId });
|
const query = usePixelsQuery({ teamId });
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataGrid query={query} allowSearch={true} autoFocus={false} allowPaging={true}>
|
<DataGrid query={query} allowSearch={true} autoFocus={false} allowPaging={true}>
|
||||||
{({ data }) => <PixelsTable data={data} />}
|
{({ data }) => <PixelsTable data={data} showActions={showActions} />}
|
||||||
</DataGrid>
|
</DataGrid>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,31 @@ import { Column } from '@umami/react-zen';
|
||||||
import { PageBody } from '@/components/common/PageBody';
|
import { PageBody } from '@/components/common/PageBody';
|
||||||
import { PageHeader } from '@/components/common/PageHeader';
|
import { PageHeader } from '@/components/common/PageHeader';
|
||||||
import { Panel } from '@/components/common/Panel';
|
import { Panel } from '@/components/common/Panel';
|
||||||
import { useMessages, useNavigation } from '@/components/hooks';
|
import { useLoginQuery, useMessages, useNavigation, useTeamMembersQuery } from '@/components/hooks';
|
||||||
|
import { ROLES } from '@/lib/constants';
|
||||||
import { PixelAddButton } from './PixelAddButton';
|
import { PixelAddButton } from './PixelAddButton';
|
||||||
import { PixelsDataTable } from './PixelsDataTable';
|
import { PixelsDataTable } from './PixelsDataTable';
|
||||||
|
|
||||||
export function PixelsPage() {
|
export function PixelsPage() {
|
||||||
|
const { user } = useLoginQuery();
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { teamId } = useNavigation();
|
const { teamId } = useNavigation();
|
||||||
|
const { data } = useTeamMembersQuery(teamId);
|
||||||
|
|
||||||
|
const showActions =
|
||||||
|
(teamId &&
|
||||||
|
data?.data.filter(team => team.userId === user.id && team.role !== ROLES.teamViewOnly)
|
||||||
|
.length > 0) ||
|
||||||
|
(!teamId && user.role !== ROLES.viewOnly);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<Column gap="6" margin="2">
|
<Column gap="6" margin="2">
|
||||||
<PageHeader title={formatMessage(labels.pixels)}>
|
<PageHeader title={formatMessage(labels.pixels)}>
|
||||||
<PixelAddButton teamId={teamId} />
|
{showActions && <PixelAddButton teamId={teamId} />}
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
<Panel>
|
<Panel>
|
||||||
<PixelsDataTable />
|
<PixelsDataTable showActions={showActions} />
|
||||||
</Panel>
|
</Panel>
|
||||||
</Column>
|
</Column>
|
||||||
</PageBody>
|
</PageBody>
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,15 @@ import { useMessages, useNavigation, useSlug } from '@/components/hooks';
|
||||||
import { PixelDeleteButton } from './PixelDeleteButton';
|
import { PixelDeleteButton } from './PixelDeleteButton';
|
||||||
import { PixelEditButton } from './PixelEditButton';
|
import { PixelEditButton } from './PixelEditButton';
|
||||||
|
|
||||||
export function PixelsTable(props: DataTableProps) {
|
export interface PixelsTableProps extends DataTableProps {
|
||||||
|
showActions?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PixelsTable({ showActions, ...props }: PixelsTableProps) {
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { renderUrl } = useNavigation();
|
const { renderUrl } = useNavigation();
|
||||||
const { getSlugUrl } = useSlug('pixel');
|
const { getSlugUrl } = useSlug('pixel');
|
||||||
|
console.log(showActions);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataTable {...props}>
|
<DataTable {...props}>
|
||||||
|
|
@ -31,6 +36,7 @@ export function PixelsTable(props: DataTableProps) {
|
||||||
<DataColumn id="created" label={formatMessage(labels.created)}>
|
<DataColumn id="created" label={formatMessage(labels.created)}>
|
||||||
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
|
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
|
||||||
</DataColumn>
|
</DataColumn>
|
||||||
|
{showActions && (
|
||||||
<DataColumn id="action" align="end" width="100px">
|
<DataColumn id="action" align="end" width="100px">
|
||||||
{(row: any) => {
|
{(row: any) => {
|
||||||
const { id, name } = row;
|
const { id, name } = row;
|
||||||
|
|
@ -43,6 +49,7 @@ export function PixelsTable(props: DataTableProps) {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
</DataColumn>
|
</DataColumn>
|
||||||
|
)}
|
||||||
</DataTable>
|
</DataTable>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,31 @@ import { Column } from '@umami/react-zen';
|
||||||
import { PageBody } from '@/components/common/PageBody';
|
import { PageBody } from '@/components/common/PageBody';
|
||||||
import { PageHeader } from '@/components/common/PageHeader';
|
import { PageHeader } from '@/components/common/PageHeader';
|
||||||
import { Panel } from '@/components/common/Panel';
|
import { Panel } from '@/components/common/Panel';
|
||||||
import { useMessages, useNavigation } from '@/components/hooks';
|
import { useLoginQuery, useMessages, useNavigation, useTeamMembersQuery } from '@/components/hooks';
|
||||||
|
import { ROLES } from '@/lib/constants';
|
||||||
import { WebsiteAddButton } from './WebsiteAddButton';
|
import { WebsiteAddButton } from './WebsiteAddButton';
|
||||||
import { WebsitesDataTable } from './WebsitesDataTable';
|
import { WebsitesDataTable } from './WebsitesDataTable';
|
||||||
|
|
||||||
export function WebsitesPage() {
|
export function WebsitesPage() {
|
||||||
|
const { user } = useLoginQuery();
|
||||||
const { teamId } = useNavigation();
|
const { teamId } = useNavigation();
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
|
const { data } = useTeamMembersQuery(teamId);
|
||||||
|
|
||||||
|
const showActions =
|
||||||
|
(teamId &&
|
||||||
|
data?.data.filter(team => team.userId === user.id && team.role !== ROLES.teamViewOnly)
|
||||||
|
.length > 0) ||
|
||||||
|
(!teamId && user.role !== ROLES.viewOnly);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageBody>
|
<PageBody>
|
||||||
<Column gap="6" margin="2">
|
<Column gap="6" margin="2">
|
||||||
<PageHeader title={formatMessage(labels.websites)}>
|
<PageHeader title={formatMessage(labels.websites)}>
|
||||||
<WebsiteAddButton teamId={teamId} />
|
{showActions && <WebsiteAddButton teamId={teamId} />}
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
<Panel>
|
<Panel>
|
||||||
<WebsitesDataTable teamId={teamId} />
|
<WebsitesDataTable teamId={teamId} showActions={showActions} />
|
||||||
</Panel>
|
</Panel>
|
||||||
</Column>
|
</Column>
|
||||||
</PageBody>
|
</PageBody>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,18 @@
|
||||||
'use client';
|
'use client';
|
||||||
import { Column, Tab, TabList, TabPanel, Tabs } from '@umami/react-zen';
|
import { Column, Tab, TabList, TabPanel, Tabs } from '@umami/react-zen';
|
||||||
import { type Key, useState } from 'react';
|
import locale from 'date-fns/locale/af';
|
||||||
|
import { type Key, useMemo, useState } from 'react';
|
||||||
import { SessionModal } from '@/app/(main)/websites/[websiteId]/sessions/SessionModal';
|
import { SessionModal } from '@/app/(main)/websites/[websiteId]/sessions/SessionModal';
|
||||||
import { WebsiteControls } from '@/app/(main)/websites/[websiteId]/WebsiteControls';
|
import { WebsiteControls } from '@/app/(main)/websites/[websiteId]/WebsiteControls';
|
||||||
|
import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||||
import { Panel } from '@/components/common/Panel';
|
import { Panel } from '@/components/common/Panel';
|
||||||
import { useMessages } from '@/components/hooks';
|
import { useMessages } from '@/components/hooks';
|
||||||
|
import { useEventStatsQuery } from '@/components/hooks/queries/useEventStatsQuery';
|
||||||
import { EventsChart } from '@/components/metrics/EventsChart';
|
import { EventsChart } from '@/components/metrics/EventsChart';
|
||||||
|
import { MetricCard } from '@/components/metrics/MetricCard';
|
||||||
|
import { MetricsBar } from '@/components/metrics/MetricsBar';
|
||||||
import { MetricsTable } from '@/components/metrics/MetricsTable';
|
import { MetricsTable } from '@/components/metrics/MetricsTable';
|
||||||
|
import { formatLongNumber } from '@/lib/format';
|
||||||
import { getItem, setItem } from '@/lib/storage';
|
import { getItem, setItem } from '@/lib/storage';
|
||||||
import { EventProperties } from './EventProperties';
|
import { EventProperties } from './EventProperties';
|
||||||
import { EventsDataTable } from './EventsDataTable';
|
import { EventsDataTable } from './EventsDataTable';
|
||||||
|
|
@ -15,16 +21,61 @@ const KEY_NAME = 'umami.events.tab';
|
||||||
|
|
||||||
export function EventsPage({ websiteId }) {
|
export function EventsPage({ websiteId }) {
|
||||||
const [tab, setTab] = useState(getItem(KEY_NAME) || 'chart');
|
const [tab, setTab] = useState(getItem(KEY_NAME) || 'chart');
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels, getErrorMessage } = useMessages();
|
||||||
|
const { data, isLoading, isFetching, error } = useEventStatsQuery({
|
||||||
|
websiteId,
|
||||||
|
});
|
||||||
|
|
||||||
const handleSelect = (value: Key) => {
|
const handleSelect = (value: Key) => {
|
||||||
setItem(KEY_NAME, value);
|
setItem(KEY_NAME, value);
|
||||||
setTab(value);
|
setTab(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const metrics = useMemo(() => {
|
||||||
|
if (!data) return [];
|
||||||
|
|
||||||
|
const { events, visitors, visits, uniqueEvents } = data || {};
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
value: visitors,
|
||||||
|
label: formatMessage(labels.visitors),
|
||||||
|
formatValue: formatLongNumber,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: visits,
|
||||||
|
label: formatMessage(labels.visits),
|
||||||
|
formatValue: formatLongNumber,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: events,
|
||||||
|
label: formatMessage(labels.events),
|
||||||
|
formatValue: formatLongNumber,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: uniqueEvents,
|
||||||
|
label: formatMessage(labels.uniqueEvents),
|
||||||
|
formatValue: formatLongNumber,
|
||||||
|
},
|
||||||
|
] as any;
|
||||||
|
}, [data, locale]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Column gap="3">
|
<Column gap="3">
|
||||||
<WebsiteControls websiteId={websiteId} />
|
<WebsiteControls websiteId={websiteId} />
|
||||||
|
<LoadingPanel
|
||||||
|
data={metrics}
|
||||||
|
isLoading={isLoading}
|
||||||
|
isFetching={isFetching}
|
||||||
|
error={getErrorMessage(error)}
|
||||||
|
minHeight="136px"
|
||||||
|
>
|
||||||
|
<MetricsBar>
|
||||||
|
{metrics?.map(({ label, value, formatValue }) => {
|
||||||
|
return <MetricCard key={label} value={value} label={label} formatValue={formatValue} />;
|
||||||
|
})}
|
||||||
|
</MetricsBar>
|
||||||
|
</LoadingPanel>
|
||||||
<Panel>
|
<Panel>
|
||||||
<Tabs selectedKey={tab} onSelectionChange={key => handleSelect(key)}>
|
<Tabs selectedKey={tab} onSelectionChange={key => handleSelect(key)}>
|
||||||
<TabList>
|
<TabList>
|
||||||
|
|
|
||||||
34
src/app/api/websites/[websiteId]/events/stats/route.ts
Normal file
34
src/app/api/websites/[websiteId]/events/stats/route.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { getQueryFilters, parseRequest } from '@/lib/request';
|
||||||
|
import { json, unauthorized } from '@/lib/response';
|
||||||
|
import { dateRangeParams, filterParams } from '@/lib/schema';
|
||||||
|
import { canViewWebsite } from '@/permissions';
|
||||||
|
import { getWebsiteEventStats } from '@/queries/sql/events/getWebsiteEventStats';
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Promise<{ websiteId: string }> },
|
||||||
|
) {
|
||||||
|
const schema = z.object({
|
||||||
|
...dateRangeParams,
|
||||||
|
...filterParams,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { auth, query, error } = await parseRequest(request, schema);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return error();
|
||||||
|
}
|
||||||
|
|
||||||
|
const { websiteId } = await params;
|
||||||
|
|
||||||
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
|
return unauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
const filters = await getQueryFilters(query, websiteId);
|
||||||
|
|
||||||
|
const data = await getWebsiteEventStats(websiteId, filters);
|
||||||
|
|
||||||
|
return json({ data });
|
||||||
|
}
|
||||||
37
src/components/hooks/queries/useEventStatsQuery.ts
Normal file
37
src/components/hooks/queries/useEventStatsQuery.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import type { UseQueryOptions } from '@tanstack/react-query';
|
||||||
|
import { useDateParameters } from '@/components/hooks/useDateParameters';
|
||||||
|
import { useApi } from '../useApi';
|
||||||
|
import { useFilterParameters } from '../useFilterParameters';
|
||||||
|
|
||||||
|
export interface EventStatsData {
|
||||||
|
events: number;
|
||||||
|
visitors: number;
|
||||||
|
visits: number;
|
||||||
|
uniqueEvents: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventStatsApiResponse = {
|
||||||
|
data: EventStatsData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useEventStatsQuery(
|
||||||
|
{ websiteId }: { websiteId: string },
|
||||||
|
options?: UseQueryOptions<EventStatsApiResponse, Error, EventStatsData>,
|
||||||
|
) {
|
||||||
|
const { get, useQuery } = useApi();
|
||||||
|
const { startAt, endAt } = useDateParameters();
|
||||||
|
const filters = useFilterParameters();
|
||||||
|
|
||||||
|
return useQuery<EventStatsApiResponse, Error, EventStatsData>({
|
||||||
|
queryKey: ['websites:events:stats', { websiteId, startAt, endAt, ...filters }],
|
||||||
|
queryFn: () =>
|
||||||
|
get(`/websites/${websiteId}/events/stats`, {
|
||||||
|
startAt,
|
||||||
|
endAt,
|
||||||
|
...filters,
|
||||||
|
}),
|
||||||
|
select: response => response.data,
|
||||||
|
enabled: !!websiteId,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,7 @@ export function FilterEditForm({ websiteId, onChange, onClose }: FilterEditFormP
|
||||||
const [currentCohort, setCurrentCohort] = useState(cohort);
|
const [currentCohort, setCurrentCohort] = useState(cohort);
|
||||||
const { isMobile } = useMobile();
|
const { isMobile } = useMobile();
|
||||||
const excludeFilters = pathname.includes('/pixels') || pathname.includes('/links');
|
const excludeFilters = pathname.includes('/pixels') || pathname.includes('/links');
|
||||||
|
const excludeEvent = !pathname.endsWith('/events');
|
||||||
|
|
||||||
const handleReset = () => {
|
const handleReset = () => {
|
||||||
setCurrentFilters([]);
|
setCurrentFilters([]);
|
||||||
|
|
@ -62,7 +63,11 @@ export function FilterEditForm({ websiteId, onChange, onClose }: FilterEditFormP
|
||||||
value={currentFilters}
|
value={currentFilters}
|
||||||
onChange={setCurrentFilters}
|
onChange={setCurrentFilters}
|
||||||
exclude={
|
exclude={
|
||||||
excludeFilters ? ['path', 'title', 'hostname', 'distinctId', 'tag', 'event'] : []
|
excludeFilters
|
||||||
|
? ['path', 'title', 'hostname', 'distinctId', 'tag', 'event']
|
||||||
|
: excludeEvent
|
||||||
|
? ['event']
|
||||||
|
: []
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</TabPanel>
|
</TabPanel>
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,7 @@ export const labels = defineMessages({
|
||||||
poweredBy: { id: 'label.powered-by', defaultMessage: 'Powered by {name}' },
|
poweredBy: { id: 'label.powered-by', defaultMessage: 'Powered by {name}' },
|
||||||
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' },
|
||||||
|
uniqueEvents: { id: 'label.unique-events', defaultMessage: 'Unique Events' },
|
||||||
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' },
|
viewsPerVisit: { id: 'label.views-per-visit', defaultMessage: 'Views per visit' },
|
||||||
visitDuration: { id: 'label.visit-duration', defaultMessage: 'Visit duration' },
|
visitDuration: { id: 'label.visit-duration', defaultMessage: 'Visit duration' },
|
||||||
|
|
|
||||||
97
src/queries/sql/events/getWebsiteEventStats.ts
Normal file
97
src/queries/sql/events/getWebsiteEventStats.ts
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
import clickhouse from '@/lib/clickhouse';
|
||||||
|
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||||
|
import prisma from '@/lib/prisma';
|
||||||
|
import type { QueryFilters } from '@/lib/types';
|
||||||
|
|
||||||
|
const FUNCTION_NAME = 'getWebsiteEventStats';
|
||||||
|
|
||||||
|
export interface WebsiteEventStatsData {
|
||||||
|
events: number;
|
||||||
|
visitors: number;
|
||||||
|
visits: number;
|
||||||
|
uniqueEvents: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getWebsiteEventStats(
|
||||||
|
...args: [websiteId: string, filters: QueryFilters]
|
||||||
|
): Promise<WebsiteEventStatsData[]> {
|
||||||
|
return runQuery({
|
||||||
|
[PRISMA]: () => relationalQuery(...args),
|
||||||
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function relationalQuery(
|
||||||
|
websiteId: string,
|
||||||
|
filters: QueryFilters,
|
||||||
|
): Promise<WebsiteEventStatsData[]> {
|
||||||
|
const { parseFilters, rawQuery } = prisma;
|
||||||
|
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({
|
||||||
|
...filters,
|
||||||
|
websiteId,
|
||||||
|
});
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`
|
||||||
|
select
|
||||||
|
cast(coalesce(sum(t.c), 0) as bigint) as "events",
|
||||||
|
count(distinct t.session_id) as "visitors",
|
||||||
|
count(distinct t.visit_id) as "visits",
|
||||||
|
count(distinct t.event_name) as "uniqueEvents"
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
website_event.session_id,
|
||||||
|
website_event.visit_id,
|
||||||
|
website_event.event_name,
|
||||||
|
count(*) as "c"
|
||||||
|
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 = 2
|
||||||
|
${filterQuery}
|
||||||
|
group by 1, 2, 3
|
||||||
|
) as t
|
||||||
|
`,
|
||||||
|
queryParams,
|
||||||
|
FUNCTION_NAME,
|
||||||
|
).then(result => result?.[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function clickhouseQuery(
|
||||||
|
websiteId: string,
|
||||||
|
filters: QueryFilters,
|
||||||
|
): Promise<WebsiteEventStatsData[]> {
|
||||||
|
const { rawQuery, parseFilters } = clickhouse;
|
||||||
|
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||||
|
...filters,
|
||||||
|
websiteId,
|
||||||
|
});
|
||||||
|
|
||||||
|
return rawQuery(
|
||||||
|
`
|
||||||
|
select
|
||||||
|
sum(t.c) as "events",
|
||||||
|
uniq(t.session_id) as "visitors",
|
||||||
|
uniq(t.visit_id) as "visits",
|
||||||
|
count(distinct t.event_name) as "uniqueEvents"
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
session_id,
|
||||||
|
visit_id,
|
||||||
|
event_name,
|
||||||
|
count(*) c
|
||||||
|
from website_event
|
||||||
|
${cohortQuery}
|
||||||
|
where website_id = {websiteId:UUID}
|
||||||
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
|
and event_type = 2
|
||||||
|
${filterQuery}
|
||||||
|
group by session_id, visit_id, event_name
|
||||||
|
) as t;
|
||||||
|
`,
|
||||||
|
queryParams,
|
||||||
|
FUNCTION_NAME,
|
||||||
|
).then(result => result?.[0]);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue