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.

Merge branch 'dev' into bug/um-362-relational-funnels-query

Francis Caoa03574e8d4f7
diff --cc lib/prisma.ts
index 5177acc2f,b02f69f7b..2aa2193c9
--- a/lib/prisma.ts
+++ b/lib/prisma.ts
@@@ -188,16 -98,18 +146,16 @@@ function getFunnelQuery
if (levelNumber >= 2) {
pv.levelQuery += `\n
, level${levelNumber} AS (
- select cl.*,
- l0.created_at level_${levelNumber}_created_at,
- l0.url_path as level_${levelNumber}_url
- from level${i} cl
- 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
+ select distinct we.session_id, we.created_at
+ from level${i} l
+ join website_event we
+ on l.session_id = we.session_id
+ where we.created_at between l.created_at
+ and ${getAddMinutesQuery(`l.created_at `, windowMinutes)}
+ and we.referrer_path = $${i + initParamLength}
+ and we.url_path = $${levelNumber + initParamLength}
- and we.created_at <= $3
- and we.website_id = $1${toUuid()}
++ and we.created_at <= {{endDate}}
++ and we.website_id = {{websiteId}}${toUuid()}
)`;
}
@@@ -214,8 -127,25 +172,9 @@@
);
}
- async function rawQuery(query: string, params: never[] = []): Promise<any> {
- const db = getDatabaseType(process.env.DATABASE_URL);
-function parseFilters(
- filters: { [key: string]: any } = {},
- params = [],
- sessionKey = 'session_id',
-) {
- const { os, browser, device, country, region, city } = filters;
-
- return {
- joinSession:
- os || browser || device || country || region || city
- ? `inner join session on website_event.${sessionKey} = session.${sessionKey}`
- : '',
- filterQuery: getFilterQuery(filters, params),
- };
-}
-
+ async function rawQuery(sql: string, data: object): Promise<any> {
+ const db = getDatabaseType();
+ const params = [];
if (db !== POSTGRESQL && db !== MYSQL) {
return Promise.reject(new Error('Unknown database.'));
@@@ -230,12 -166,9 +195,11 @@@ export default
...prisma,
getAddMinutesQuery,
getDateQuery,
- getTimestampInterval,
+ getTimestampIntervalQuery,
getFilterQuery,
- getEventDataFilterQuery,
- getFunnelQuery,
+ toUuid,
parseFilters,
+ getFunnelParams,
+ getFunnelQuery,
rawQuery,
};
diff --cc queries/analytics/reports/getFunnel.ts
index bdfb1c5b8,1dde1a136..9e1fd123b
--- a/queries/analytics/reports/getFunnel.ts
+++ b/queries/analytics/reports/getFunnel.ts
@@@ -35,33 -34,28 +35,33 @@@ async function relationalQuery
}[]
> {
const { windowMinutes, startDate, endDate, urls } = criteria;
- const { rawQuery, getFunnelQuery } = prisma;
- const { levelQuery, sumQuery, urlFilterQuery } = getFunnelQuery(urls, windowMinutes);
+ const { rawQuery, getFunnelQuery, toUuid } = prisma;
+ const { levelQuery, sumQuery, urlParams } = getFunnelQuery(
+ urls,
+ endDate,
+ websiteId,
+ windowMinutes,
+ );
+
+ const params: any = [websiteId, startDate, endDate, ...urlParams];
return rawQuery(
- `WITH level0 AS (
- select distinct session_id, url_path, referrer_path, created_at
+ `WITH level1 AS (
+ select distinct session_id, created_at
from website_event
- where website_id = $1${toUuid()}
- and created_at between $2 and $3
- where url_path in (${urlFilterQuery})
- and website_id = {{websiteId::uuid}}
++ where website_id = {{websiteId}}${toUuid()}
+ and created_at between {{startDate}} and {{endDate}}
- ),level1 AS (
- 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 level${urls.length};
- `,
- { websiteId, startDate, endDate, ...urls },
- ).then((a: { [key: string]: number }) => {
- return urls.map((b, i) => ({ x: b, y: a[0][`level${i + 1}`] || 0 }));
+ and url_path = $4)
+ ${levelQuery}
+ ${sumQuery}
+ ORDER BY level;`,
+ params,
+ ).then(results => {
+ return urls.map((a, i) => ({
+ x: a,
+ y: results[i]?.count || 0,
+ z: (1 - (Number(results[i]?.count) * 1.0) / Number(results[i - 1]?.count)) * 100 || 0, // drop off
+ }));
});
}
@@@ -81,32 -74,35 +81,34 @@@ async function clickhouseQuery
}[]
> {
const { windowMinutes, startDate, endDate, urls } = criteria;
- const { rawQuery, getBetweenDates, getFunnelQuery } = clickhouse;
+ const { rawQuery, getFunnelQuery } = clickhouse;
- const { columnsQuery, urlParams } = getFunnelQuery(urls);
+ const { levelQuery, sumQuery, urlFilterQuery, urlParams } = getFunnelQuery(urls, windowMinutes);
+
+ const params = {
+ websiteId,
++ startDate,
++ endDate,
+ ...urlParams,
+ };
return rawQuery<{ level: number; count: number }[]>(
`
- SELECT level,
- count(*) AS count
- FROM (
- SELECT session_id,
- windowFunnel({window:UInt32}, 'strict_increase')
- (
- created_at
- ${columnsQuery}
- ) AS level
- FROM website_event
- WHERE website_id = {websiteId:UUID}
- AND created_at BETWEEN {startDate:DateTime} AND {endDate:DateTime}
- GROUP BY 1
- )
- GROUP BY level
- ORDER BY level ASC;
- `,
- {
- websiteId,
- startDate,
- endDate,
- window: windowMinutes * 60,
- ...urlParams,
- },
+ WITH level0 AS (
+ select distinct session_id, url_path, referrer_path, created_at
+ from umami.website_event
+ where url_path in (${urlFilterQuery})
+ and website_id = {websiteId:UUID}
- and ${getBetweenDates('created_at', startDate, endDate)}
++ and created_at between {startDate:DateTime64} and {endDate:DateTime64}
+ ), level1 AS (
+ select *
+ from level0
+ where url_path = {url0:String})
+ ${levelQuery}
+ select *
+ from (
+ ${sumQuery}
+ ) ORDER BY level;`,
+ params,
).then(results => {
return urls.map((a, i) => ({
x: a,