clean-up session property string charts and insights.
This commit is contained in:
1 parent
1f2bd7cad9
commit
d5c48ee75f
15 files changed
+381
-260
No files matched your search
@@ -48,7 +48,7 @@ export * from './sessions/getSessionDataPropertySeries';
|
||||
export * from './sessions/getSessionDataNumericSeries';
|
||||
export * from './sessions/getSessionDataNumericStats';
|
||||
export * from './sessions/getSessionDataPivot';
|
||||
export * from './sessions/getSessionDataStats';
|
||||
export * from './sessions/getSessionDataActivityStats';
|
||||
export * from './sessions/getSessionDataValues';
|
||||
export * from './sessions/getSessionExpandedMetrics';
|
||||
export * from './sessions/getSessionMetrics';
|
||||
|
||||
+4
-8
@@ -4,9 +4,9 @@ import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { PropertyFilter, PropertyLeaderboardRow, QueryFilters } from '@/lib/types';
|
||||
|
||||
const FUNCTION_NAME = 'getSessionDataStats';
|
||||
const FUNCTION_NAME = 'getSessionDataActivityStats';
|
||||
|
||||
export async function getSessionDataStats(
|
||||
export async function getSessionDataActivityStats(
|
||||
...args: [websiteId: string, propertyName: string, filters: QueryFilters, propertyFilters?: PropertyFilter[]]
|
||||
): Promise<PropertyLeaderboardRow[]> {
|
||||
return runQuery({
|
||||
@@ -90,9 +90,7 @@ async function relationalQuery(
|
||||
coalesce(sum(session_stats.visits), 0) as visits,
|
||||
coalesce(sum(session_stats.views), 0) as views,
|
||||
coalesce(sum(session_stats.events), 0) as events,
|
||||
coalesce(sum(session_stats.totaltime), 0) as totaltime,
|
||||
coalesce(sum(case when session_stats.visits = 1 then 1 else 0 end), 0) as "newSessions",
|
||||
coalesce(sum(case when session_stats.visits > 1 then 1 else 0 end), 0) as "returningSessions"
|
||||
coalesce(sum(session_stats.totaltime), 0) as totaltime
|
||||
from property_values
|
||||
join session_stats on session_stats.session_id = property_values.session_id
|
||||
group by property_values.value
|
||||
@@ -170,9 +168,7 @@ async function clickhouseQuery(
|
||||
ifNull(sum(session_stats.visits), 0) as visits,
|
||||
ifNull(sum(session_stats.views), 0) as views,
|
||||
ifNull(sum(session_stats.events), 0) as events,
|
||||
ifNull(sum(session_stats.totaltime), 0) as totaltime,
|
||||
ifNull(sum(if(session_stats.visits = 1, 1, 0)), 0) as newSessions,
|
||||
ifNull(sum(if(session_stats.visits > 1, 1, 0)), 0) as returningSessions
|
||||
ifNull(sum(session_stats.totaltime), 0) as totaltime
|
||||
from property_values
|
||||
join session_stats on session_stats.session_id = property_values.session_id
|
||||
group by property_values.value
|
||||
@@ -55,16 +55,28 @@ async function relationalQuery(
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
selected_property_sessions as (
|
||||
select distinct session_data.session_id
|
||||
from session_data
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {{websiteId::uuid}}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
latest_session_properties as (
|
||||
select
|
||||
ranked.session_id,
|
||||
ranked.data_key
|
||||
from (
|
||||
select
|
||||
session_data.session_id,
|
||||
session_data.data_key,
|
||||
row_number() over (
|
||||
partition by session_data.session_id, session_data.data_key
|
||||
order by session_data.created_at desc, session_data.session_data_id desc
|
||||
) as row_num
|
||||
from session_data
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {{websiteId::uuid}}
|
||||
) ranked
|
||||
where ranked.row_num = 1
|
||||
)
|
||||
select count(*) as num
|
||||
from selected_property_sessions
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {{propertyName}}
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
)) as { num: number }[];
|
||||
@@ -83,14 +95,6 @@ async function relationalQuery(
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
selected_property_sessions as (
|
||||
select distinct session_data.session_id
|
||||
from session_data
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {{websiteId::uuid}}
|
||||
and session_data.data_key = {{propertyName}}
|
||||
),
|
||||
latest_session_properties as (
|
||||
select
|
||||
ranked.session_id,
|
||||
@@ -116,17 +120,19 @@ async function relationalQuery(
|
||||
order by session_data.created_at desc, session_data.session_data_id desc
|
||||
) as row_num
|
||||
from session_data
|
||||
join selected_property_sessions
|
||||
on selected_property_sessions.session_id = session_data.session_id
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {{websiteId::uuid}}
|
||||
) ranked
|
||||
where ranked.row_num = 1
|
||||
),
|
||||
paged_sessions as (
|
||||
select latest_session_properties.session_id
|
||||
select
|
||||
latest_session_properties.session_id,
|
||||
latest_session_properties.created_at as sort_created_at
|
||||
from latest_session_properties
|
||||
group by latest_session_properties.session_id
|
||||
order by max(latest_session_properties.created_at) desc
|
||||
where latest_session_properties.data_key = {{propertyName}}
|
||||
order by latest_session_properties.created_at desc
|
||||
limit ${size} offset ${offset}
|
||||
)
|
||||
select
|
||||
@@ -146,9 +152,10 @@ async function relationalQuery(
|
||||
order by latest_session_properties.data_key asc
|
||||
) as "propertyValues"
|
||||
from latest_session_properties
|
||||
join paged_sessions on paged_sessions.session_id = latest_session_properties.session_id
|
||||
group by latest_session_properties.session_id
|
||||
order by max(latest_session_properties.created_at) desc
|
||||
join paged_sessions
|
||||
on paged_sessions.session_id = latest_session_properties.session_id
|
||||
group by latest_session_properties.session_id, paged_sessions.sort_created_at
|
||||
order by paged_sessions.sort_created_at desc
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
FUNCTION_NAME,
|
||||
@@ -187,15 +194,21 @@ async function clickhouseQuery(
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
selected_property_sessions as (
|
||||
select distinct session_data.session_id
|
||||
latest_session_properties as (
|
||||
select
|
||||
session_data.session_id as session_id,
|
||||
session_data.data_key as data_key
|
||||
from session_data final
|
||||
join filtered_sessions on filtered_sessions.session_id = session_data.session_id
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {websiteId:UUID}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
group by
|
||||
session_data.session_id,
|
||||
session_data.data_key
|
||||
)
|
||||
select count() as num
|
||||
from selected_property_sessions
|
||||
from latest_session_properties
|
||||
where latest_session_properties.data_key = {propertyName:String}
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
)) as { num: number }[];
|
||||
@@ -212,13 +225,6 @@ async function clickhouseQuery(
|
||||
${filterQuery}
|
||||
${pfSQL}
|
||||
),
|
||||
selected_property_sessions as (
|
||||
select distinct session_data.session_id
|
||||
from session_data final
|
||||
join filtered_sessions on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {websiteId:UUID}
|
||||
and session_data.data_key = {propertyName:String}
|
||||
),
|
||||
latest_session_properties as (
|
||||
select
|
||||
session_data.session_id as session_id,
|
||||
@@ -230,8 +236,8 @@ async function clickhouseQuery(
|
||||
argMax(session_data.date_value, session_data.created_at) as date_value,
|
||||
max(session_data.created_at) as created_at
|
||||
from session_data final
|
||||
join selected_property_sessions
|
||||
on selected_property_sessions.session_id = session_data.session_id
|
||||
join filtered_sessions
|
||||
on filtered_sessions.session_id = session_data.session_id
|
||||
where session_data.website_id = {websiteId:UUID}
|
||||
group by
|
||||
session_data.session_id,
|
||||
@@ -239,10 +245,11 @@ async function clickhouseQuery(
|
||||
),
|
||||
paged_sessions as (
|
||||
select
|
||||
latest_session_properties.session_id
|
||||
latest_session_properties.session_id,
|
||||
latest_session_properties.created_at as sort_created_at
|
||||
from latest_session_properties
|
||||
group by latest_session_properties.session_id
|
||||
order by max(latest_session_properties.created_at) desc
|
||||
where latest_session_properties.data_key = {propertyName:String}
|
||||
order by latest_session_properties.created_at desc
|
||||
limit ${size} offset ${offset}
|
||||
)
|
||||
select
|
||||
@@ -262,8 +269,9 @@ async function clickhouseQuery(
|
||||
join paged_sessions
|
||||
on paged_sessions.session_id = latest_session_properties.session_id
|
||||
group by
|
||||
latest_session_properties.session_id
|
||||
order by createdAt desc
|
||||
latest_session_properties.session_id,
|
||||
paged_sessions.sort_created_at
|
||||
order by paged_sessions.sort_created_at desc
|
||||
`,
|
||||
{ ...queryParams, websiteId, propertyName, ...pfParams },
|
||||
FUNCTION_NAME,
|
||||
|
||||
@@ -31,7 +31,7 @@ async function relationalQuery(
|
||||
`
|
||||
select
|
||||
array_item.value as "value",
|
||||
count(distinct session_data.session_id) as "total"
|
||||
count(*) as "total"
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
@@ -46,7 +46,7 @@ async function relationalQuery(
|
||||
${filterQuery}
|
||||
group by array_item.value
|
||||
order by 2 desc
|
||||
limit 100
|
||||
limit 500
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
@@ -61,7 +61,7 @@ async function relationalQuery(
|
||||
when data_type = 4 then ${getDateSQL('date_value', 'hour')}
|
||||
else string_value
|
||||
end as "value",
|
||||
count(distinct session_data.session_id) as "total"
|
||||
count(*) as "total"
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
${joinSessionQuery}
|
||||
@@ -75,7 +75,7 @@ async function relationalQuery(
|
||||
${filterQuery}
|
||||
group by value
|
||||
order by 2 desc
|
||||
limit 100
|
||||
limit 500
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
@@ -95,7 +95,7 @@ async function clickhouseQuery(
|
||||
`
|
||||
select
|
||||
arrayJoin(JSONExtract(ifNull(session_data.string_value, '[]'), 'Array(String)')) as "value",
|
||||
uniq(session_data.session_id) as "total"
|
||||
count() as "total"
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
join session_data final
|
||||
@@ -108,7 +108,7 @@ async function clickhouseQuery(
|
||||
${filterQuery}
|
||||
group by value
|
||||
order by 2 desc
|
||||
limit 100
|
||||
limit 500
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
@@ -121,7 +121,7 @@ async function clickhouseQuery(
|
||||
multiIf(data_type = 2, replaceAll(string_value, '.0000', ''),
|
||||
data_type = 4, toString(date_trunc('hour', date_value)),
|
||||
string_value) as "value",
|
||||
uniq(session_data.session_id) as "total"
|
||||
count() as "total"
|
||||
from website_event
|
||||
${cohortQuery}
|
||||
join session_data final
|
||||
@@ -134,7 +134,7 @@ async function clickhouseQuery(
|
||||
${filterQuery}
|
||||
group by value
|
||||
order by 2 desc
|
||||
limit 100
|
||||
limit 500
|
||||
`,
|
||||
queryParams,
|
||||
FUNCTION_NAME,
|
||||
|
||||
Reference in new issue
Block a user