From 414854b064fb7e1e5c4f74d77dfe64cd531603f2 Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Tue, 20 Jun 2023 10:22:12 -0700 Subject: [PATCH] Fix funnel report. --- lib/prisma.ts | 7 +++++-- queries/analytics/pageview/getPageviewFunnel.ts | 11 +++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index 51707049..910c1c71 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -137,12 +137,13 @@ function getFilterQuery(filters = {}, params = []): string { function getFunnelQuery( urls: string[], windowMinutes: number, - initParamLength = 3, ): { levelQuery: string; sumQuery: string; urlFilterQuery: string; } { + const initParamLength = 3; + return urls.reduce( (pv, cv, i) => { const levelNumber = i + 1; @@ -155,12 +156,14 @@ function getFunnelQuery( l0.created_at level_${levelNumber}_created_at, l0.url_path as level_${levelNumber}_url from level${i} cl - left join level0 l0 + left join website_event l0 on cl.session_id = l0.session_id and l0.created_at between cl.level_${i}_created_at and ${getAddMinutesQuery(`cl.level_${i}_created_at`, windowMinutes)} and l0.referrer_path = $${i + initParamLength} and l0.url_path = $${levelNumber + initParamLength} + and created_at between $2 and $3 + and website_id = $1${toUuid()} )`; } diff --git a/queries/analytics/pageview/getPageviewFunnel.ts b/queries/analytics/pageview/getPageviewFunnel.ts index ef62e526..7305e545 100644 --- a/queries/analytics/pageview/getPageviewFunnel.ts +++ b/queries/analytics/pageview/getPageviewFunnel.ts @@ -41,23 +41,23 @@ async function relationalQuery( return rawQuery( `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 where url_path in (${urlFilterQuery}) and website_id = $1${toUuid()} and created_at between $2 and $3 ),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 where url_path = $4 )${levelQuery} SELECT ${sumQuery} - from level3; + from level${urls.length}; `, params, ).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 FROM ( SELECT session_id, - windowFunnel({window:UInt32}, 'strict_order') + windowFunnel({window:UInt32}) ( created_at ${columnsQuery} @@ -99,7 +99,6 @@ async function clickhouseQuery( FROM website_event WHERE website_id = {websiteId:UUID} and ${getBetweenDates('created_at', startDate, endDate)} - AND (url_path in [${conditionQuery}]) GROUP BY 1 ) GROUP BY level