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
v3 prisma queries update
Francis Cao2ffde37f5b0f
diff --git a/src/lib/prisma.ts b/src/lib/prisma.tsindex 220987c79..d441bcec1 100644--- a/src/lib/prisma.ts+++ b/src/lib/prisma.ts@@ -118,6 +118,7 @@ function getCohortQuery(filters: QueryFilters = {}) {(select distinct website_event.session_idfrom website_eventjoin session on session.session_id = website_event.session_id+ and session.website_id = website_event.website_idwhere website_event.website_id = {{websiteId}}and website_event.created_at between {{cohort_startDate}} and {{cohort_endDate}}${filterQuery}@@ -165,7 +166,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {return {joinSessionQuery:options?.joinSession || joinSession- ? `inner join session on website_event.session_id = session.session_id`+ ? `inner join session on website_event.session_id = session.session_id and website_event.website_id = session.website_id`: '',dateQuery: getDateQuery(filters),filterQuery: getFilterQuery(filters, options),@@ -225,8 +226,8 @@ async function pagedQuery<T>(model: string, criteria: T, filters?: QueryFilters)async function pagedRawQuery(query: string,- filters: QueryFilters,queryParams: Record<string, any>,+ filters: QueryFilters,name?: string,) {const { page = 1, pageSize, orderBy, sortDescending = false } = filters;diff --git a/src/queries/sql/events/getEventData.ts b/src/queries/sql/events/getEventData.tsindex 52cb53004..42dc2040b 100644--- a/src/queries/sql/events/getEventData.ts+++ b/src/queries/sql/events/getEventData.ts@@ -19,17 +19,17 @@ async function relationalQuery(websiteId: string, eventId: string) {return rawQuery(`- select website_id as websiteId,- session_id as sessionId,- event_id as eventId,- url_path as urlPath,- event_name as eventName,- data_key as dataKey,- string_value as stringValue,- number_value as numberValue,- date_value as dateValue,- data_type as dataType,- created_at as createdAt+ select website_id as "websiteId",+ session_id as "sessionId",+ event_id as "eventId",+ url_path as "urlPath",+ event_name as "eventName",+ data_key as "dataKey",+ string_value as "stringValue",+ number_value as "numberValue",+ date_value as "dateValue",+ data_type as "dataType",+ created_at as "createdAt"from event_datawebsite_id = {{websiteId::uuid}}event_id = {{eventId::uuid}}diff --git a/src/queries/sql/events/getWebsiteEvents.ts b/src/queries/sql/events/getWebsiteEvents.tsindex 52113daf5..ef4f300c0 100644--- a/src/queries/sql/events/getWebsiteEvents.ts+++ b/src/queries/sql/events/getWebsiteEvents.ts@@ -18,7 +18,6 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {const { filterQuery, dateQuery, cohortQuery, queryParams } = parseFilters({...filters,websiteId,- search: `%${search}%`,});const searchQuery = search@@ -29,32 +28,33 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {return pagedRawQuery(`select- event_id as "id",- website_id as "websiteId",- session_id as "sessionId",- created_at as "createdAt",- hostname,- url_path as "urlPath",- url_query as "urlQuery",- referrer_path as "referrerPath",- referrer_query as "referrerQuery",- referrer_domain as "referrerDomain",- country as country,+ website_event.event_id as "id",+ website_event.website_id as "websiteId",+ website_event.session_id as "sessionId",+ website_event.created_at as "createdAt",+ website_event.hostname,+ website_event.url_path as "urlPath",+ website_event.url_query as "urlQuery",+ website_event.referrer_path as "referrerPath",+ website_event.referrer_query as "referrerQuery",+ website_event.referrer_domain as "referrerDomain",+ session.country as country,city as city,device as device,os as os,browser as browser,page_title as "pageTitle",- event_type as "eventType",- event_name as "eventName"+ website_event.event_type as "eventType",+ website_event.event_name as "eventName"from website_event${cohortQuery}- join session on website_event.session_id = session.session_id- where website_id = {{websiteId::uuid}}+ join session on session.session_id = website_event.session_id+ and session.website_id = website_event.website_id+ where website_event.website_id = {{websiteId::uuid}}${dateQuery}${filterQuery}${searchQuery}- order by created_at desc+ order by website_event.created_at desc`,queryParams,filters,diff --git a/src/queries/sql/getActiveVisitors.ts b/src/queries/sql/getActiveVisitors.tsindex 693efd63c..20327424d 100644--- a/src/queries/sql/getActiveVisitors.ts+++ b/src/queries/sql/getActiveVisitors.ts@@ -18,7 +18,7 @@ async function relationalQuery(websiteId: string) {const result = await rawQuery(`- select count(distinct session_id) as visitors+ select count(distinct session_id) as "visitors"from website_eventwhere website_id = {{websiteId::uuid}}and created_at >= {{startDate}}diff --git a/src/queries/sql/getChannelExpandedMetrics.ts b/src/queries/sql/getChannelExpandedMetrics.tsindex 4515ddd65..5ce884ad0 100644--- a/src/queries/sql/getChannelExpandedMetrics.ts+++ b/src/queries/sql/getChannelExpandedMetrics.ts@@ -49,19 +49,19 @@ async function relationalQuery(return rawQuery(`WITH prefix AS (- select case when utm_medium LIKE 'p%' OR- utm_medium LIKE '%ppc%' OR- utm_medium LIKE '%retargeting%' OR- utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,- referrer_domain,- url_query,- utm_medium,- utm_source,- session_id,- visit_id,+ select case when website_event.utm_medium LIKE 'p%' OR+ website_event.utm_medium LIKE '%ppc%' OR+ website_event.utm_medium LIKE '%retargeting%' OR+ website_event.utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,+ website_event.referrer_domain,+ website_event.url_query,+ website_event.utm_medium,+ website_event.utm_source,+ website_event.session_id,+ website_event.visit_id,count(*) c,- min(created_at) min_time,- max(created_at) max_time+ min(website_event.created_at) min_time,+ max(website_event.created_at) max_timefrom website_event${cohortQuery}${joinSessionQuery}@@ -70,12 +70,12 @@ async function relationalQuery(${dateQuery}${filterQuery}group by prefix,- referrer_domain,- url_query,- utm_medium,- utm_source,- session_id,- visit_id),+ website_event.referrer_domain,+ website_event.url_query,+ website_event.utm_medium,+ website_event.utm_source,+ website_event.session_id,+ website_event.visit_id),channels as (select casediff --git a/src/queries/sql/getChannelMetrics.ts b/src/queries/sql/getChannelMetrics.tsindex 592ed2123..a608ec5ef 100644--- a/src/queries/sql/getChannelMetrics.ts+++ b/src/queries/sql/getChannelMetrics.ts@@ -30,15 +30,15 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {return rawQuery(`WITH prefix AS (- select case when utm_medium LIKE 'p%' OR- utm_medium LIKE '%ppc%' OR- utm_medium LIKE '%retargeting%' OR- utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,- referrer_domain,- url_query,- utm_medium,- utm_source,- session_id+ select case when website_event.utm_medium LIKE 'p%' OR+ website_event.utm_medium LIKE '%ppc%' OR+ website_event.utm_medium LIKE '%retargeting%' OR+ website_event.utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,+ website_event.referrer_domain,+ website_event.url_query,+ website_event.utm_medium,+ website_event.utm_source,+ website_event.session_idfrom website_event${cohortQuery}${joinSessionQuery}diff --git a/src/queries/sql/getRealtimeActivity.ts b/src/queries/sql/getRealtimeActivity.tsindex ec9a104fe..d7aa06e93 100644--- a/src/queries/sql/getRealtimeActivity.ts+++ b/src/queries/sql/getRealtimeActivity.ts@@ -35,6 +35,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${cohortQuery}inner join sessionon session.session_id = website_event.session_id+ and session.website_id = website_event.website_idwhere website_event.website_id = {{websiteId::uuid}}${filterQuery}${dateQuery}diff --git a/src/queries/sql/getValues.ts b/src/queries/sql/getValues.tsindex 426731560..6251fe3b8 100644--- a/src/queries/sql/getValues.ts+++ b/src/queries/sql/getValues.ts@@ -51,6 +51,7 @@ async function relationalQuery(websiteId: string, column: string, filters: Queryfrom website_eventinner join sessionon session.session_id = website_event.session_id+ and session.website_id = website_event.website_idwhere website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${searchQuery}diff --git a/src/queries/sql/getWebsiteDateRange.ts b/src/queries/sql/getWebsiteDateRange.tsindex 2f7f9f190..58d8ab25e 100644--- a/src/queries/sql/getWebsiteDateRange.ts+++ b/src/queries/sql/getWebsiteDateRange.ts@@ -20,8 +20,8 @@ async function relationalQuery(websiteId: string) {const result = await rawQuery(`select- min(created_at) as startDate,- max(created_at) as endDate+ min(created_at) as "startDate",+ max(created_at) as "endDate"from website_eventwhere website_id = {{websiteId::uuid}}and created_at >= {{startDate}}diff --git a/src/queries/sql/getWeeklyTraffic.ts b/src/queries/sql/getWeeklyTraffic.tsindex 2ea370670..ea2029935 100644--- a/src/queries/sql/getWeeklyTraffic.ts+++ b/src/queries/sql/getWeeklyTraffic.ts@@ -24,13 +24,13 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {return rawQuery(`select- ${getDateWeeklySQL('created_at', timezone)} as time,- count(distinct session_id) as value+ ${getDateWeeklySQL('website_event.created_at', timezone)} as time,+ count(distinct website_event.session_id) as valuefrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}${filterQuery}group by timeorder by 2diff --git a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts b/src/queries/sql/pageviews/getPageviewExpandedMetrics.tsindex c5f467dc1..2e234ab17 100644--- a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts+++ b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts@@ -103,6 +103,7 @@ async function relationalQuery(${filterQuery}group by name, website_event.session_id, website_event.visit_id) as t+ where name != ''group by nameorder by visitors desc, visits desclimit ${limit}diff --git a/src/queries/sql/reports/getAttribution.ts b/src/queries/sql/reports/getAttribution.tsindex 3db2ff087..9277bbf7b 100644--- a/src/queries/sql/reports/getAttribution.ts+++ b/src/queries/sql/reports/getAttribution.ts@@ -69,14 +69,14 @@ async function relationalQuery(const eventQuery = `WITH events AS (select distinct- session_id,- max(created_at) max_dt+ website_event.session_id,+ max(website_event.created_at) max_dtfrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and ${column} = {{step}}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ and website_event.${column} = {{step}}${filterQuery}group by 1),`;@@ -234,14 +234,14 @@ async function relationalQuery(`selectcount(*) as "pageviews",- count(distinct session_id) as "visitors",- count(distinct visit_id) as "visits"+ count(distinct website_event.session_id) as "visitors",+ count(distinct website_event.visit_id) as "visits"from website_event${joinSessionQuery}${cohortQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and ${column} = {{step}}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ and website_event.${column} = {{step}}${filterQuery}`,queryParams,diff --git a/src/queries/sql/reports/getFunnel.ts b/src/queries/sql/reports/getFunnel.tsindex a56439dbc..ada5eeb2d 100644--- a/src/queries/sql/reports/getFunnel.ts+++ b/src/queries/sql/reports/getFunnel.ts@@ -67,12 +67,12 @@ async function relationalQuery(if (levelNumber === 1) {pv.levelOneQuery = `WITH level1 AS (- select distinct session_id, created_at+ select distinct website_event.session_id, website_event.created_atfrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}and ${column} ${operator} {{${i}}}${filterQuery})`;diff --git a/src/queries/sql/reports/getGoal.ts b/src/queries/sql/reports/getGoal.tsindex 81ec80abe..702c160ab 100644--- a/src/queries/sql/reports/getGoal.ts+++ b/src/queries/sql/reports/getGoal.ts@@ -42,26 +42,26 @@ async function relationalQuery(return rawQuery(`- select count(*) as num,+ select count(distinct website_event.session_id) as num,(- select count(distinct session_id)+ select count(distinct website_event.session_id)from website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}+ where website_event.website_id = {{websiteId::uuid}}${dateQuery}${filterQuery}) as totalfrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}+ where website_event.website_id = {{websiteId::uuid}}and ${column} = {{value}}${dateQuery}${filterQuery}`,queryParams,- );+ ).then(results => results?.[0]);}async function clickhouseQuery(@@ -84,7 +84,7 @@ async function clickhouseQuery(return rawQuery(`- select count(*) as num,+ select count(distinct session_id) as num,(select count(distinct session_id)from website_eventdiff --git a/src/queries/sql/reports/getJourney.ts b/src/queries/sql/reports/getJourney.tsindex 3ed4acc95..8d1a90d45 100644--- a/src/queries/sql/reports/getJourney.ts+++ b/src/queries/sql/reports/getJourney.ts@@ -117,16 +117,16 @@ async function relationalQuery(`WITH events AS (select distinct- visit_id,- referrer_path,- coalesce(nullIf(event_name, ''), url_path) event,- row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_number+ website_event.visit_id,+ website_event.referrer_path,+ coalesce(nullIf(website_event.event_name, ''), website_event.url_path) event,+ row_number() OVER (PARTITION BY visit_id ORDER BY website_event.created_at) AS event_numberfrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}),- ${filterQuery}+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ ${filterQuery}),${sequenceQuery}select *from sequencesdiff --git a/src/queries/sql/reports/getUTM.ts b/src/queries/sql/reports/getUTM.tsindex 61a3c0426..5dee7a7b0 100644--- a/src/queries/sql/reports/getUTM.ts+++ b/src/queries/sql/reports/getUTM.ts@@ -37,13 +37,13 @@ async function relationalQuery(return rawQuery(`- select ${column} utm, count(*) as views+ select website_event.${column} utm, count(*) as viewsfrom website_event${cohortQuery}${joinSessionQuery}- where website_id = {{websiteId::uuid}}- and created_at between {{startDate}} and {{endDate}}- and coalesce(${column}, '') != ''+ where website_event.website_id = {{websiteId::uuid}}+ and website_event.created_at between {{startDate}} and {{endDate}}+ and coalesce(website_event.${column}, '') != ''${filterQuery}group by 1order by 2 descdiff --git a/src/queries/sql/sessions/getSessionActivity.ts b/src/queries/sql/sessions/getSessionActivity.tsindex 6b79338ee..360db530a 100644--- a/src/queries/sql/sessions/getSessionActivity.ts+++ b/src/queries/sql/sessions/getSessionActivity.ts@@ -21,20 +21,23 @@ async function relationalQuery(websiteId: string, sessionId: string, filters: Qureturn rawQuery(`select- created_at as createdAt,- url_path as urlPath,- url_query as urlQuery,- referrer_domain as referrerDomain,- event_id as eventId,- event_type as eventType,- event_name as eventName,- visit_id as visitId,- event_id IN (SELECT event_id FROM event_data) AS hasData- from website_event e- where e.website_id = {websiteId:UUID}- and e.session_id = {sessionId:UUID}- and e.created_at between {startDate:DateTime64} and {endDate:DateTime64}- order by e.created_at desc+ created_at as "createdAt",+ url_path as "urlPath",+ url_query as "urlQuery",+ referrer_domain as "referrerDomain",+ event_id as "eventId",+ event_type as "eventType",+ event_name as "eventName",+ visit_id as "visitId",+ event_id IN (select event_id+ from event_data+ where website_id = {{websiteId::uuid}}+ and session_id = {{sessionId::uuid}}) AS "hasData"+ from website_event+ where website_id = {{websiteId::uuid}}+ and session_id = {{sessionId::uuid}}+ and created_at between {{startDate}} and {{endDate}}+ order by created_at desclimit 500`,{ websiteId, sessionId, startDate, endDate },diff --git a/src/queries/sql/sessions/getSessionDataProperties.ts b/src/queries/sql/sessions/getSessionDataProperties.tsindex 78e4cba4a..96cc17bf6 100644--- a/src/queries/sql/sessions/getSessionDataProperties.ts+++ b/src/queries/sql/sessions/getSessionDataProperties.ts@@ -31,6 +31,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {${joinSessionQuery}join session_dataon session_data.session_id = website_event.session_id+ and session_data.website_id = website_event.website_idwhere website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${filterQuery}diff --git a/src/queries/sql/sessions/getSessionDataValues.ts b/src/queries/sql/sessions/getSessionDataValues.tsindex efc85090b..0093575d8 100644--- a/src/queries/sql/sessions/getSessionDataValues.ts+++ b/src/queries/sql/sessions/getSessionDataValues.ts@@ -38,6 +38,7 @@ async function relationalQuery(${joinSessionQuery}join session_dataon session_data.session_id = website_event.session_id+ and session_data.website_id = website_event.website_idwhere website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}and session_data.data_key = {{propertyName}}diff --git a/src/queries/sql/sessions/getWebsiteSessionStats.ts b/src/queries/sql/sessions/getWebsiteSessionStats.tsindex be90d4cd2..2e824f0d0 100644--- a/src/queries/sql/sessions/getWebsiteSessionStats.ts+++ b/src/queries/sql/sessions/getWebsiteSessionStats.ts@@ -44,6 +44,7 @@ async function relationalQuery(from website_event${cohortQuery}join session on website_event.session_id = session.session_id+ and website_event.website_id = session.website_idwhere website_event.website_id = {{websiteId::uuid}}and website_event.created_at between {{startDate}} and {{endDate}}${filterQuery}diff --git a/src/queries/sql/sessions/getWebsiteSessions.ts b/src/queries/sql/sessions/getWebsiteSessions.tsindex 302deca6e..5ebd1740b 100644--- a/src/queries/sql/sessions/getWebsiteSessions.ts+++ b/src/queries/sql/sessions/getWebsiteSessions.ts@@ -52,6 +52,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {from website_event${cohortQuery}join session on session.session_id = website_event.session_id+ and session.website_id = website_event.website_idwhere website_event.website_id = {{websiteId::uuid}}${dateQuery}${filterQuery}