mirror of
https://github.com/umami-software/umami.git
synced 2026-02-11 16:17:13 +01:00
udpate oss attribution logic
This commit is contained in:
parent
b192d90aaf
commit
e648b4178f
1 changed files with 48 additions and 33 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
import { EVENT_TYPE } from '@/lib/constants';
|
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';
|
import prisma from '@/lib/prisma';
|
||||||
|
|
||||||
export async function getAttribution(
|
export async function getAttribution(
|
||||||
|
|
@ -45,14 +45,14 @@ async function relationalQuery(
|
||||||
const conversionStep = steps[0].value;
|
const conversionStep = steps[0].value;
|
||||||
const eventType = steps[0].type === 'url' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;
|
const eventType = steps[0].type === 'url' ? EVENT_TYPE.pageView : EVENT_TYPE.customEvent;
|
||||||
const column = steps[0].type === 'url' ? 'url_path' : 'event_name';
|
const column = steps[0].type === 'url' ? 'url_path' : 'event_name';
|
||||||
//const db = getDatabaseType();
|
const db = getDatabaseType();
|
||||||
//const like = db === POSTGRESQL ? 'ilike' : 'like';
|
const like = db === POSTGRESQL ? 'ilike' : 'like';
|
||||||
|
|
||||||
function getUTMQuery(utmColumn: string) {
|
function getUTMQuery(utmColumn: string) {
|
||||||
return `
|
return `
|
||||||
select
|
select
|
||||||
we.${utmColumn} name,
|
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
|
from events e
|
||||||
join model m
|
join model m
|
||||||
on m.session_id = e.session_id
|
on m.session_id = e.session_id
|
||||||
|
|
@ -70,29 +70,31 @@ async function relationalQuery(
|
||||||
session_id,
|
session_id,
|
||||||
max(created_at) max_dt
|
max(created_at) max_dt
|
||||||
from website_event
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {{websiteId::uuid}}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {{startDate}} and {{endDate}}
|
||||||
and ${column} = {conversionStep:String}
|
and ${column} = {{conversionStep}}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {{eventType}}
|
||||||
group by 1),`;
|
group by 1),`;
|
||||||
|
|
||||||
const revenueEventQuery = `WITH events AS (
|
const revenueEventQuery = `WITH events AS (
|
||||||
select
|
select
|
||||||
ed.session_id,
|
we.session_id,
|
||||||
max(ed.created_at) max_dt,
|
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
|
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
|
from event_data
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {{websiteId::uuid}}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {{startDate}} and {{endDate}}
|
||||||
and positionCaseInsensitive(data_key, 'currency') > 0
|
and data_key ${like} '%currency%'
|
||||||
and string_value = {currency:String}) c
|
and string_value = {{currency}}) currency
|
||||||
on c.event_id = ed.event_id
|
on currency.website_event_id = ed.website_event_id
|
||||||
where website_id = {websiteId:UUID}
|
where ed.website_id = {{websiteId::uuid}}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and ed.created_at between {{startDate}} and {{endDate}}
|
||||||
and ${column} = {conversionStep:String}
|
and ed.data_key ${like} '%revenue%'
|
||||||
and positionCaseInsensitive(ed.data_key, 'revenue') > 0
|
|
||||||
group by 1),`;
|
group by 1),`;
|
||||||
|
|
||||||
function getModelQuery(model: string) {
|
function getModelQuery(model: string) {
|
||||||
|
|
@ -119,13 +121,15 @@ async function relationalQuery(
|
||||||
${currency ? revenueEventQuery : eventQuery}
|
${currency ? revenueEventQuery : eventQuery}
|
||||||
${getModelQuery(model)}
|
${getModelQuery(model)}
|
||||||
select we.referrer_domain name,
|
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
|
from events e
|
||||||
join model m
|
join model m
|
||||||
on m.session_id = e.session_id
|
on m.session_id = e.session_id
|
||||||
join website_event we
|
join website_event we
|
||||||
on we.created_at = m.created_at
|
on we.created_at = m.created_at
|
||||||
and we.session_id = m.session_id
|
and we.session_id = m.session_id
|
||||||
|
join session s
|
||||||
|
on s.session_id = e.session_id
|
||||||
${
|
${
|
||||||
currency
|
currency
|
||||||
? ''
|
? ''
|
||||||
|
|
@ -142,19 +146,30 @@ async function relationalQuery(
|
||||||
const paidAdsres = await rawQuery(
|
const paidAdsres = await rawQuery(
|
||||||
`
|
`
|
||||||
${currency ? revenueEventQuery : eventQuery}
|
${currency ? revenueEventQuery : eventQuery}
|
||||||
${getModelQuery(model)}
|
${getModelQuery(model)},
|
||||||
select multiIf(gclid != '', 'Google', fbclid != '', 'Facebook', '') name,
|
|
||||||
${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
|
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
|
from events e
|
||||||
join model m
|
join model m
|
||||||
on m.session_id = e.session_id
|
on m.session_id = e.session_id
|
||||||
join website_event we
|
join website_event we
|
||||||
on we.created_at = m.created_at
|
on we.created_at = m.created_at
|
||||||
and we.session_id = m.session_id
|
and we.session_id = m.session_id
|
||||||
${currency ? '' : `WHERE name != ''`}
|
|
||||||
group by 1
|
group by 1
|
||||||
order by 2 desc
|
order by 2 desc
|
||||||
limit 20
|
limit 20)
|
||||||
|
SELECT *
|
||||||
|
FROM results
|
||||||
|
${currency ? '' : `WHERE name != ''`}
|
||||||
`,
|
`,
|
||||||
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
|
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
|
||||||
);
|
);
|
||||||
|
|
@ -208,13 +223,13 @@ async function relationalQuery(
|
||||||
`
|
`
|
||||||
select
|
select
|
||||||
count(*) as "pageviews",
|
count(*) as "pageviews",
|
||||||
uniqExact(session_id) as "visitors",
|
count(distinct session_id) as "visitors",
|
||||||
uniqExact(visit_id) as "visits"
|
count(distinct visit_id) as "visits"
|
||||||
from website_event
|
from website_event
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {{websiteId::uuid}}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {{startDate}} and {{endDate}}
|
||||||
and ${column} = {conversionStep:String}
|
and ${column} = {{conversionStep}}
|
||||||
and event_type = {eventType:UInt32}
|
and event_type = {{eventType}}
|
||||||
`,
|
`,
|
||||||
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
|
{ websiteId, startDate, endDate, conversionStep, eventType, currency },
|
||||||
).then(result => result?.[0]);
|
).then(result => result?.[0]);
|
||||||
|
|
@ -365,7 +380,7 @@ async function clickhouseQuery(
|
||||||
fbclid != '', 'Facebook / Meta',
|
fbclid != '', 'Facebook / Meta',
|
||||||
msclkid != '', 'Microsoft Ads',
|
msclkid != '', 'Microsoft Ads',
|
||||||
ttclid != '', 'TikTok Ads',
|
ttclid != '', 'TikTok Ads',
|
||||||
li_fat_id != '', ' LinkedIn Ads',
|
li_fat_id != '', 'LinkedIn Ads',
|
||||||
twclid != '', 'Twitter Ads (X)','') name,
|
twclid != '', 'Twitter Ads (X)','') name,
|
||||||
${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
|
${currency ? 'sum(e.value)' : 'uniqExact(we.session_id)'} value
|
||||||
from events e
|
from events e
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue