Fix funnel report.

This commit is contained in:
Brian Cao 2023-06-20 10:22:12 -07:00
parent 56f3134936
commit 414854b064
2 changed files with 10 additions and 8 deletions

View File

@ -137,12 +137,13 @@ function getFilterQuery(filters = {}, params = []): string {
function getFunnelQuery( function getFunnelQuery(
urls: string[], urls: string[],
windowMinutes: number, windowMinutes: number,
initParamLength = 3,
): { ): {
levelQuery: string; levelQuery: string;
sumQuery: string; sumQuery: string;
urlFilterQuery: string; urlFilterQuery: string;
} { } {
const initParamLength = 3;
return urls.reduce( return urls.reduce(
(pv, cv, i) => { (pv, cv, i) => {
const levelNumber = i + 1; const levelNumber = i + 1;
@ -155,12 +156,14 @@ function getFunnelQuery(
l0.created_at level_${levelNumber}_created_at, l0.created_at level_${levelNumber}_created_at,
l0.url_path as level_${levelNumber}_url l0.url_path as level_${levelNumber}_url
from level${i} cl from level${i} cl
left join level0 l0 left join website_event l0
on cl.session_id = l0.session_id on cl.session_id = l0.session_id
and l0.created_at between cl.level_${i}_created_at and l0.created_at between cl.level_${i}_created_at
and ${getAddMinutesQuery(`cl.level_${i}_created_at`, windowMinutes)} and ${getAddMinutesQuery(`cl.level_${i}_created_at`, windowMinutes)}
and l0.referrer_path = $${i + initParamLength} and l0.referrer_path = $${i + initParamLength}
and l0.url_path = $${levelNumber + initParamLength} and l0.url_path = $${levelNumber + initParamLength}
and created_at between $2 and $3
and website_id = $1${toUuid()}
)`; )`;
} }

View File

@ -41,23 +41,23 @@ async function relationalQuery(
return rawQuery( return rawQuery(
`WITH level0 AS ( `WITH level0 AS (
select session_id, url_path, referrer_path, created_at select distinct session_id, url_path, referrer_path, created_at
from website_event from website_event
where url_path in (${urlFilterQuery}) where url_path in (${urlFilterQuery})
and website_id = $1${toUuid()} and website_id = $1${toUuid()}
and created_at between $2 and $3 and created_at between $2 and $3
),level1 AS ( ),level1 AS (
select session_id, url_path as level_1_url, created_at as level_1_created_at select distinct session_id, url_path as level_1_url, created_at as level_1_created_at
from level0 from level0
where url_path = $4 where url_path = $4
)${levelQuery} )${levelQuery}
SELECT ${sumQuery} SELECT ${sumQuery}
from level3; from level${urls.length};
`, `,
params, params,
).then((a: { [key: string]: number }) => { ).then((a: { [key: string]: number }) => {
return urls.map((b, i) => ({ x: b, y: a[`level${i + 1}`] || 0 })); return urls.map((b, i) => ({ x: b, y: a[0][`level${i + 1}`] || 0 }));
}); });
} }
@ -91,7 +91,7 @@ async function clickhouseQuery(
count(*) AS count count(*) AS count
FROM ( FROM (
SELECT session_id, SELECT session_id,
windowFunnel({window:UInt32}, 'strict_order') windowFunnel({window:UInt32})
( (
created_at created_at
${columnsQuery} ${columnsQuery}
@ -99,7 +99,6 @@ async function clickhouseQuery(
FROM website_event FROM website_event
WHERE website_id = {websiteId:UUID} WHERE website_id = {websiteId:UUID}
and ${getBetweenDates('created_at', startDate, endDate)} and ${getBetweenDates('created_at', startDate, endDate)}
AND (url_path in [${conditionQuery}])
GROUP BY 1 GROUP BY 1
) )
GROUP BY level GROUP BY level