Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
update insights query
Francis Cao632ecd1a1b7c
diff --git a/src/app/(main)/reports/insights/InsightsTable.tsx b/src/app/(main)/reports/insights/InsightsTable.tsxindex 2cccc24c3..692d7824b 100644--- a/src/app/(main)/reports/insights/InsightsTable.tsx+++ b/src/app/(main)/reports/insights/InsightsTable.tsx@@ -3,6 +3,7 @@ import { GridTable, GridColumn } from 'react-basics';import { useFormat, useMessages } from 'components/hooks';import { ReportContext } from '../[reportId]/Report';import EmptyPlaceholder from 'components/common/EmptyPlaceholder';+import { formatShortTime } from 'lib/format';export function InsightsTable() {const [fields, setFields] = useState([]);@@ -31,6 +32,12 @@ export function InsightsTable() {</GridColumn>);})}+ <GridColumn name="views" label={formatMessage(labels.views)} width="100px" alignment="end">+ {row => row?.views?.toLocaleString()}+ </GridColumn>+ <GridColumn name="visits" label={formatMessage(labels.visits)} width="100px" alignment="end">+ {row => row?.visits?.toLocaleString()}+ </GridColumn><GridColumnname="visitors"label={formatMessage(labels.visitors)}@@ -39,8 +46,27 @@ export function InsightsTable() {>{row => row?.visitors?.toLocaleString()}</GridColumn>- <GridColumn name="views" label={formatMessage(labels.views)} width="100px" alignment="end">- {row => row?.views?.toLocaleString()}+ <GridColumn+ name="bounceRate"+ label={formatMessage(labels.bounceRate)}+ width="100px"+ alignment="end"+ >+ {row => {+ const n = (Math.min(row?.visits, row?.bounces) / row?.visits) * 100;+ return Math.round(+n) + '%';+ }}+ </GridColumn>+ <GridColumn+ name="visitDuration"+ label={formatMessage(labels.visitDuration)}+ width="100px"+ alignment="end"+ >+ {row => {+ const n = row?.totaltime / row?.visits;+ return `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`;+ }}</GridColumn></GridTable>);diff --git a/src/lib/clickhouse.ts b/src/lib/clickhouse.tsindex e5cb40c02..c1b9d25f2 100644--- a/src/lib/clickhouse.ts+++ b/src/lib/clickhouse.ts@@ -119,7 +119,7 @@ async function parseFilters(websiteId: string, filters: QueryFilters = {}, optio};}-async function rawQuery(query: string, params: Record<string, unknown> = {}): Promise<unknown[]> {+async function rawQuery(query: string, params: Record<string, unknown> = {}): Promise<unknown> {if (process.env.LOG_QUERY) {log('QUERY:\n', query);log('PARAMETERS:\n', params);diff --git a/src/queries/analytics/reports/getInsights.ts b/src/queries/analytics/reports/getInsights.tsindex 282ed7557..7313e4369 100644--- a/src/queries/analytics/reports/getInsights.ts+++ b/src/queries/analytics/reports/getInsights.ts@@ -23,7 +23,7 @@ async function relationalQuery(y: number;}[]> {- const { parseFilters, rawQuery } = prisma;+ const { getTimestampDiffQuery, parseFilters, rawQuery } = prisma;const { filterQuery, joinSession, params } = await parseFilters(websiteId,{@@ -37,15 +37,31 @@ async function relationalQuery(return rawQuery(`- select+ select+ sum(t.c) as "views",+ count(distinct t.session_id) as "visitors",+ count(distinct t.visit_id) as "visits",+ sum(case when t.c = 1 then 1 else 0 end) as "bounces",+ sum(${getTimestampDiffQuery('t.min_time', 't.max_time')}) as "totaltime",${parseFields(fields)}- from website_event- ${joinSession}- where website_event.website_id = {{websiteId::uuid}}- and website_event.created_at between {{startDate}} and {{endDate}}- and website_event.event_type = {{eventType}}- ${filterQuery}- ${parseGroupBy(fields)}+ from (+ select+ ${parseFields(fields)},+ website_event.session_id,+ website_event.visit_id,+ count(*) as "c",+ min(website_event.created_at) as "min_time",+ max(website_event.created_at) as "max_time"+ from website_event+ ${joinSession}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ and event_type = {{eventType}}+ ${filterQuery}+ group by ${parseFields(fields)},+ website_event.session_id, website_event.visit_id+ ) as t+ group by ${parseFields(fields)}order by 1 desc, 2 desclimit 500`,@@ -71,14 +87,30 @@ async function clickhouseQuery(return rawQuery(`- select+ select+ sum(t.c) as "views",+ count(distinct t.session_id) as "visitors",+ count(distinct t.visit_id) as "visits",+ sum(if(t.c = 1, 1, 0)) as "bounces",+ sum(max_time-min_time) as "totaltime",${parseFields(fields)}- from website_event- where website_id = {websiteId:UUID}- and created_at between {startDate:DateTime64} and {endDate:DateTime64}- and event_type = {eventType:UInt32}- ${filterQuery}- ${parseGroupBy(fields)}+ from (+ select+ ${parseFields(fields)},+ session_id,+ visit_id,+ count(*) c,+ min(created_at) min_time,+ max(created_at) max_time+ from website_event+ where website_id = {websiteId:UUID}+ and created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and event_type = {eventType:UInt32}+ ${filterQuery}+ group by ${parseFields(fields)},+ session_id, visit_id+ ) as t+ group by ${parseFields(fields)}order by 1 desc, 2 desclimit 500`,@@ -89,27 +121,14 @@ async function clickhouseQuery(...a,views: Number(a.views),visitors: Number(a.visitors),+ visits: Number(a.visits),+ bounces: Number(a.bounces),+ totaltime: Number(a.totaltime),};});});}-function parseFields(fields: any[]) {- const query = fields.reduce(- (arr, field) => {- const { name } = field;-- return arr.concat(`${FILTER_COLUMNS[name]} as "${name}"`);- },- ['count(*) as views', 'count(distinct website_event.session_id) as visitors'],- );-- return query.join(',\n');-}--function parseGroupBy(fields: { name: any }[]) {- if (!fields.length) {- return '';- }- return `group by ${fields.map(({ name }) => FILTER_COLUMNS[name]).join(',')}`;+function parseFields(fields: { name: any }[]) {+ return `${fields.map(({ name }) => FILTER_COLUMNS[name]).join(',')}`;}