umami/queries/analytics/reports/getFunnel.ts

113 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-05-09 08:46:58 +02:00
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
export async function getFunnel(
2023-05-09 08:46:58 +02:00
...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-12 01:42:58 +02:00
): Promise<
{
2023-05-18 20:17:35 +02:00
x: string;
y: number;
2023-05-12 01:42:58 +02:00
}[]
> {
2023-05-09 08:46:58 +02:00
const { windowMinutes, startDate, endDate, urls } = criteria;
2023-07-25 08:06:16 +02:00
const { rawQuery, getFunnelQuery } = prisma;
2023-05-09 08:46:58 +02:00
const { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes);
return rawQuery(
`WITH level0 AS (
2023-06-20 19:22:12 +02:00
select distinct session_id, url_path, referrer_path, created_at
2023-05-12 01:42:58 +02:00
from website_event
where url_path in (${urlFilterQuery})
2023-07-25 08:06:16 +02:00
and website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
2023-05-12 01:42:58 +02:00
),level1 AS (
2023-06-20 19:22:12 +02:00
select distinct session_id, url_path as level_1_url, created_at as level_1_created_at
2023-05-12 01:42:58 +02:00
from level0
where url_path = $4
)${levelQuery}
SELECT ${sumQuery}
2023-06-20 19:22:12 +02:00
from level${urls.length};
2023-05-12 01:42:58 +02:00
`,
2023-07-25 08:06:16 +02:00
{ websiteId, startDate, endDate, ...urls },
2023-05-12 01:42:58 +02:00
).then((a: { [key: string]: number }) => {
2023-06-20 19:22:12 +02:00
return urls.map((b, i) => ({ x: b, y: a[0][`level${i + 1}`] || 0 }));
2023-05-12 01:42:58 +02:00
});
2023-05-09 08:46:58 +02:00
}
async function clickhouseQuery(
websiteId: string,
criteria: {
windowMinutes: number;
startDate: Date;
endDate: Date;
urls: string[];
},
2023-05-12 01:42:58 +02:00
): Promise<
{
2023-05-18 20:17:35 +02:00
x: string;
y: number;
2023-05-12 01:42:58 +02:00
}[]
> {
2023-05-09 08:46:58 +02:00
const { windowMinutes, startDate, endDate, urls } = criteria;
const { rawQuery, getFunnelQuery } = clickhouse;
const { columnsQuery, urlParams } = getFunnelQuery(urls);
2023-05-09 08:46:58 +02:00
2023-05-12 01:42:58 +02:00
return rawQuery<{ level: number; count: number }[]>(
2023-05-09 08:46:58 +02:00
`
SELECT level,
count(*) AS count
FROM (
SELECT session_id,
2023-07-11 21:43:43 +02:00
windowFunnel({window:UInt32}, 'strict_increase')
2023-05-09 08:46:58 +02:00
(
2023-05-12 01:42:58 +02:00
created_at
2023-05-09 08:46:58 +02:00
${columnsQuery}
) AS level
FROM website_event
WHERE website_id = {websiteId:UUID}
AND created_at BETWEEN {startDate:DateTime} AND {endDate:DateTime}
2023-05-09 08:46:58 +02:00
GROUP BY 1
)
GROUP BY level
ORDER BY level ASC;
`,
{
websiteId,
startDate,
endDate,
window: windowMinutes * 60,
...urlParams,
},
2023-05-15 23:03:42 +02:00
).then(results => {
return urls.map((a, i) => ({
2023-05-18 20:17:35 +02:00
x: a,
y: results[i + 1]?.count || 0,
2023-05-15 23:03:42 +02:00
}));
2023-05-12 01:42:58 +02:00
});
2023-05-09 08:46:58 +02:00
}