umami/queries/analytics/pageview/getPageviewFunnel.ts

120 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-05-09 06:46:58 +00:00
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
export async function getPageviewFunnel(
...args: [
websiteId: string,
criteria: {
windowMinutes: number;
startDate: Date;
endDate: Date;
urls: string[];
},
]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
criteria: {
windowMinutes: number;
startDate: Date;
endDate: Date;
urls: string[];
},
2023-05-11 23:42:58 +00:00
): Promise<
{
2023-05-18 18:17:35 +00:00
x: string;
y: number;
z: number;
2023-05-11 23:42:58 +00:00
}[]
> {
2023-05-09 06:46:58 +00:00
const { windowMinutes, startDate, endDate, urls } = criteria;
2023-07-24 18:57:46 +00:00
const { rawQuery, getFunnelQuery, toUuid } = prisma;
2023-05-09 06:46:58 +00:00
const { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes);
const params: any = [websiteId, startDate, endDate, ...urls];
return rawQuery(
`WITH level0 AS (
2023-06-20 17:22:12 +00:00
select distinct session_id, url_path, referrer_path, created_at
2023-05-11 23:42:58 +00:00
from website_event
where url_path in (${urlFilterQuery})
and website_id = $1${toUuid()}
and created_at between $2 and $3
2023-07-24 18:57:46 +00:00
),level1 AS (
select distinct session_id, created_at
from level0
where url_path = $4
)${levelQuery}
${sumQuery}
ORDER BY level;`,
2023-05-09 06:46:58 +00:00
params,
).then(results => {
return urls.map((a, i) => ({
x: a,
y: results[i]?.count || 0,
2023-07-24 18:57:46 +00:00
z: (1 - (Number(results[i]?.count) * 1.0) / Number(results[i - 1]?.count)) * 100 || 0, // drop off
}));
2023-05-11 23:42:58 +00:00
});
2023-05-09 06:46:58 +00:00
}
async function clickhouseQuery(
websiteId: string,
criteria: {
windowMinutes: number;
startDate: Date;
endDate: Date;
urls: string[];
},
2023-05-11 23:42:58 +00:00
): Promise<
{
2023-05-18 18:17:35 +00:00
x: string;
y: number;
2023-05-11 23:42:58 +00:00
}[]
> {
2023-05-09 06:46:58 +00:00
const { windowMinutes, startDate, endDate, urls } = criteria;
const { rawQuery, getBetweenDates, getFunnelQuery } = clickhouse;
2023-07-21 04:13:29 +00:00
const { columnsQuery, urlParams } = getFunnelQuery(urls);
2023-05-09 06:46:58 +00:00
const params = {
websiteId,
window: windowMinutes * 60,
...urlParams,
};
2023-05-11 23:42:58 +00:00
return rawQuery<{ level: number; count: number }[]>(
2023-05-09 06:46:58 +00:00
`
2023-07-24 18:57:46 +00:00
WITH funnel as (select level,
2023-05-09 06:46:58 +00:00
count(*) AS count
2023-07-21 04:13:29 +00:00
from (
select session_id,
2023-07-11 19:43:43 +00:00
windowFunnel({window:UInt32}, 'strict_increase')
2023-05-09 06:46:58 +00:00
(
2023-05-11 23:42:58 +00:00
created_at
2023-05-09 06:46:58 +00:00
${columnsQuery}
) AS level
2023-07-21 04:13:29 +00:00
from website_event
where website_id = {websiteId:UUID}
2023-05-09 06:46:58 +00:00
and ${getBetweenDates('created_at', startDate, endDate)}
2023-07-21 04:13:29 +00:00
group by 1
2023-05-09 06:46:58 +00:00
)
2023-07-21 04:13:29 +00:00
group by level
2023-07-24 18:57:46 +00:00
order by level asc)
select * from funnel where level > 0;
2023-05-09 06:46:58 +00:00
`,
params,
2023-05-15 21:03:42 +00:00
).then(results => {
return urls.map((a, i) => ({
2023-05-18 18:17:35 +00:00
x: a,
2023-07-24 18:57:46 +00:00
y: results[i]?.count || 0,
z: (1 - (Number(results[i]?.count) * 1.0) / Number(results[i - 1]?.count)) * 100 || 0, // drop off
2023-05-15 21:03:42 +00:00
}));
2023-05-11 23:42:58 +00:00
});
2023-05-09 06:46:58 +00:00
}