Skip to content

armgit / umami

Public Imported from GitHub
0

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.

udpate oss attribution logic

Francis Caoe648b4178fed
diff --git a/src/queries/sql/reports/getAttribution.ts b/src/queries/sql/reports/getAttribution.ts
index 62ad068a5..b6bf25b33 100644
--- a/src/queries/sql/reports/getAttribution.ts
+++ b/src/queries/sql/reports/getAttribution.ts
@@ -1,6 +1,6 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
-import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
+import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
export async function getAttribution(
@@ -45,14 +45,14 @@ async function relationalQuery(
const conversionStep = steps[0].value;
const eventType = steps[0].type === 'url' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;
const column = steps[0].type === 'url' ? 'url_path' : 'event_name';
- //const db = getDatabaseType();
- //const like = db === POSTGRESQL ? 'ilike' : 'like';
+ const db = getDatabaseType();
+ const like = db === POSTGRESQL ? 'ilike' : 'like';
function getUTMQuery(utmColumn: string) {
return `
select
we.${utmColumn} name,
- ${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
+ ${currency ? 'sum(e.value)' : 'count(distinct we.session_id)'} value
from events e
join model m
on m.session_id = e.session_id
@@ -70,29 +70,31 @@ async function relationalQuery(
session_id,
max(created_at) max_dt
from website_event
- where website_id = {websiteId:UUID}
- and created_at between {startDate:DateTime64} and {endDate:DateTime64}
- and ${column} = {conversionStep:String}
- and event_type = {eventType:UInt32}
+ where website_id = {{websiteId::uuid}}
+ and created_at between {{startDate}} and {{endDate}}
+ and ${column} = {{conversionStep}}
+ and event_type = {{eventType}}
group by 1),`;
const revenueEventQuery = `WITH events AS (
select
- ed.session_id,
+ we.session_id,
max(ed.created_at) max_dt,
- sum(coalesce(toDecimal64(number_value, 2), toDecimal64(string_value, 2))) as value
+ sum(coalesce(cast(number_value as decimal(10,2)), cast(string_value as decimal(10,2)))) value
from event_data ed
- join (select event_id
+ join website_event we
+ on we.event_id = ed.website_event_Id
+ and we.website_id = ed.website_id
+ join (select website_event_id
from event_data
- where website_id = {websiteId:UUID}
- and created_at between {startDate:DateTime64} and {endDate:DateTime64}
- and positionCaseInsensitive(data_key, 'currency') > 0
- and string_value = {currency:String}) c
- on c.event_id = ed.event_id
- where website_id = {websiteId:UUID}
- and created_at between {startDate:DateTime64} and {endDate:DateTime64}
- and ${column} = {conversionStep:String}
- and positionCaseInsensitive(ed.data_key, 'revenue') > 0
+ where website_id = {{websiteId::uuid}}
+ and created_at between {{startDate}} and {{endDate}}
+ and data_key ${like} '%currency%'
+ and string_value = {{currency}}) currency
+ on currency.website_event_id = ed.website_event_id
+ where ed.website_id = {{websiteId::uuid}}
+ and ed.created_at between {{startDate}} and {{endDate}}
+ and ed.data_key ${like} '%revenue%'
group by 1),`;
function getModelQuery(model: string) {
@@ -119,13 +121,15 @@ async function relationalQuery(
${currency ? revenueEventQuery : eventQuery}
${getModelQuery(model)}
select we.referrer_domain name,
- ${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
+ ${currency ? 'sum(e.value)' : 'count(distinct we.session_id)'} value
from events e
join model m
on m.session_id = e.session_id
join website_event we
on we.created_at = m.created_at
and we.session_id = m.session_id
+ join session s
+ on s.session_id = e.session_id
${
currency
? ''
@@ -142,19 +146,30 @@ async function relationalQuery(
const paidAdsres = await rawQuery(
`
${currency ? revenueEventQuery : eventQuery}
- ${getModelQuery(model)}
- select multiIf(gclid != '', 'Google', fbclid != '', 'Facebook', '') name,
- ${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
+ ${getModelQuery(model)},
+
+ results AS (
+ select case
+ when coalesce(gclid, '') != '' then 'Google Ads'
+ when coalesce(fbclid, '') != '' then 'Facebook / Meta'
+ when coalesce(msclkid, '') != '' then 'Microsoft Ads'
+ when coalesce(ttclid, '') != '' then 'TikTok Ads'
+ when coalesce(li_fat_id, '') != '' then 'LinkedIn Ads'
+ when coalesce(twclid, '') != '' then 'Twitter Ads (X)'
+ 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
join website_event we
on we.created_at = m.created_at
and we.session_id = m.session_id
- ${currency ? '' : `WHERE name != ''`}
group by 1
order by 2 desc
- limit 20
+ limit 20)
+ SELECT *
+ FROM results
+ ${currency ? '' : `WHERE name != ''`}
`,
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
);
@@ -208,13 +223,13 @@ async function relationalQuery(
`
select
count(*) as "pageviews",
- uniqExact(session_id) as "visitors",
- uniqExact(visit_id) as "visits"
+ count(distinct session_id) as "visitors",
+ count(distinct visit_id) as "visits"
from website_event
- where website_id = {websiteId:UUID}
- and created_at between {startDate:DateTime64} and {endDate:DateTime64}
- and ${column} = {conversionStep:String}
- and event_type = {eventType:UInt32}
+ where website_id = {{websiteId::uuid}}
+ and created_at between {{startDate}} and {{endDate}}
+ and ${column} = {{conversionStep}}
+ and event_type = {{eventType}}
`,
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
).then(result => result?.[0]);
@@ -365,7 +380,7 @@ 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