From ab08fcb00fda6603017bdca2ec269ff0138c04ec Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Wed, 12 Feb 2025 13:20:54 -0800 Subject: [PATCH] fix pageview metrics / channel metrics --- src/queries/sql/getChannelMetrics.ts | 2 +- .../sql/pageviews/getPageviewMetrics.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/queries/sql/getChannelMetrics.ts b/src/queries/sql/getChannelMetrics.ts index a2223870..9141290e 100644 --- a/src/queries/sql/getChannelMetrics.ts +++ b/src/queries/sql/getChannelMetrics.ts @@ -21,7 +21,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) { referrer_query as query, count(distinct session_id) as visitors from website_event - where website_id = {websiteId:UUID} + where website_id = {{websiteId::uuid}} ${filterQuery} ${dateQuery} group by 1, 2 diff --git a/src/queries/sql/pageviews/getPageviewMetrics.ts b/src/queries/sql/pageviews/getPageviewMetrics.ts index 344a5e94..19a9b467 100644 --- a/src/queries/sql/pageviews/getPageviewMetrics.ts +++ b/src/queries/sql/pageviews/getPageviewMetrics.ts @@ -32,15 +32,17 @@ async function relationalQuery( websiteId, { ...filters, + eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView, }, - { joinSession: SESSION_COLUMNS.includes(type) }, + { joinSession: SESSION_COLUMNS.includes(type) || column === 'referrer_domain' }, ); let entryExitQuery = ''; let excludeDomain = ''; + if (column === 'referrer_domain') { - excludeDomain = `and website_event.referrer_domain != website_event.hostname - and website_event.referrer_domain is not null`; + excludeDomain = `and website_event.referrer_domain != session.hostname + and website_event.referrer_domain != ''`; } if (type === 'entry' || type === 'exit') { @@ -53,6 +55,7 @@ async function relationalQuery( from website_event where website_event.website_id = {{websiteId::uuid}} and website_event.created_at between {{startDate}} and {{endDate}} + and event_type = {{eventType}} group by visit_id ) x on x.visit_id = website_event.visit_id @@ -63,7 +66,7 @@ async function relationalQuery( return rawQuery( ` select ${column} x, - ${column === 'referrer_domain' ? 'count(distinct session_id)' : 'count(*)'} as y + ${column === 'referrer_domain' ? 'count(distinct website_event.session_id)' : 'count(*)'} as y from website_event ${joinSession} ${entryExitQuery} @@ -102,7 +105,7 @@ async function clickhouseQuery( let entryExitQuery = ''; if (column === 'referrer_domain') { - excludeDomain = `and referrer_domain != hostname and hostname != ''`; + excludeDomain = `and referrer_domain != hostname and referrer_domain != ''`; } if (type === 'entry' || type === 'exit') { @@ -114,6 +117,7 @@ async function clickhouseQuery( from website_event where website_id = {websiteId:UUID} and created_at between {startDate:DateTime64} and {endDate:DateTime64} + and event_type = {eventType:UInt32} group by visit_id) x ON x.visit_id = website_event.visit_id and x.target_created_at = website_event.created_at`; @@ -126,6 +130,7 @@ async function clickhouseQuery( ${entryExitQuery} where website_id = {websiteId:UUID} and created_at between {startDate:DateTime64} and {endDate:DateTime64} + and event_type = {eventType:UInt32} ${excludeDomain} ${filterQuery} group by x @@ -138,7 +143,7 @@ async function clickhouseQuery( let columnQuery = `arrayJoin(${column})`; if (column === 'referrer_domain') { - excludeDomain = `and t != hostname and hostname != ''`; + excludeDomain = `and t != hostname and t != ''`; columnQuery = `session_id s, arrayJoin(${column})`; } @@ -162,6 +167,7 @@ async function clickhouseQuery( from website_event_stats_hourly website_event where website_id = {websiteId:UUID} and created_at between {startDate:DateTime64} and {endDate:DateTime64} + and event_type = {eventType:UInt32} ${excludeDomain} ${filterQuery} ${groupByQuery}) as g