Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
resolve psql/mysql attribution bugs and optimize queries
Francis Cao88d658f72e50
diff --git a/src/queries/sql/reports/getAttribution.ts b/src/queries/sql/reports/getAttribution.tsindex b6bf25b33..aba987f62 100644--- a/src/queries/sql/reports/getAttribution.ts+++ b/src/queries/sql/reports/getAttribution.ts@@ -51,15 +51,16 @@ async function relationalQuery(function getUTMQuery(utmColumn: string) {return `select- we.${utmColumn} name,+ coalesce(we.${utmColumn}, '') name,${currency ? 'sum(e.value)' : 'count(distinct we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_id- ${currency ? '' : `where we.${utmColumn} != ''`}+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {{websiteId::uuid}}+ and we.created_at between {{startDate}} and {{endDate}}+ ${currency ? '' : `and we.${utmColumn} != ''`}group by 1order by 2 desclimit 20`;@@ -94,6 +95,7 @@ async function relationalQuery(on currency.website_event_id = ed.website_event_idwhere ed.website_id = {{websiteId::uuid}}and ed.created_at between {{startDate}} and {{endDate}}+ and ${column} = {{conversionStep}}and ed.data_key ${like} '%revenue%'group by 1),`;@@ -105,6 +107,8 @@ async function relationalQuery(from events ejoin website_event weon we.session_id = e.session_id+ where we.website_id = {{websiteId::uuid}}+ and we.created_at between {{startDate}} and {{endDate}}group by e.session_id)`: `\nmodel AS (select e.session_id,@@ -112,7 +116,9 @@ async function relationalQuery(from events ejoin website_event weon we.session_id = e.session_id- where we.created_at < e.max_dt+ where we.website_id = {{websiteId::uuid}}+ and we.created_at between {{startDate}} and {{endDate}}+ and we.created_at < e.max_dtgroup by e.session_id)`;}@@ -120,20 +126,21 @@ async function relationalQuery(`${currency ? revenueEventQuery : eventQuery}${getModelQuery(model)}- select we.referrer_domain name,+ select coalesce(we.referrer_domain, '') name,${currency ? 'sum(e.value)' : 'count(distinct we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_idjoin session s- on s.session_id = e.session_id+ on s.session_id = m.session_id+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {{websiteId::uuid}}+ and we.created_at between {{startDate}} and {{endDate}}${currency? ''- : `where referrer_domain != hostname+ : `and we.referrer_domain != hostnameand we.referrer_domain != ''`}group by 1@@ -156,14 +163,16 @@ async function relationalQuery(when coalesce(ttclid, '') != '' then 'TikTok Ads'when coalesce(li_fat_id, '') != '' then 'LinkedIn Ads'when coalesce(twclid, '') != '' then 'Twitter Ads (X)'+ else ''end name,${currency ? 'sum(e.value)' : 'count(distinct we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_id+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {{websiteId::uuid}}+ and we.created_at between {{startDate}} and {{endDate}}group by 1order by 2 desclimit 20)@@ -276,13 +285,14 @@ async function clickhouseQuery(selectwe.${utmColumn} name,${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_id- ${currency ? '' : `where we.${utmColumn} != ''`}+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {websiteId:UUID}+ and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${currency ? '' : `and we.${utmColumn} != ''`}group by 1order by 2 desclimit 20`;@@ -326,6 +336,8 @@ async function clickhouseQuery(from events ejoin website_event weon we.session_id = e.session_id+ where we.website_id = {websiteId:UUID}+ and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}group by e.session_id)`: `\nmodel AS (select e.session_id,@@ -333,7 +345,9 @@ async function clickhouseQuery(from events ejoin website_event weon we.session_id = e.session_id- where we.created_at < e.max_dt+ where we.website_id = {websiteId:UUID}+ and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ and we.created_at < e.max_dtgroup by e.session_id)`;}@@ -348,16 +362,17 @@ async function clickhouseQuery(${getModelQuery(model)}select we.referrer_domain name,${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_id+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {websiteId:UUID}+ and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}${currency? ''- : `where referrer_domain != hostname+ : `and we.referrer_domain != hostnameand we.referrer_domain != ''`}group by 1@@ -380,16 +395,17 @@ async function clickhouseQuery(fbclid != '', 'Facebook / Meta',msclkid != '', 'Microsoft Ads',ttclid != '', 'TikTok Ads',- li_fat_id != '', 'LinkedIn Ads',+ li_fat_id != '', ' LinkedIn Ads',twclid != '', 'Twitter Ads (X)','') name,${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value- from events e- join model m- on m.session_id = e.session_id+ from model mjoin website_event weon we.created_at = m.created_atand we.session_id = m.session_id- ${currency ? '' : `WHERE name != ''`}+ ${currency ? 'join events e on e.session_id = m.session_id' : ''}+ where we.website_id = {websiteId:UUID}+ and we.created_at between {startDate:DateTime64} and {endDate:DateTime64}+ ${currency ? '' : `and name != ''`}group by 1order by 2 desclimit 20