mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
implement pageviews, events, and channels queries
This commit is contained in:
parent
c1cad16cb9
commit
0a0c1f27c6
24 changed files with 521 additions and 86 deletions
|
|
@ -4,9 +4,9 @@ import { getQueryFilters, parseRequest } from '@/lib/request';
|
|||
import { badRequest, json, unauthorized } from '@/lib/response';
|
||||
import { dateRangeParams, filterParams, searchParams } from '@/lib/schema';
|
||||
import {
|
||||
getChannelMetrics,
|
||||
getEventMetrics,
|
||||
getPageviewMetrics,
|
||||
getChannelExpandedMetrics,
|
||||
getEventExpandedMetrics,
|
||||
getPageviewExpandedMetrics,
|
||||
getSessionExpandedMetrics,
|
||||
} from '@/queries';
|
||||
import { z } from 'zod';
|
||||
|
|
@ -46,22 +46,6 @@ export async function GET(
|
|||
if (SESSION_COLUMNS.includes(type)) {
|
||||
const data = await getSessionExpandedMetrics(websiteId, { type, limit, offset }, filters);
|
||||
|
||||
// if (type === 'language') {
|
||||
// const combined = {};
|
||||
|
||||
// for (const { x, y } of data) {
|
||||
// const key = String(x).toLowerCase().split('-')[0];
|
||||
|
||||
// if (combined[key] === undefined) {
|
||||
// combined[key] = { x: key, y };
|
||||
// } else {
|
||||
// combined[key].y += y;
|
||||
// }
|
||||
// }
|
||||
|
||||
// return json(Object.values(combined));
|
||||
// }
|
||||
|
||||
return json(data);
|
||||
}
|
||||
|
||||
|
|
@ -69,16 +53,16 @@ export async function GET(
|
|||
let data;
|
||||
|
||||
if (type === 'event') {
|
||||
data = await getEventMetrics(websiteId, { type, limit, offset }, filters);
|
||||
data = await getEventExpandedMetrics(websiteId, { type, limit, offset }, filters);
|
||||
} else {
|
||||
data = await getPageviewMetrics(websiteId, { type, limit, offset }, filters);
|
||||
data = await getPageviewExpandedMetrics(websiteId, { type, limit, offset }, filters);
|
||||
}
|
||||
|
||||
return json(data);
|
||||
}
|
||||
|
||||
if (type === 'channel') {
|
||||
const data = await getChannelMetrics(websiteId, filters);
|
||||
const data = await getChannelExpandedMetrics(websiteId, { limit, offset }, filters);
|
||||
|
||||
return json(data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,22 +46,6 @@ export async function GET(
|
|||
if (SESSION_COLUMNS.includes(type)) {
|
||||
const data = await getSessionMetrics(websiteId, { type, limit, offset }, filters);
|
||||
|
||||
if (type === 'language') {
|
||||
const combined = {};
|
||||
|
||||
for (const { x, y } of data) {
|
||||
const key = String(x).toLowerCase().split('-')[0];
|
||||
|
||||
if (combined[key] === undefined) {
|
||||
combined[key] = { x: key, y };
|
||||
} else {
|
||||
combined[key].y += y;
|
||||
}
|
||||
}
|
||||
|
||||
return json(Object.values(combined));
|
||||
}
|
||||
|
||||
return json(data);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { useDateParameters } from '../useDateParameters';
|
|||
import { ReactQueryOptions } from '@/lib/types';
|
||||
|
||||
export type WebsiteExpandedMetricsData = {
|
||||
label: string;
|
||||
name: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
|
|
|
|||
7
src/components/metrics/ListExpandedTable.module.css
Normal file
7
src/components/metrics/ListExpandedTable.module.css
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
.truncate {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
width: 300px;
|
||||
display: block;
|
||||
}
|
||||
|
|
@ -2,23 +2,25 @@ import { useMessages } from '@/components/hooks';
|
|||
import { formatShortTime } from '@/lib/format';
|
||||
import { DataColumn, DataTable } from '@umami/react-zen';
|
||||
import { ReactNode } from 'react';
|
||||
import styles from './ListExpandedTable.module.css';
|
||||
|
||||
export interface ListExpandedTableProps {
|
||||
data?: any[];
|
||||
title?: string;
|
||||
type?: string;
|
||||
renderLabel?: (row: any, index: number) => ReactNode;
|
||||
}
|
||||
|
||||
export function ListExpandedTable({ data = [], title, renderLabel }: ListExpandedTableProps) {
|
||||
export function ListExpandedTable({ data = [], title, type, renderLabel }: ListExpandedTableProps) {
|
||||
const { formatMessage, labels } = useMessages();
|
||||
|
||||
return (
|
||||
<DataTable data={data}>
|
||||
<DataColumn id="label" label={title} align="start" width="300px">
|
||||
<DataColumn id="label" label={title} align="start" className={styles.truncate}>
|
||||
{row =>
|
||||
renderLabel
|
||||
? renderLabel({ x: row?.label, country: row?.['country'] }, Number(row.id))
|
||||
: (row.label ?? formatMessage(labels.unknown))
|
||||
? renderLabel({ x: row?.['name'], country: row?.['country'] }, Number(row.id))
|
||||
: (row?.['name'] ?? formatMessage(labels.unknown))
|
||||
}
|
||||
</DataColumn>
|
||||
<DataColumn id="visitors" label={formatMessage(labels.visitors)} align="end">
|
||||
|
|
@ -30,18 +32,26 @@ export function ListExpandedTable({ data = [], title, renderLabel }: ListExpande
|
|||
<DataColumn id="pageviews" label={formatMessage(labels.views)} align="end">
|
||||
{row => row?.['pageviews']?.toLocaleString()}
|
||||
</DataColumn>
|
||||
{type !== 'exit' && type !== 'entry' ? (
|
||||
<DataColumn id="bounceRate" label={formatMessage(labels.bounceRate)} align="end">
|
||||
{row => {
|
||||
const n = (Math.min(row?.['visits'], row?.['bounces']) / row?.['visits']) * 100;
|
||||
return Math.round(+n) + '%';
|
||||
}}
|
||||
</DataColumn>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
{type !== 'exit' && type !== 'entry' ? (
|
||||
<DataColumn id="visitDuration" label={formatMessage(labels.visitDuration)} align="end">
|
||||
{row => {
|
||||
const n = (row?.['totaltime'] / row?.['visits']) * 100;
|
||||
return `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`;
|
||||
}}
|
||||
</DataColumn>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
</DataTable>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,6 @@ export function MetricsTable({
|
|||
websiteId,
|
||||
{
|
||||
type,
|
||||
limit: 30,
|
||||
search: searchFormattedValues ? undefined : search,
|
||||
...params,
|
||||
},
|
||||
|
|
@ -111,6 +110,8 @@ export function MetricsTable({
|
|||
return [];
|
||||
}, [data, dataFilter, search, limit, formatValue, type]);
|
||||
|
||||
const downloadData = expanded ? data : filteredData;
|
||||
|
||||
return (
|
||||
<Column gap="3" justifyContent="space-between">
|
||||
<LoadingPanel data={data} isFetching={isFetching} isLoading={isLoading} error={error} gap>
|
||||
|
|
@ -118,12 +119,12 @@ export function MetricsTable({
|
|||
{allowSearch && <SearchField value={search} onSearch={setSearch} delay={300} />}
|
||||
<Row>
|
||||
{children}
|
||||
{allowDownload && <DownloadButton filename={type} data={filteredData} />}
|
||||
{allowDownload && <DownloadButton filename={type} data={downloadData} />}
|
||||
</Row>
|
||||
</Row>
|
||||
{data &&
|
||||
(expanded ? (
|
||||
<ListExpandedTable {...(props as ListExpandedTableProps)} data={data} />
|
||||
<ListExpandedTable {...(props as ListExpandedTableProps)} data={data} type={type} />
|
||||
) : (
|
||||
<ListTable {...(props as ListTableProps)} data={filteredData} />
|
||||
))}
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ export function QueryParametersTable({
|
|||
dataFilter={filters[filter]}
|
||||
renderLabel={renderLabel}
|
||||
delay={0}
|
||||
expanded={false}
|
||||
>
|
||||
{allowFilter && <FilterButtons items={buttons} value={filter} onChange={setFilter} />}
|
||||
</MetricsTable>
|
||||
|
|
|
|||
|
|
@ -90,7 +90,11 @@ function mapFilter(column: string, operator: string, name: string, type: string
|
|||
function getFilterQuery(filters: Record<string, any>, options: QueryOptions = {}) {
|
||||
const query = filtersToArray(filters, options).reduce((arr, { name, column, operator }) => {
|
||||
if (column) {
|
||||
if (name === 'eventType') {
|
||||
arr.push(`and ${mapFilter(column, operator, name, 'UInt32')}`);
|
||||
} else {
|
||||
arr.push(`and ${mapFilter(column, operator, name)}`);
|
||||
}
|
||||
|
||||
if (name === 'referrer') {
|
||||
arr.push(`and referrer_domain != hostname`);
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ export const EVENT_COLUMNS = [
|
|||
'query',
|
||||
'event',
|
||||
'tag',
|
||||
'host',
|
||||
'hostname',
|
||||
];
|
||||
|
||||
export const SESSION_COLUMNS = [
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export * from '@/queries/sql/events/getEventDataValues';
|
|||
export * from '@/queries/sql/events/getEventDataStats';
|
||||
export * from '@/queries/sql/events/getEventDataUsage';
|
||||
export * from '@/queries/sql/events/getEventMetrics';
|
||||
export * from '@/queries/sql/events/getEventExpandedMetrics';
|
||||
export * from '@/queries/sql/events/getEventStats';
|
||||
export * from '@/queries/sql/events/getWebsiteEvents';
|
||||
export * from '@/queries/sql/events/getEventUsage';
|
||||
|
|
@ -21,6 +22,7 @@ export * from '@/queries/sql/reports/getRetention';
|
|||
export * from '@/queries/sql/reports/getBreakdown';
|
||||
export * from '@/queries/sql/reports/getUTM';
|
||||
export * from '@/queries/sql/pageviews/getPageviewMetrics';
|
||||
export * from '@/queries/sql/pageviews/getPageviewExpandedMetrics';
|
||||
export * from '@/queries/sql/pageviews/getPageviewStats';
|
||||
export * from '@/queries/sql/sessions/createSession';
|
||||
export * from '@/queries/sql/sessions/getWebsiteSession';
|
||||
|
|
@ -37,6 +39,7 @@ export * from '@/queries/sql/sessions/getSessionStats';
|
|||
export * from '@/queries/sql/sessions/saveSessionData';
|
||||
export * from '@/queries/sql/getActiveVisitors';
|
||||
export * from '@/queries/sql/getChannelMetrics';
|
||||
export * from '@/queries/sql/getChannelExpandedMetrics';
|
||||
export * from '@/queries/sql/getRealtimeActivity';
|
||||
export * from '@/queries/sql/getRealtimeData';
|
||||
export * from '@/queries/sql/getValues';
|
||||
|
|
|
|||
113
src/queries/sql/events/getEventExpandedMetrics.ts
Normal file
113
src/queries/sql/events/getEventExpandedMetrics.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export interface EventExpandedMetricParameters {
|
||||
type: string;
|
||||
limit?: string;
|
||||
offset?: string;
|
||||
}
|
||||
|
||||
export interface EventExpandedMetricData {
|
||||
name: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
bounces: number;
|
||||
totaltime: number;
|
||||
}
|
||||
|
||||
export async function getEventExpandedMetrics(
|
||||
...args: [websiteId: string, parameters: EventExpandedMetricParameters, filters: QueryFilters]
|
||||
): Promise<EventExpandedMetricData[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
parameters: EventExpandedMetricParameters,
|
||||
filters: QueryFilters,
|
||||
) {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: EVENT_TYPE.customEvent,
|
||||
},
|
||||
{ joinSession: SESSION_COLUMNS.includes(type) },
|
||||
);
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${column} x,
|
||||
count(*) as y
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and event_type = {{eventType}}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
queryParams,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
parameters: EventExpandedMetricParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<EventExpandedMetricData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
name,
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(t.visit_id) as "visits",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select
|
||||
${column} name,
|
||||
session_id,
|
||||
visit_id,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and name != ''
|
||||
${filterQuery}
|
||||
group by name, session_id, visit_id
|
||||
) as t
|
||||
group by name
|
||||
order by visitors desc, visits desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
|
|
@ -83,7 +83,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by x
|
||||
order by y desc
|
||||
|
|
|
|||
|
|
@ -72,7 +72,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by x, t
|
||||
order by t
|
||||
|
|
|
|||
162
src/queries/sql/getChannelExpandedMetrics.ts
Normal file
162
src/queries/sql/getChannelExpandedMetrics.ts
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import {
|
||||
EMAIL_DOMAINS,
|
||||
EVENT_TYPE,
|
||||
PAID_AD_PARAMS,
|
||||
SEARCH_DOMAINS,
|
||||
SHOPPING_DOMAINS,
|
||||
SOCIAL_DOMAINS,
|
||||
VIDEO_DOMAINS,
|
||||
} from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export interface ChannelExpandedMetricsParameters {
|
||||
limit?: number | string;
|
||||
offset?: number | string;
|
||||
}
|
||||
|
||||
export interface ChannelExpandedMetricsData {
|
||||
name: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
bounces: number;
|
||||
totaltime: number;
|
||||
}
|
||||
|
||||
export async function getChannelExpandedMetrics(
|
||||
...args: [websiteId: string, parameters: ChannelExpandedMetricsParameters, filters?: QueryFilters]
|
||||
): Promise<ChannelExpandedMetricsData[]> {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
parameters: ChannelExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<ChannelExpandedMetricsData[]> {
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { queryParams, filterQuery, cohortQuery, dateQuery } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
WITH channels as (
|
||||
select case when ${toPostgresPositionClause('utm_medium', ['cp', 'ppc', 'retargeting', 'paid'])} then 'paid' else 'organic' end prefix,
|
||||
case
|
||||
when referrer_domain = '' and url_query = '' then 'direct'
|
||||
when ${toPostgresPositionClause('url_query', PAID_AD_PARAMS)} then 'paidAds'
|
||||
when ${toPostgresPositionClause('utm_medium', ['referral', 'app', 'link'])} then 'referral'
|
||||
when position(utm_medium, 'affiliate') > 0 then 'affiliate'
|
||||
when position(utm_medium, 'sms') > 0 or position(utm_source, 'sms') > 0 then 'sms'
|
||||
when ${toPostgresPositionClause('referrer_domain', SEARCH_DOMAINS)} or position(utm_medium, 'organic') > 0 then concat(prefix, 'Search')
|
||||
when ${toPostgresPositionClause('referrer_domain', SOCIAL_DOMAINS)} then concat(prefix, 'Social')
|
||||
when ${toPostgresPositionClause('referrer_domain', EMAIL_DOMAINS)} or position(utm_medium, 'mail') > 0 then 'email'
|
||||
when ${toPostgresPositionClause('referrer_domain', SHOPPING_DOMAINS)} or position(utm_medium, 'shop') > 0 then concat(prefix, 'Shopping')
|
||||
when ${toPostgresPositionClause('referrer_domain', VIDEO_DOMAINS)} or position(utm_medium, 'video') > 0 then concat(prefix, 'Video')
|
||||
else '' end AS x,
|
||||
count(distinct session_id) y
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {{websiteId::uuid}}
|
||||
and event_type = {{eventType}}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
order by y desc)
|
||||
|
||||
select x, sum(y) y
|
||||
from channels
|
||||
where x != ''
|
||||
group by x
|
||||
order by y desc;
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
parameters: ChannelExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<ChannelExpandedMetricsData[]> {
|
||||
const { limit = 500, offset = 0 } = parameters;
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { queryParams, filterQuery, cohortQuery } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: EVENT_TYPE.pageView,
|
||||
});
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
name,
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(t.visit_id) as "visits",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select case when multiSearchAny(utm_medium, ['cp', 'ppc', 'retargeting', 'paid']) != 0 then 'paid' else 'organic' end prefix,
|
||||
case
|
||||
when referrer_domain = '' and url_query = '' then 'direct'
|
||||
when multiSearchAny(url_query, [${toClickHouseStringArray(
|
||||
PAID_AD_PARAMS,
|
||||
)}]) != 0 then 'paidAds'
|
||||
when multiSearchAny(utm_medium, ['referral', 'app','link']) != 0 then 'referral'
|
||||
when position(utm_medium, 'affiliate') > 0 then 'affiliate'
|
||||
when position(utm_medium, 'sms') > 0 or position(utm_source, 'sms') > 0 then 'sms'
|
||||
when multiSearchAny(referrer_domain, [${toClickHouseStringArray(
|
||||
SEARCH_DOMAINS,
|
||||
)}]) != 0 or position(utm_medium, 'organic') > 0 then concat(prefix, 'Search')
|
||||
when multiSearchAny(referrer_domain, [${toClickHouseStringArray(
|
||||
SOCIAL_DOMAINS,
|
||||
)}]) != 0 then concat(prefix, 'Social')
|
||||
when multiSearchAny(referrer_domain, [${toClickHouseStringArray(
|
||||
EMAIL_DOMAINS,
|
||||
)}]) != 0 or position(utm_medium, 'mail') > 0 then 'email'
|
||||
when multiSearchAny(referrer_domain, [${toClickHouseStringArray(
|
||||
SHOPPING_DOMAINS,
|
||||
)}]) != 0 or position(utm_medium, 'shop') > 0 then concat(prefix, 'Shopping')
|
||||
when multiSearchAny(referrer_domain, [${toClickHouseStringArray(
|
||||
VIDEO_DOMAINS,
|
||||
)}]) != 0 or position(utm_medium, 'video') > 0 then concat(prefix, 'Video')
|
||||
else '' end AS name,
|
||||
session_id,
|
||||
visit_id,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and name != ''
|
||||
${filterQuery}
|
||||
group by prefix, name, session_id, visit_id
|
||||
) as t
|
||||
group by name
|
||||
order by visitors desc, visits desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
|
||||
function toClickHouseStringArray(arr: string[]): string {
|
||||
return arr.map(p => `'${p.replace(/'/g, "\\'")}'`).join(', ');
|
||||
}
|
||||
|
||||
function toPostgresPositionClause(column: string, arr: string[]) {
|
||||
return arr.map(val => `position(${column}, '${val.replace(/'/g, "''")}') > 0`).join(' OR\n ');
|
||||
}
|
||||
|
|
@ -105,7 +105,6 @@ async function clickhouseQuery(
|
|||
from website_event
|
||||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and event_type = {eventType:UInt32}
|
||||
${dateQuery}
|
||||
${filterQuery}
|
||||
group by 1, 2
|
||||
|
|
|
|||
|
|
@ -94,7 +94,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by session_id, visit_id
|
||||
) as t;
|
||||
|
|
@ -117,7 +116,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by session_id, visit_id
|
||||
) as t;
|
||||
|
|
|
|||
166
src/queries/sql/pageviews/getPageviewExpandedMetrics.ts
Normal file
166
src/queries/sql/pageviews/getPageviewExpandedMetrics.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
import clickhouse from '@/lib/clickhouse';
|
||||
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
|
||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { QueryFilters } from '@/lib/types';
|
||||
|
||||
export interface PageviewExpandedMetricsParameters {
|
||||
type: string;
|
||||
limit?: number | string;
|
||||
offset?: number | string;
|
||||
}
|
||||
|
||||
export interface PageviewExpandedMetricsData {
|
||||
name: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
bounces: number;
|
||||
totaltime: number;
|
||||
}
|
||||
|
||||
export async function getPageviewExpandedMetrics(
|
||||
...args: [websiteId: string, parameters: PageviewExpandedMetricsParameters, filters: QueryFilters]
|
||||
) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(
|
||||
websiteId: string,
|
||||
parameters: PageviewExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<PageviewExpandedMetricsData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { rawQuery, parseFilters } = prisma;
|
||||
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters(
|
||||
{
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
},
|
||||
{ joinSession: SESSION_COLUMNS.includes(type) },
|
||||
);
|
||||
|
||||
let entryExitQuery = '';
|
||||
let excludeDomain = '';
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
excludeDomain = `and website_event.referrer_domain != website_event.hostname
|
||||
and website_event.referrer_domain != ''`;
|
||||
}
|
||||
|
||||
if (type === 'entry' || type === 'exit') {
|
||||
const aggregrate = type === 'entry' ? 'min' : 'max';
|
||||
|
||||
entryExitQuery = `
|
||||
join (
|
||||
select visit_id,
|
||||
${aggregrate}(created_at) target_created_at
|
||||
from website_event
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and event_type = {{eventType}}
|
||||
group by visit_id
|
||||
) x
|
||||
on x.visit_id = website_event.visit_id
|
||||
and x.target_created_at = website_event.created_at
|
||||
`;
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select ${column} x,
|
||||
count(distinct website_event.session_id) as y
|
||||
from website_event
|
||||
${joinSessionQuery}
|
||||
${cohortQuery}
|
||||
${entryExitQuery}
|
||||
where website_event.website_id = {{websiteId::uuid}}
|
||||
and website_event.created_at between {{startDate}} and {{endDate}}
|
||||
and event_type = {{eventType}}
|
||||
${excludeDomain}
|
||||
${filterQuery}
|
||||
group by 1
|
||||
order by 2 desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
queryParams,
|
||||
);
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId: string,
|
||||
parameters: PageviewExpandedMetricsParameters,
|
||||
filters: QueryFilters,
|
||||
): Promise<{ x: string; y: number }[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
const { rawQuery, parseFilters } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
websiteId,
|
||||
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
||||
});
|
||||
|
||||
let excludeDomain = '';
|
||||
let entryExitQuery = '';
|
||||
|
||||
if (column === 'referrer_domain') {
|
||||
excludeDomain = `and referrer_domain != hostname and referrer_domain != ''`;
|
||||
}
|
||||
|
||||
if (type === 'entry' || type === 'exit') {
|
||||
const aggregrate = type === 'entry' ? 'min' : 'max';
|
||||
|
||||
entryExitQuery = `
|
||||
JOIN (select visit_id,
|
||||
${aggregrate}(created_at) target_created_at
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
group by visit_id) x
|
||||
ON x.visit_id = website_event.visit_id
|
||||
and x.target_created_at = website_event.created_at`;
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
name,
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
uniq(t.visit_id) as "visits",
|
||||
sum(if(t.c = 1, 1, 0)) as "bounces",
|
||||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select
|
||||
${column} name,
|
||||
session_id,
|
||||
visit_id,
|
||||
count(*) c,
|
||||
min(created_at) min_time,
|
||||
max(created_at) max_time
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${entryExitQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and name != ''
|
||||
${excludeDomain}
|
||||
${filterQuery}
|
||||
group by name, session_id, visit_id
|
||||
) as t
|
||||
group by name
|
||||
order by visitors desc, visits desc
|
||||
limit ${limit}
|
||||
offset ${offset}
|
||||
`,
|
||||
{ ...queryParams, ...parameters },
|
||||
);
|
||||
}
|
||||
|
|
@ -136,7 +136,6 @@ async function clickhouseQuery(
|
|||
${entryExitQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${excludeDomain}
|
||||
${filterQuery}
|
||||
group by x
|
||||
|
|
@ -174,7 +173,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${excludeDomain}
|
||||
${filterQuery}
|
||||
${groupByQuery}) as g
|
||||
|
|
|
|||
|
|
@ -66,7 +66,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by t
|
||||
) as g
|
||||
|
|
@ -85,7 +84,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by t
|
||||
) as g
|
||||
|
|
|
|||
|
|
@ -477,7 +477,6 @@ async function clickhouseQuery(
|
|||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and ${column} = {step:String}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
`,
|
||||
queryParams,
|
||||
|
|
|
|||
|
|
@ -115,7 +115,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by ${parseFieldsByName(fields)},
|
||||
session_id, visit_id
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export interface SessionExpandedMetricsParameters {
|
|||
}
|
||||
|
||||
export interface SessionExpandedMetricsData {
|
||||
label: string;
|
||||
name: string;
|
||||
pageviews: number;
|
||||
visitors: number;
|
||||
visits: number;
|
||||
|
|
@ -34,7 +34,7 @@ async function relationalQuery(
|
|||
filters: QueryFilters,
|
||||
): Promise<SessionExpandedMetricsData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
let column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters(
|
||||
{
|
||||
|
|
@ -48,6 +48,10 @@ async function relationalQuery(
|
|||
);
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
if (type === 'language') {
|
||||
column = `lower(left(${type}, 2))`;
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
|
|
@ -77,7 +81,7 @@ async function clickhouseQuery(
|
|||
filters: QueryFilters,
|
||||
): Promise<SessionExpandedMetricsData[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
let column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
|
|
@ -86,10 +90,14 @@ async function clickhouseQuery(
|
|||
});
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
if (type === 'language') {
|
||||
column = `lower(left(${type}, 2))`;
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
label,
|
||||
name,
|
||||
${includeCountry ? 'country,' : ''}
|
||||
sum(t.c) as "pageviews",
|
||||
uniq(t.session_id) as "visitors",
|
||||
|
|
@ -98,7 +106,7 @@ async function clickhouseQuery(
|
|||
sum(max_time-min_time) as "totaltime"
|
||||
from (
|
||||
select
|
||||
${column} label,
|
||||
${column} name,
|
||||
${includeCountry ? 'country,' : ''}
|
||||
session_id,
|
||||
visit_id,
|
||||
|
|
@ -109,13 +117,12 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
and label != ''
|
||||
and name != ''
|
||||
${filterQuery}
|
||||
group by label, session_id, visit_id
|
||||
group by name, session_id, visit_id
|
||||
${includeCountry ? ', country' : ''}
|
||||
) as t
|
||||
group by label
|
||||
group by name
|
||||
${includeCountry ? ', country' : ''}
|
||||
order by visitors desc, visits desc
|
||||
limit ${limit}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ async function relationalQuery(
|
|||
filters: QueryFilters,
|
||||
) {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
let column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = prisma;
|
||||
const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters(
|
||||
{
|
||||
|
|
@ -39,6 +39,10 @@ async function relationalQuery(
|
|||
);
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
if (type === 'language') {
|
||||
column = `lower(left(${type}, 2))`;
|
||||
}
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
select
|
||||
|
|
@ -68,7 +72,7 @@ async function clickhouseQuery(
|
|||
filters: QueryFilters,
|
||||
): Promise<{ x: string; y: number }[]> {
|
||||
const { type, limit = 500, offset = 0 } = parameters;
|
||||
const column = FILTER_COLUMNS[type] || type;
|
||||
let column = FILTER_COLUMNS[type] || type;
|
||||
const { parseFilters, rawQuery } = clickhouse;
|
||||
const { filterQuery, cohortQuery, queryParams } = parseFilters({
|
||||
...filters,
|
||||
|
|
@ -77,6 +81,10 @@ async function clickhouseQuery(
|
|||
});
|
||||
const includeCountry = column === 'city' || column === 'region';
|
||||
|
||||
if (type === 'language') {
|
||||
column = `lower(left(${type}, 2))`;
|
||||
}
|
||||
|
||||
let sql = '';
|
||||
|
||||
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||
|
|
@ -89,7 +97,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by x
|
||||
${includeCountry ? ', country' : ''}
|
||||
|
|
@ -107,7 +114,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by x
|
||||
${includeCountry ? ', country' : ''}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,6 @@ async function clickhouseQuery(
|
|||
${cohortQuery}
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by t
|
||||
) as g
|
||||
|
|
@ -84,7 +83,6 @@ async function clickhouseQuery(
|
|||
from website_event_stats_hourly as website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||
and event_type = {eventType:UInt32}
|
||||
${filterQuery}
|
||||
group by t
|
||||
) as g
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue