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.

update queries for event / session properties and values screens

Francis Cao917916c2247d
diff --git a/src/components/common/TypeIcon.tsx b/src/components/common/TypeIcon.tsx
index bb99c2f7f..d617e7592 100644
--- a/src/components/common/TypeIcon.tsx
+++ b/src/components/common/TypeIcon.tsx
@@ -13,7 +13,7 @@ export function TypeIcon({
<>
<img
src={`${process.env.basePath || ''}/images/${type}/${
- value.replaceAll(' ', '-').toLowerCase() || 'unknown'
+ value?.replaceAll(' ', '-').toLowerCase() || 'unknown'
}.png`}
alt={value}
width={type === 'country' ? undefined : 16}
diff --git a/src/queries/analytics/events/getEventDataProperties.ts b/src/queries/analytics/events/getEventDataProperties.ts
index cdf76c3f5..e2cf08288 100644
--- a/src/queries/analytics/events/getEventDataProperties.ts
+++ b/src/queries/analytics/events/getEventDataProperties.ts
@@ -24,15 +24,15 @@ async function relationalQuery(
return rawQuery(
`
select
- we.event_name as "eventName",
- ed.data_key as "propertyName",
+ website_event.event_name as "eventName",
+ event_data.data_key as "propertyName",
count(*) as "total"
- from event_data ed
- join website_event we on we.event_id = ed.website_event_id
- where ed.website_id = {{websiteId::uuid}}
- and ed.created_at between {{startDate}} and {{endDate}}
+ from event_data
+ join website_event on website_event.event_id = event_data.website_event_id
+ where event_data.website_id = {{websiteId::uuid}}
+ and event_data.created_at between {{startDate}} and {{endDate}}
${filterQuery}
- group by we.event_name, ed.data_key
+ group by website_event.event_name, event_data.data_key
order by 3 desc
limit 500
`,
diff --git a/src/queries/analytics/events/getEventDataValues.ts b/src/queries/analytics/events/getEventDataValues.ts
index f6b36b566..07e423075 100644
--- a/src/queries/analytics/events/getEventDataValues.ts
+++ b/src/queries/analytics/events/getEventDataValues.ts
@@ -19,21 +19,26 @@ async function relationalQuery(
websiteId: string,
filters: QueryFilters & { eventName?: string; propertyName?: string },
) {
- const { rawQuery, parseFilters } = prisma;
+ const { rawQuery, parseFilters, getDateSQL } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, filters);
return rawQuery(
`
select
- string_value as "value",
+ case
+ when data_type = 2 then replace(string_value, '.0000', '')
+ when data_type = 4 then ${getDateSQL('date_value', 'hour')}
+ else string_value
+ end as "value",
count(*) as "total"
from event_data
- where website_id = {{websiteId::uuid}}
- and created_at between {{startDate}} and {{endDate}}
- and data_key = {{propertyName}}
- and event_key = {{eventName}}
+ join website_event on website_event.event_id = event_data.website_event_id
+ where event_data.website_id = {{websiteId::uuid}}
+ and event_data.created_at between {{startDate}} and {{endDate}}
+ and event_data.data_key = {{propertyName}}
+ and website_event.event_name = {{eventName}}
${filterQuery}
- group by string_value
+ group by value
order by 2 desc
limit 100
`,
diff --git a/src/queries/analytics/sessions/getSessionDataValues.ts b/src/queries/analytics/sessions/getSessionDataValues.ts
index d9a7eb596..c02e4adb0 100644
--- a/src/queries/analytics/sessions/getSessionDataValues.ts
+++ b/src/queries/analytics/sessions/getSessionDataValues.ts
@@ -16,20 +16,24 @@ async function relationalQuery(
websiteId: string,
filters: QueryFilters & { propertyName?: string },
) {
- const { rawQuery, parseFilters } = prisma;
+ const { rawQuery, parseFilters, getDateSQL } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, filters);
return rawQuery(
`
select
- string_value as "value",
+ case
+ when data_type = 2 then replace(string_value, '.0000', '')
+ when data_type = 4 then ${getDateSQL('date_value', 'hour')}
+ else string_value
+ end as "value",
count(*) as "total"
from session_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key = {{propertyName}}
${filterQuery}
- group by string_value
+ group by value
order by 2 desc
limit 100
`,
diff --git a/src/queries/analytics/sessions/getWebsiteSessions.ts b/src/queries/analytics/sessions/getWebsiteSessions.ts
index 81e007ca0..d2a827d0e 100644
--- a/src/queries/analytics/sessions/getWebsiteSessions.ts
+++ b/src/queries/analytics/sessions/getWebsiteSessions.ts
@@ -22,29 +22,39 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
`
with sessions as (
select
- s.session_id as "id",
- s.website_id as "websiteId",
- hostname,
- browser,
- os,
- device,
- screen,
- language,
- country,
- subdivision1,
- city,
- min(we.created_at) as "firstAt",
- max(we.created_at) as "lastAt",
- count(distinct we.visit_id) as "visits",
- sum(case when we.event_type = 1 then 1 else 0 end) as "views",
- max(we.created_at) as "createdAt"
- from website_event we
- join session s on s.session_id = we.session_id
- where we.website_id = {{websiteId::uuid}}
- and we.created_at between {{startDate}} and {{endDate}}
+ session.session_id as "id",
+ session.website_id as "websiteId",
+ session.hostname,
+ session.browser,
+ session.os,
+ session.device,
+ session.screen,
+ session.language,
+ session.country,
+ session.subdivision1,
+ session.city,
+ min(website_event.created_at) as "firstAt",
+ max(website_event.created_at) as "lastAt",
+ count(distinct website_event.visit_id) as "visits",
+ sum(case when website_event.event_type = 1 then 1 else 0 end) as "views",
+ max(website_event.created_at) as "createdAt"
+ from website_event
+ join session on session.session_id = website_event.session_id
+ where website_event.website_id = {{websiteId::uuid}}
+ and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery}
- group by s.session_id, s.website_id, s.hostname, s.browser, s.os, s.device, s.screen, s.language, s.country, s.subdivision1, s.city
- order by max(we.created_at) desc
+ group by session.session_id,
+ session.website_id,
+ session.hostname,
+ session.browser,
+ session.os,
+ session.device,
+ session.screen,
+ session.language,
+ session.country,
+ session.subdivision1,
+ session.city
+ order by max(website_event.created_at) desc
limit 1000)
select * from sessions
`,