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
implement filtering and cohorts to all relevant queries
Francis Cao2f1f70472801
diff --git a/src/app/(main)/boards/BoardsPage.tsx b/src/app/(main)/boards/BoardsPage.tsxindex 991ef965c..adf0e02bf 100644--- a/src/app/(main)/boards/BoardsPage.tsx+++ b/src/app/(main)/boards/BoardsPage.tsx@@ -7,7 +7,7 @@ import { BoardAddButton } from './BoardAddButton';export function BoardsPage() {return (<PageBody>- <Column>+ <Column margin="2"><PageHeader title="My Boards"><BoardAddButton /></PageHeader>diff --git a/src/app/(main)/links/LinksPage.tsx b/src/app/(main)/links/LinksPage.tsxindex 766974f50..e94bccf83 100644--- a/src/app/(main)/links/LinksPage.tsx+++ b/src/app/(main)/links/LinksPage.tsx@@ -13,7 +13,7 @@ export function LinksPage() {return (<PageBody>- <Column gap="6">+ <Column gap="6" margin="2"><PageHeader title={formatMessage(labels.links)}><LinkAddButton teamId={teamId} /></PageHeader>diff --git a/src/app/(main)/pixels/PixelsPage.tsx b/src/app/(main)/pixels/PixelsPage.tsxindex 58ccb7d1d..0733d5b11 100644--- a/src/app/(main)/pixels/PixelsPage.tsx+++ b/src/app/(main)/pixels/PixelsPage.tsx@@ -13,7 +13,7 @@ export function PixelsPage() {return (<PageBody>- <Column gap="6">+ <Column gap="6" margin="2"><PageHeader title={formatMessage(labels.pixels)}><PixelAddButton teamId={teamId} /></PageHeader>diff --git a/src/app/(main)/websites/WebsitesPage.tsx b/src/app/(main)/websites/WebsitesPage.tsxindex db563147f..313dccc7d 100644--- a/src/app/(main)/websites/WebsitesPage.tsx+++ b/src/app/(main)/websites/WebsitesPage.tsx@@ -13,7 +13,7 @@ export function WebsitesPage() {return (<PageBody>- <Column gap="6">+ <Column gap="6" margin="2"><PageHeader title={formatMessage(labels.websites)}><WebsiteAddButton teamId={teamId} /></PageHeader>diff --git a/src/queries/sql/events/getEventDataFields.ts b/src/queries/sql/events/getEventDataFields.tsindex 91f9771e2..1b76f0a06 100644--- a/src/queries/sql/events/getEventDataFields.ts+++ b/src/queries/sql/events/getEventDataFields.ts@@ -1,6 +1,6 @@-import prisma from '@/lib/prisma';import clickhouse from '@/lib/clickhouse';import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';+import prisma from '@/lib/prisma';import { QueryFilters } from '@/lib/types';export async function getEventDataFields(...args: [websiteId: string, filters: QueryFilters]) {@@ -12,7 +12,10 @@ export async function getEventDataFields(...args: [websiteId: string, filters: Qasync function relationalQuery(websiteId: string, filters: QueryFilters) {const { rawQuery, parseFilters, getDateSQL } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters(filters);+ const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`@@ -30,6 +33,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {and website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${cohortQuery}+ ${joinSessionQuery}where event_data.website_id = {{websiteId::uuid}}and event_data.created_at between {{startDate}} and {{endDate}}${filterQuery}@@ -46,7 +50,7 @@ async function clickhouseQuery(filters: QueryFilters,): Promise<{ propertyName: string; dataType: number; propertyValue: string; total: number }[]> {const { rawQuery, parseFilters } = clickhouse;- const { filterQuery, cohortQuery, queryParams } = parseFilters(filters);+ const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });return rawQuery(`diff --git a/src/queries/sql/events/getEventDataProperties.ts b/src/queries/sql/events/getEventDataProperties.tsindex 708abebf9..7eccf594c 100644--- a/src/queries/sql/events/getEventDataProperties.ts+++ b/src/queries/sql/events/getEventDataProperties.ts@@ -17,7 +17,7 @@ async function relationalQuery(filters: QueryFilters & { propertyName?: string },) {const { rawQuery, parseFilters } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters(+ const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({ ...filters, websiteId },{columns: { propertyName: 'data_key' },@@ -35,6 +35,7 @@ async function relationalQuery(and website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${cohortQuery}+ ${joinSessionQuery}where event_data.website_id = {{websiteId::uuid}}and event_data.created_at between {{startDate}} and {{endDate}}${filterQuery}diff --git a/src/queries/sql/events/getEventDataStats.ts b/src/queries/sql/events/getEventDataStats.tsindex 01b5db342..ea42be7c0 100644--- a/src/queries/sql/events/getEventDataStats.ts+++ b/src/queries/sql/events/getEventDataStats.ts@@ -18,7 +18,10 @@ export async function getEventDataStats(async function relationalQuery(websiteId: string, filters: QueryFilters) {const { rawQuery, parseFilters } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`@@ -33,9 +36,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {count(*) as "total"from event_datajoin website_event on website_event.event_id = event_data.website_event_id- and website_event.website_id = {{websiteId::uuid}}- and website_event.created_at between {{startDate}} and {{endDate}}- ${cohortQuery}+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${cohortQuery}+ ${joinSessionQuery}where event_data.website_id = {{websiteId::uuid}}and event_data.created_at between {{startDate}} and {{endDate}}${filterQuery}diff --git a/src/queries/sql/events/getEventDataValues.ts b/src/queries/sql/events/getEventDataValues.tsindex 1c30617ca..a8f4b4a1e 100644--- a/src/queries/sql/events/getEventDataValues.ts+++ b/src/queries/sql/events/getEventDataValues.ts@@ -25,7 +25,10 @@ async function relationalQuery(filters: QueryFilters & { eventName?: string; propertyName?: string },) {const { rawQuery, parseFilters, getDateSQL } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`@@ -41,6 +44,7 @@ async function relationalQuery(and website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${cohortQuery}+ ${joinSessionQuery}where event_data.website_id = {{websiteId::uuid}}and event_data.created_at between {{startDate}} and {{endDate}}and event_data.data_key = {{propertyName}}diff --git a/src/queries/sql/events/getEventExpandedMetrics.ts b/src/queries/sql/events/getEventExpandedMetrics.tsindex 235822b82..96a452fa9 100644--- a/src/queries/sql/events/getEventExpandedMetrics.ts+++ b/src/queries/sql/events/getEventExpandedMetrics.ts@@ -54,7 +54,6 @@ async function relationalQuery(${joinSessionQuery}where website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}- and event_type = {{eventType}}${filterQuery}group by 1order by 2 descdiff --git a/src/queries/sql/events/getEventMetrics.ts b/src/queries/sql/events/getEventMetrics.tsindex ad95e316e..4ecfe2d5a 100644--- a/src/queries/sql/events/getEventMetrics.ts+++ b/src/queries/sql/events/getEventMetrics.ts@@ -51,7 +51,6 @@ async function relationalQuery(${joinSessionQuery}where website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}- and event_type = {{eventType}}${filterQuery}group by 1order by 2 descdiff --git a/src/queries/sql/events/getEventStats.ts b/src/queries/sql/events/getEventStats.tsindex 8d26dcfc7..54afaaf50 100644--- a/src/queries/sql/events/getEventStats.ts+++ b/src/queries/sql/events/getEventStats.ts@@ -39,7 +39,6 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${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, 2order by 2diff --git a/src/queries/sql/events/getWebsiteEvents.ts b/src/queries/sql/events/getWebsiteEvents.tsindex 579593d66..b81ca9701 100644--- a/src/queries/sql/events/getWebsiteEvents.ts+++ b/src/queries/sql/events/getWebsiteEvents.ts@@ -31,6 +31,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {website_id as "websiteId",session_id as "sessionId",created_at as "createdAt",+ hostname,url_path as "urlPath",url_query as "urlQuery",referrer_path as "referrerPath",@@ -46,6 +47,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {event_name as "eventName"from website_event${cohortQuery}+ join session on website_event.session_id = session.session_idwhere website_id = {{websiteId::uuid}}${dateQuery}${filterQuery}@@ -77,6 +79,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {website_id as websiteId,session_id as sessionId,created_at as createdAt,+ hostname,url_path as urlPath,url_query as urlQuery,referrer_path as referrerPath,@@ -84,13 +87,13 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {referrer_domain as referrerDomain,country as country,city as city,- device as device,+ device as device,os as os,browser as browser,page_title as pageTitle,event_type as eventType,event_name as eventName,- event_id IN (SELECT event_id FROM event_data) AS hasData+ event_id IN (SELECT event_id FROM event_data) as hasDatafrom website_event${cohortQuery}where website_id = {websiteId:UUID}diff --git a/src/queries/sql/getChannelExpandedMetrics.ts b/src/queries/sql/getChannelExpandedMetrics.tsindex e4ce08abf..51f5e37e6 100644--- a/src/queries/sql/getChannelExpandedMetrics.ts+++ b/src/queries/sql/getChannelExpandedMetrics.ts@@ -40,7 +40,7 @@ async function relationalQuery(filters: QueryFilters,): Promise<ChannelExpandedMetricsData[]> {const { rawQuery, parseFilters } = prisma;- const { queryParams, filterQuery, cohortQuery, dateQuery } = parseFilters({+ const { queryParams, filterQuery, joinSessionQuery, cohortQuery, dateQuery } = parseFilters({...filters,websiteId,eventType: EVENT_TYPE.pageView,@@ -65,8 +65,8 @@ async function relationalQuery(count(distinct session_id) yfrom website_event${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}- and event_type = {{eventType}}${dateQuery}${filterQuery}group by 1, 2diff --git a/src/queries/sql/getChannelMetrics.ts b/src/queries/sql/getChannelMetrics.tsindex 9e6ba82c4..a828dc8a2 100644--- a/src/queries/sql/getChannelMetrics.ts+++ b/src/queries/sql/getChannelMetrics.ts@@ -21,7 +21,7 @@ export async function getChannelMetrics(...args: [websiteId: string, filters?: Qasync function relationalQuery(websiteId: string, filters: QueryFilters) {const { rawQuery, parseFilters } = prisma;- const { queryParams, filterQuery, cohortQuery, dateQuery } = parseFilters({+ const { queryParams, filterQuery, joinSessionQuery, cohortQuery, dateQuery } = parseFilters({...filters,websiteId,eventType: EVENT_TYPE.pageView,@@ -46,8 +46,8 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {count(distinct session_id) yfrom website_event${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}- and event_type = {{eventType}}${dateQuery}${filterQuery}group by 1, 2diff --git a/src/queries/sql/getRealtimeActivity.ts b/src/queries/sql/getRealtimeActivity.tsindex d53cab6bd..6ae72aa3a 100644--- a/src/queries/sql/getRealtimeActivity.ts+++ b/src/queries/sql/getRealtimeActivity.ts@@ -12,7 +12,10 @@ export async function getRealtimeActivity(...args: [websiteId: string, filters:async function relationalQuery(websiteId: string, filters: QueryFilters) {const { rawQuery, parseFilters } = prisma;- const { queryParams, filterQuery, cohortQuery, dateQuery } = parseFilters(filters);+ const { queryParams, filterQuery, cohortQuery, dateQuery } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`diff --git a/src/queries/sql/getWebsiteStats.ts b/src/queries/sql/getWebsiteStats.tsindex 4fa15a3c0..925c40818 100644--- a/src/queries/sql/getWebsiteStats.ts+++ b/src/queries/sql/getWebsiteStats.ts@@ -49,11 +49,10 @@ async function relationalQuery(min(website_event.created_at) as "min_time",max(website_event.created_at) as "max_time"from website_event- ${joinSessionQuery}- ${cohortQuery}+ ${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, 2) as tdiff --git a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts b/src/queries/sql/pageviews/getPageviewExpandedMetrics.tsindex ce1f1df9f..3d0e2f8c8 100644--- a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts+++ b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts@@ -76,12 +76,11 @@ async function relationalQuery(select ${column} x,count(distinct website_event.session_id) as yfrom website_event- ${joinSessionQuery}${cohortQuery}+ ${joinSessionQuery}${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 1diff --git a/src/queries/sql/pageviews/getPageviewMetrics.ts b/src/queries/sql/pageviews/getPageviewMetrics.tsindex 8d73c6f0b..a5b13733c 100644--- a/src/queries/sql/pageviews/getPageviewMetrics.ts+++ b/src/queries/sql/pageviews/getPageviewMetrics.ts@@ -73,12 +73,11 @@ async function relationalQuery(select ${column} x,count(distinct website_event.session_id) as yfrom website_event- ${joinSessionQuery}${cohortQuery}+ ${joinSessionQuery}${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 1diff --git a/src/queries/sql/pageviews/getPageviewStats.ts b/src/queries/sql/pageviews/getPageviewStats.tsindex e9e763e87..a8ee4734c 100644--- a/src/queries/sql/pageviews/getPageviewStats.ts+++ b/src/queries/sql/pageviews/getPageviewStats.ts@@ -26,11 +26,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${getDateSQL('website_event.created_at', unit, timezone)} x,count(*) yfrom website_event- ${joinSessionQuery}- ${cohortQuery}+ ${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 1order by 1diff --git a/src/queries/sql/reports/getAttribution.ts b/src/queries/sql/reports/getAttribution.tsindex 0bfbd58b4..b4df4d11e 100644--- a/src/queries/sql/reports/getAttribution.ts+++ b/src/queries/sql/reports/getAttribution.ts@@ -42,9 +42,10 @@ async function relationalQuery(const { rawQuery, parseFilters } = prisma;const eventType = type === 'page' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;const column = type === 'page' ? 'url_path' : 'event_name';- const { filterQuery, queryParams } = parseFilters({+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({...filters,...parameters,+ websiteId,eventType,});@@ -71,23 +72,32 @@ async function relationalQuery(session_id,max(created_at) max_dtfrom website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}and ${column} = {{step}}- and event_type = {{eventType}}${filterQuery}group by 1),`;const revenueEventQuery = `WITH events AS (select- session_id,- max(created_at) max_dt,- sum(revenue) value+ revenue.session_id,+ max(revenue.created_at) max_dt,+ sum(revenue.revenue) valuefrom revenue- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and ${column} = {{step}}- and currency = {{currency}}+ join website_event+ on website_event.website_id = revenue.website_id+ and website_event.session_id = revenue.session_id+ and website_event.event_id = revenue.event_id+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${cohortQuery}+ ${joinSessionQuery}+ where revenue.website_id = {{websiteId::uuid}}+ and revenue.created_at between {{startDate}} and {{endDate}}+ and revenue.${column} = {{step}}+ and revenue.currency = {{currency}}${filterQuery}group by 1),`;@@ -227,10 +237,11 @@ async function relationalQuery(count(distinct session_id) as "visitors",count(distinct visit_id) as "visits"from website_event+ ${joinSessionQuery}+ ${cohortQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}and ${column} = {{step}}- and event_type = {{eventType}}${filterQuery}`,queryParams,@@ -257,7 +268,7 @@ async function clickhouseQuery(const { rawQuery, parseFilters } = clickhouse;const eventType = type === 'page' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;const column = type === 'page' ? 'url_path' : 'event_name';- const { filterQuery, queryParams } = parseFilters({+ const { filterQuery, cohortQuery, queryParams } = parseFilters({...filters,...parameters,websiteId,@@ -293,7 +304,6 @@ async function clickhouseQuery(on we.session_id = e.session_idwhere we.website_id = {websiteId:UUID}and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}- ${filterQuery}group by e.session_id)`;}@@ -307,7 +317,6 @@ async function clickhouseQuery(where we.website_id = {websiteId:UUID}and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}and we.created_at < e.max_dt- ${filterQuery}group by e.session_id)`;}@@ -317,22 +326,31 @@ async function clickhouseQuery(session_id,max(created_at) max_dtfrom website_event+ ${cohortQuery}where website_id = {websiteId:UUID}and created_at between {startDate:DateTime64} and {endDate:DateTime64}and ${column} = {step:String}- and event_type = {eventType:UInt32}+ ${filterQuery}group by 1),`;const revenueEventQuery = `WITH events AS (select- session_id,- max(created_at) max_dt,- sum(revenue) as value+ website_revenue.session_id,+ max(website_revenue.created_at) max_dt,+ sum(website_revenue.revenue) as valuefrom website_revenue- where website_id = {websiteId:UUID}- and created_at between {startDate:DateTime64} and {endDate:DateTime64}- and ${column} = {step:String}- and currency = {currency:String}+ join website_event+ on website_event.website_id = website_revenue.website_id+ and website_event.session_id = website_revenue.session_id+ and website_event.event_id = website_revenue.event_id+ and website_event.website_id = {websiteId:UUID}+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${cohortQuery}+ where website_revenue.website_id = {websiteId:UUID}+ and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and website_revenue.${column} = {step:String}+ and website_revenue.currency = {currency:String}+ ${filterQuery}group by 1),`;const referrerRes = await rawQuery<@@ -474,6 +492,7 @@ async function clickhouseQuery(uniqExact(session_id) as "visitors",uniqExact(visit_id) as "visits"from website_event+ ${cohortQuery}where website_id = {websiteId:UUID}and created_at between {startDate:DateTime64} and {endDate:DateTime64}and ${column} = {step:String}diff --git a/src/queries/sql/reports/getBreakdown.ts b/src/queries/sql/reports/getBreakdown.tsindex 7cae67a1a..6cea68f3f 100644--- a/src/queries/sql/reports/getBreakdown.ts+++ b/src/queries/sql/reports/getBreakdown.ts@@ -62,11 +62,10 @@ async function relationalQuery(min(website_event.created_at) as "min_time",max(website_event.created_at) as "max_time"from website_event- ${cohortQuery}- ${joinSessionQuery}+ ${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 ${parseFieldsByName(fields)},website_event.session_id, website_event.visit_iddiff --git a/src/queries/sql/reports/getFunnel.ts b/src/queries/sql/reports/getFunnel.tsindex 9065b6093..81e2f3bb9 100644--- a/src/queries/sql/reports/getFunnel.ts+++ b/src/queries/sql/reports/getFunnel.ts@@ -34,7 +34,12 @@ async function relationalQuery(const { rawQuery, getAddIntervalQuery, parseFilters } = prisma;const { levelOneQuery, levelQuery, sumQuery, params } = getFunnelQuery(steps, window);- const { filterQuery, queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate });+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ startDate,+ endDate,+ });function getFunnelQuery(steps: { type: string; value: string }[],@@ -65,6 +70,8 @@ async function relationalQuery(WITH level1 AS (select distinct session_id, created_atfrom website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}and ${column} ${operator} {{${i}}}@@ -132,7 +139,12 @@ async function clickhouseQuery(steps,window,);- const { filterQuery, queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate });+ const { filterQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ startDate,+ endDate,+ });function getFunnelQuery(steps: { type: string; value: string }[],@@ -204,6 +216,7 @@ async function clickhouseQuery(WITH level0 AS (select distinct session_id, url_path, referrer_path, event_name, created_atfrom website_event+ ${cohortQuery}where (${stepFilterQuery})and website_id = {websiteId:UUID}and created_at between {startDate:DateTime64} and {endDate:DateTime64}diff --git a/src/queries/sql/reports/getGoal.ts b/src/queries/sql/reports/getGoal.tsindex c659bb470..3ecce7ac6 100644--- a/src/queries/sql/reports/getGoal.ts+++ b/src/queries/sql/reports/getGoal.ts@@ -1,4 +1,5 @@import clickhouse from '@/lib/clickhouse';+import { EVENT_TYPE } from '@/lib/constants';import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';import prisma from '@/lib/prisma';import { QueryFilters } from '@/lib/types';@@ -28,16 +29,16 @@ async function relationalQuery() {const { startDate, endDate, type, value } = parameters;const { rawQuery, parseFilters } = prisma;- const { filterQuery, dateQuery, queryParams } = parseFilters({+ const eventType = type === 'page' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;+ const column = type === 'page' ? 'url_path' : 'event_name';+ const { filterQuery, dateQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,value,startDate,endDate,+ eventType,});- const isPage = type === 'page';- const column = isPage ? 'url_path' : 'event_name';- const eventType = isPage ? 1 : 2;return rawQuery(`@@ -45,16 +46,19 @@ async function relationalQuery((select count(distinct session_id)from website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {websiteId:UUID}- and event_type = ${eventType}${dateQuery}+ ${filterQuery}) as totalfrom website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {websiteId:UUID}- and event_type = ${eventType}- and ${column} = {value:String}- ${dateQuery}- ${filterQuery}+ and ${column} = {value:String}+ ${dateQuery}+ ${filterQuery}`,queryParams,);@@ -67,16 +71,16 @@ async function clickhouseQuery() {const { startDate, endDate, type, value } = parameters;const { rawQuery, parseFilters } = clickhouse;- const { filterQuery, dateQuery, queryParams } = parseFilters({+ const eventType = type === 'page' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;+ const column = type === 'page' ? 'url_path' : 'event_name';+ const { filterQuery, dateQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,value,startDate,endDate,+ eventType,});- const isPage = type === 'page';- const column = isPage ? 'url_path' : 'event_name';- const eventType = isPage ? 1 : 2;return rawQuery(`@@ -84,15 +88,17 @@ async function clickhouseQuery((select count(distinct session_id)from website_event+ ${cohortQuery}where website_id = {websiteId:UUID}- ${dateQuery}+ ${dateQuery}+ ${filterQuery}) as totalfrom website_event+ ${cohortQuery}where website_id = {websiteId:UUID}- and event_type = ${eventType}- and ${column} = {value:String}- ${dateQuery}- ${filterQuery}+ and ${column} = {value:String}+ ${dateQuery}+ ${filterQuery}`,queryParams,).then(results => results?.[0]);diff --git a/src/queries/sql/reports/getJourney.ts b/src/queries/sql/reports/getJourney.tsindex e7ab14ad7..3ed4acc95 100644--- a/src/queries/sql/reports/getJourney.ts+++ b/src/queries/sql/reports/getJourney.ts@@ -43,7 +43,12 @@ async function relationalQuery(startStep,endStep,);- const { filterQuery, queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate });+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ startDate,+ endDate,+ });function getJourneyQuery(steps: number,@@ -117,6 +122,8 @@ async function relationalQuery(coalesce(nullIf(event_name, ''), url_path) event,row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_numberfrom website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}),${filterQuery}@@ -148,7 +155,12 @@ async function clickhouseQuery(startStep,endStep,);- const { filterQuery, queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate });+ const { filterQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ startDate,+ endDate,+ });function getJourneyQuery(steps: number,@@ -221,6 +233,7 @@ async function clickhouseQuery(coalesce(nullIf(event_name, ''), url_path) event,row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_numberfrom website_event+ ${cohortQuery}where website_id = {websiteId:UUID}${filterQuery}and created_at between {startDate:DateTime64} and {endDate:DateTime64}),diff --git a/src/queries/sql/reports/getRetention.ts b/src/queries/sql/reports/getRetention.tsindex c552ae457..67c373f83 100644--- a/src/queries/sql/reports/getRetention.ts+++ b/src/queries/sql/reports/getRetention.ts@@ -35,7 +35,7 @@ async function relationalQuery(const { getDateSQL, getDayDiffQuery, getCastColumnQuery, rawQuery, parseFilters } = prisma;const unit = 'day';- const { filterQuery, queryParams } = parseFilters({+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,startDate,@@ -54,11 +54,13 @@ async function relationalQuery(),user_activities AS (select distinct- w.session_id,- ${getDayDiffQuery(getDateSQL('created_at', unit, timezone), 'c.cohort_date')} as day_number- from website_event w- join cohort_items c- on w.session_id = c.session_id+ website_event.session_id,+ ${getDayDiffQuery(getDateSQL('created_at', unit, timezone), 'cohort_items.cohort_date')} as day_number+ from website_event+ join cohort_items+ on website_event.session_id = cohort_items.session_id+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}${filterQuery}@@ -104,7 +106,7 @@ async function clickhouseQuery(const { getDateSQL, rawQuery, parseFilters } = clickhouse;const unit = 'day';- const { filterQuery, queryParams } = parseFilters({+ const { filterQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,startDate,@@ -125,11 +127,12 @@ async function clickhouseQuery(),user_activities AS (select distinct- w.session_id,- (${getDateSQL('created_at', unit, timezone)} - c.cohort_date) / 86400 as day_number- from website_event w- join cohort_items c- on w.session_id = c.session_id+ website_event.session_id,+ (${getDateSQL('created_at', unit, timezone)} - cohort_items.cohort_date) / 86400 as day_number+ from website_event+ join cohort_items+ on website_event.session_id = cohort_items.session_id+ ${cohortQuery}where website_id = {websiteId:UUID}and created_at between {startDate:DateTime64} and {endDate:DateTime64}${filterQuery}diff --git a/src/queries/sql/reports/getRevenue.ts b/src/queries/sql/reports/getRevenue.tsindex 20d77297d..f5c652aa8 100644--- a/src/queries/sql/reports/getRevenue.ts+++ b/src/queries/sql/reports/getRevenue.ts@@ -38,18 +38,33 @@ async function relationalQuery(): Promise<RevenueResult> {const { startDate, endDate, currency, unit = 'day' } = parameters;const { getDateSQL, rawQuery, parseFilters } = prisma;- const { queryParams } = parseFilters({ ...filters, websiteId, startDate, endDate, currency });+ const { queryParams, filterQuery, cohortQuery, joinSessionQuery } = parseFilters({+ ...filters,+ websiteId,+ startDate,+ endDate,+ currency,+ });const chart = await rawQuery(`select- event_name x,- ${getDateSQL('created_at', unit)} t,- sum(revenue) y+ revenue.event_name x,+ ${getDateSQL('revenue.created_at', unit)} t,+ sum(revenue.revenue) yfrom revenue- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and currency like {{currency}}+ join website_event+ on website_event.website_id = revenue.website_id+ and website_event.session_id = revenue.session_id+ and website_event.event_id = revenue.event_id+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${cohortQuery}+ ${joinSessionQuery}+ where revenue.website_id = {{websiteId::uuid}}+ and revenue.created_at between {{startDate}} and {{endDate}}+ and revenue.currency like {{currency}}+ ${filterQuery}group by x, torder by t`,@@ -59,15 +74,24 @@ async function relationalQuery(const country = await rawQuery(`select- s.country as name,+ session.country as name,sum(r.revenue) value- from revenue r- join session s- on s.session_id = r.session_id- where r.website_id = {{websiteId::uuid}}- and r.created_at between {{startDate}} and {{endDate}}- and r.currency = {{currency}}- group by s.country+ from revenue+ join website_event+ on website_event.website_id = revenue.website_id+ and website_event.session_id = revenue.session_id+ and website_event.event_id = revenue.event_id+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ join session+ on session.website_id = revenue.website_id+ and session.session_id = revenue.session_id+ ${cohortQuery}+ where revenue.website_id = {{websiteId::uuid}}+ and revenue.created_at between {{startDate}} and {{endDate}}+ and revenue.currency = {{currency}}+ ${filterQuery}+ group by session.country`,queryParams,);@@ -75,13 +99,22 @@ async function relationalQuery(const total = await rawQuery(`select- sum(revenue) as sum,- count(distinct event_id) as count,- count(distinct session_id) as unique_count- from revenue r- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and currency = {{currency}}+ sum(revenue.revenue) as sum,+ count(distinct revenue.event_id) as count,+ count(distinct revenue.session_id) as unique_count+ from revenue+ join website_event+ on website_event.website_id = revenue.website_id+ and website_event.session_id = revenue.session_id+ and website_event.event_id = revenue.event_id+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${cohortQuery}+ ${joinSessionQuery}+ where revenue.website_id = {{websiteId::uuid}}+ and revenue.created_at between {{startDate}} and {{endDate}}+ and revenue.currency = {{currency}}+ ${filterQuery}`,queryParams,).then(result => result?.[0]);@@ -91,14 +124,23 @@ async function relationalQuery(const table = await rawQuery(`select- currency,- sum(revenue) as sum,- count(distinct event_id) as count,- count(distinct session_id) as unique_count- from revenue r- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- group by currency+ revenue.currency,+ sum(revenue.revenue) as sum,+ count(distinct revenue.event_id) as count,+ count(distinct revenue.session_id) as unique_count+ from revenue+ join website_event+ on website_event.website_id = revenue.website_id+ and website_event.session_id = revenue.session_id+ and website_event.event_id = revenue.event_id+ and website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${cohortQuery}+ ${joinSessionQuery}+ where revenue.website_id = {{websiteId::uuid}}+ and revenue.created_at between {{startDate}} and {{endDate}}+ ${filterQuery}+ group by revenue.currencyorder by sum desc`,queryParams,@@ -114,7 +156,7 @@ async function clickhouseQuery(): Promise<RevenueResult> {const { startDate, endDate, unit = 'day', currency } = parameters;const { getDateSQL, rawQuery, parseFilters } = clickhouse;- const { queryParams } = parseFilters({+ const { filterQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,startDate,@@ -131,13 +173,21 @@ async function clickhouseQuery(>(`select- event_name x,- ${getDateSQL('created_at', unit)} t,- sum(revenue) y+ website_revenue.event_name x,+ ${getDateSQL('website_revenue.created_at', unit)} t,+ sum(website_revenue.revenue) yfrom website_revenue- where website_id = {websiteId:UUID}- and created_at between {startDate:DateTime64} and {endDate:DateTime64}- and currency = {currency:String}+ join website_event+ on website_event.website_id = website_revenue.website_id+ and website_event.session_id = website_revenue.session_id+ and website_event.event_id = website_revenue.event_id+ and website_event.website_id = {websiteId:UUID}+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${cohortQuery}+ where website_revenue.website_id = {websiteId:UUID}+ and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and website_revenue.currency = {currency:String}+ ${filterQuery}group by x, torder by t`,@@ -152,20 +202,21 @@ async function clickhouseQuery(>(`select- s.country as name,- sum(w.revenue) as value- from website_revenue w- join (- select distinct website_id, session_id, country- from website_event- where website_id = {websiteId:UUID}- ) s- on w.website_id = s.website_id- and w.session_id = s.session_id- where w.website_id = {websiteId:UUID}- and w.created_at between {startDate:DateTime64} and {endDate:DateTime64}- and w.currency = {currency:String}- group by s.country+ website_event.country as name,+ sum(website_revenue.revenue) as value+ from website_revenue+ join website_event+ on website_event.website_id = website_revenue.website_id+ and website_event.session_id = website_revenue.session_id+ and website_event.event_id = website_revenue.event_id+ and website_event.website_id = {websiteId:UUID}+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${cohortQuery}+ where website_revenue.website_id = {websiteId:UUID}+ and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and website_revenue.currency = {currency:String}+ ${filterQuery}+ group by website_event.country`,queryParams,);@@ -177,13 +228,21 @@ async function clickhouseQuery(}>(`select- sum(revenue) as sum,- uniqExact(event_id) as count,- uniqExact(session_id) as unique_count+ sum(website_revenue.revenue) as sum,+ uniqExact(website_revenue.event_id) as count,+ uniqExact(website_revenue.session_id) as unique_countfrom website_revenue- where website_id = {websiteId:UUID}- and created_at between {startDate:DateTime64} and {endDate:DateTime64}- and currency = {currency:String}+ join website_event+ on website_event.website_id = website_revenue.website_id+ and website_event.session_id = website_revenue.session_id+ and website_event.event_id = website_revenue.event_id+ and website_event.website_id = {websiteId:UUID}+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${cohortQuery}+ where website_revenue.website_id = {websiteId:UUID}+ and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and website_revenue.currency = {currency:String}+ ${filterQuery}`,queryParams,).then(result => result?.[0]);@@ -200,14 +259,22 @@ async function clickhouseQuery(>(`select- currency,- sum(revenue) as sum,- uniqExact(event_id) as count,- uniqExact(session_id) as unique_count+ website_revenue.currency,+ sum(website_revenue.revenue) as sum,+ uniqExact(website_revenue.event_id) as count,+ uniqExact(website_revenue.session_id) as unique_countfrom website_revenue- where website_id = {websiteId:UUID}- and created_at between {startDate:DateTime64} and {endDate:DateTime64}- group by currency+ join website_event+ on website_event.website_id = website_revenue.website_id+ and website_event.session_id = website_revenue.session_id+ and website_event.event_id = website_revenue.event_id+ and website_event.website_id = {websiteId:UUID}+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${cohortQuery}+ where website_revenue.website_id = {websiteId:UUID}+ and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${filterQuery}+ group by website_revenue.currencyorder by sum desc`,queryParams,diff --git a/src/queries/sql/reports/getUTM.ts b/src/queries/sql/reports/getUTM.tsindex fbf980218..61a3c0426 100644--- a/src/queries/sql/reports/getUTM.ts+++ b/src/queries/sql/reports/getUTM.ts@@ -27,7 +27,7 @@ async function relationalQuery(const { column, startDate, endDate } = parameters;const { parseFilters, rawQuery } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters({+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,startDate,@@ -40,6 +40,7 @@ async function relationalQuery(select ${column} utm, count(*) as viewsfrom website_event${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}and coalesce(${column}, '') != ''diff --git a/src/queries/sql/sessions/getSessionDataProperties.ts b/src/queries/sql/sessions/getSessionDataProperties.tsindex ed2bc3e47..d72a04804 100644--- a/src/queries/sql/sessions/getSessionDataProperties.ts+++ b/src/queries/sql/sessions/getSessionDataProperties.ts@@ -17,7 +17,7 @@ async function relationalQuery(filters: QueryFilters & { propertyName?: string },) {const { rawQuery, parseFilters } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters(+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId },{columns: { propertyName: 'data_key' },@@ -30,7 +30,8 @@ async function relationalQuery(data_key as "propertyName",count(distinct session_data.session_id) as "total"from website_event- ${cohortQuery}+ ${cohortQuery}+ ${joinSessionQuery}join session_dataon session_data.session_id = website_event.session_idwhere website_event.website_id = {{websiteId::uuid}}diff --git a/src/queries/sql/sessions/getSessionDataValues.ts b/src/queries/sql/sessions/getSessionDataValues.tsindex e4d04f1a8..9ff3cb45e 100644--- a/src/queries/sql/sessions/getSessionDataValues.ts+++ b/src/queries/sql/sessions/getSessionDataValues.ts@@ -17,7 +17,10 @@ async function relationalQuery(filters: QueryFilters & { propertyName?: string },) {const { rawQuery, parseFilters, getDateSQL } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`@@ -30,6 +33,7 @@ async function relationalQuery(count(distinct session_data.session_id) as "total"from website_event${cohortQuery}+ ${joinSessionQuery}join session_dataon session_data.session_id = website_event.session_idwhere website_event.website_id = {{websiteId::uuid}}diff --git a/src/queries/sql/sessions/getSessionExpandedMetrics.ts b/src/queries/sql/sessions/getSessionExpandedMetrics.tsindex 7c6d98f6f..56dba66e7 100644--- a/src/queries/sql/sessions/getSessionExpandedMetrics.ts+++ b/src/queries/sql/sessions/getSessionExpandedMetrics.ts@@ -63,7 +63,6 @@ async function relationalQuery(${joinSessionQuery}where website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}- and website_event.event_type = {{eventType}}${filterQuery}group by 1${includeCountry ? ', 3' : ''}diff --git a/src/queries/sql/sessions/getSessionMetrics.ts b/src/queries/sql/sessions/getSessionMetrics.tsindex 725c73793..9af826364 100644--- a/src/queries/sql/sessions/getSessionMetrics.ts+++ b/src/queries/sql/sessions/getSessionMetrics.ts@@ -54,7 +54,6 @@ async function relationalQuery(${joinSessionQuery}where website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}- and website_event.event_type = {{eventType}}${filterQuery}group by 1${includeCountry ? ', 3' : ''}diff --git a/src/queries/sql/sessions/getSessionStats.ts b/src/queries/sql/sessions/getSessionStats.tsindex 497dc80db..62ef44802 100644--- a/src/queries/sql/sessions/getSessionStats.ts+++ b/src/queries/sql/sessions/getSessionStats.ts@@ -26,11 +26,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${getDateSQL('website_event.created_at', unit, timezone)} x,count(distinct website_event.session_id) yfrom website_event- ${joinSessionQuery}- ${cohortQuery}+ ${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 1order by 1diff --git a/src/queries/sql/sessions/getWebsiteSessionStats.ts b/src/queries/sql/sessions/getWebsiteSessionStats.tsindex 5718037fd..804a483b5 100644--- a/src/queries/sql/sessions/getWebsiteSessionStats.ts+++ b/src/queries/sql/sessions/getWebsiteSessionStats.ts@@ -26,7 +26,10 @@ async function relationalQuery(filters: QueryFilters,): Promise<WebsiteSessionStatsData[]> {const { parseFilters, rawQuery } = prisma;- const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });+ const { filterQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`diff --git a/src/queries/sql/sessions/getWebsiteSessionsWeekly.ts b/src/queries/sql/sessions/getWebsiteSessionsWeekly.tsindex 895967371..9bae54258 100644--- a/src/queries/sql/sessions/getWebsiteSessionsWeekly.ts+++ b/src/queries/sql/sessions/getWebsiteSessionsWeekly.ts@@ -14,9 +14,12 @@ export async function getWebsiteSessionsWeekly(}async function relationalQuery(websiteId: string, filters: QueryFilters) {- const { timezone = 'utc' } = filters;+ const timezone = 'utc';const { rawQuery, getDateWeeklySQL, parseFilters } = prisma;- const { queryParams } = parseFilters(filters);+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({+ ...filters,+ websiteId,+ });return rawQuery(`@@ -24,8 +27,11 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${getDateWeeklySQL('created_at', timezone)} as time,count(distinct session_id) as valuefrom website_event+ ${cohortQuery}+ ${joinSessionQuery}where website_id = {{websiteId::uuid}}and created_at between {{startDate}} and {{endDate}}+ ${filterQuery}group by timeorder by 2`,