implement filtering and cohorts to all relevant queries

This commit is contained in:
Francis Cao 2025-08-19 15:26:41 -07:00
parent 789b8b36d8
commit 2f1f704728
34 changed files with 311 additions and 167 deletions

View file

@ -7,7 +7,7 @@ import { BoardAddButton } from './BoardAddButton';
export function BoardsPage() {
return (
<PageBody>
<Column>
<Column margin="2">
<PageHeader title="My Boards">
<BoardAddButton />
</PageHeader>

View file

@ -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>

View file

@ -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>

View file

@ -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>

View file

@ -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: Q
async 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(
`

View file

@ -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}

View file

@ -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_data
join 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}

View file

@ -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}}

View file

@ -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 1
order by 2 desc

View file

@ -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 1
order by 2 desc

View file

@ -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, 2
order by 2

View file

@ -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_id
where 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 hasData
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}

View file

@ -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) y
from website_event
${cohortQuery}
${joinSessionQuery}
where website_id = {{websiteId::uuid}}
and event_type = {{eventType}}
${dateQuery}
${filterQuery}
group by 1, 2

View file

@ -21,7 +21,7 @@ export async function getChannelMetrics(...args: [websiteId: string, filters?: Q
async 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) y
from website_event
${cohortQuery}
${joinSessionQuery}
where website_id = {{websiteId::uuid}}
and event_type = {{eventType}}
${dateQuery}
${filterQuery}
group by 1, 2

View file

@ -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(
`

View file

@ -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 t

View file

@ -76,12 +76,11 @@ async function relationalQuery(
select ${column} x,
count(distinct website_event.session_id) as y
from 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 1

View file

@ -73,12 +73,11 @@ async function relationalQuery(
select ${column} x,
count(distinct website_event.session_id) as y
from 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 1

View file

@ -26,11 +26,10 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
${getDateSQL('website_event.created_at', unit, timezone)} x,
count(*) y
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
order by 1

View file

@ -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_dt
from 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) value
from 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_id
where 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_dt
from 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 value
from 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}

View file

@ -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_id

View file

@ -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_at
from 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_at
from website_event
${cohortQuery}
where (${stepFilterQuery})
and website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}

View file

@ -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 total
from 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 total
from 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]);

View file

@ -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_number
from 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_number
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}
${filterQuery}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}),

View file

@ -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}

View file

@ -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) y
from 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, t
order 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.currency
order 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) y
from 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, t
order 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_count
from 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_count
from 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.currency
order by sum desc
`,
queryParams,

View file

@ -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 views
from website_event
${cohortQuery}
${joinSessionQuery}
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and coalesce(${column}, '') != ''

View file

@ -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_data
on session_data.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}

View file

@ -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_data
on session_data.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}

View file

@ -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' : ''}

View file

@ -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' : ''}

View file

@ -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) y
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
order by 1

View file

@ -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(
`

View file

@ -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 value
from website_event
${cohortQuery}
${joinSessionQuery}
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by time
order by 2
`,