Skip to content

Commit c5efc27

Browse files
committed
distinct_id schema changes and search on sessions page
1 parent 9d3c926 commit c5efc27

11 files changed

Lines changed: 176 additions & 39 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
-- add tag column
2+
ALTER TABLE umami.website_event ADD COLUMN "distinct_id" String AFTER "tag";
3+
ALTER TABLE umami.website_event_stats_hourly ADD COLUMN "distinct_id" String AFTER "tag";
4+
ALTER TABLE umami.session_data ADD COLUMN "distinct_id" String AFTER "data_type";
5+
6+
-- update materialized view
7+
DROP TABLE umami.website_event_stats_hourly_mv;
8+
9+
CREATE MATERIALIZED VIEW umami.website_event_stats_hourly_mv
10+
TO umami.website_event_stats_hourly
11+
AS
12+
SELECT
13+
website_id,
14+
session_id,
15+
visit_id,
16+
hostname,
17+
browser,
18+
os,
19+
device,
20+
screen,
21+
language,
22+
country,
23+
region,
24+
city,
25+
entry_url,
26+
exit_url,
27+
url_paths as url_path,
28+
url_query,
29+
utm_source,
30+
utm_medium,
31+
utm_campaign,
32+
utm_content,
33+
utm_term,
34+
referrer_domain,
35+
page_title,
36+
gclid,
37+
fbclid,
38+
msclkid,
39+
ttclid,
40+
li_fat_id,
41+
twclid,
42+
event_type,
43+
event_name,
44+
views,
45+
min_time,
46+
max_time,
47+
tag,
48+
distinct_id,
49+
timestamp as created_at
50+
FROM (SELECT
51+
website_id,
52+
session_id,
53+
visit_id,
54+
hostname,
55+
browser,
56+
os,
57+
device,
58+
screen,
59+
language,
60+
country,
61+
region,
62+
city,
63+
argMinState(url_path, created_at) entry_url,
64+
argMaxState(url_path, created_at) exit_url,
65+
arrayFilter(x -> x != '', groupArray(url_path)) as url_paths,
66+
arrayFilter(x -> x != '', groupArray(url_query)) url_query,
67+
arrayFilter(x -> x != '', groupArray(utm_source)) utm_source,
68+
arrayFilter(x -> x != '', groupArray(utm_medium)) utm_medium,
69+
arrayFilter(x -> x != '', groupArray(utm_campaign)) utm_campaign,
70+
arrayFilter(x -> x != '', groupArray(utm_content)) utm_content,
71+
arrayFilter(x -> x != '', groupArray(utm_term)) utm_term,
72+
arrayFilter(x -> x != '', groupArray(referrer_domain)) referrer_domain,
73+
arrayFilter(x -> x != '', groupArray(page_title)) page_title,
74+
arrayFilter(x -> x != '', groupArray(gclid)) gclid,
75+
arrayFilter(x -> x != '', groupArray(fbclid)) fbclid,
76+
arrayFilter(x -> x != '', groupArray(msclkid)) msclkid,
77+
arrayFilter(x -> x != '', groupArray(ttclid)) ttclid,
78+
arrayFilter(x -> x != '', groupArray(li_fat_id)) li_fat_id,
79+
arrayFilter(x -> x != '', groupArray(twclid)) twclid,
80+
event_type,
81+
if(event_type = 2, groupArray(event_name), []) event_name,
82+
sumIf(1, event_type = 1) views,
83+
min(created_at) min_time,
84+
max(created_at) max_time,
85+
arrayFilter(x -> x != '', groupArray(tag)) tag,
86+
distinct_id,
87+
toStartOfHour(created_at) timestamp
88+
FROM umami.website_event
89+
GROUP BY website_id,
90+
session_id,
91+
visit_id,
92+
hostname,
93+
browser,
94+
os,
95+
device,
96+
screen,
97+
language,
98+
country,
99+
region,
100+
city,
101+
event_type,
102+
distinct_id,
103+
timestamp);

db/clickhouse/schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CREATE TABLE umami.website_event
3838
event_type UInt32,
3939
event_name String,
4040
tag String,
41+
distinct_id String,
4142
created_at DateTime('UTC'),
4243
job_id Nullable(UUID)
4344
)
@@ -75,6 +76,7 @@ CREATE TABLE umami.session_data
7576
number_value Nullable(Decimal64(4)),
7677
date_value Nullable(DateTime('UTC')),
7778
data_type UInt32,
79+
distinct_id String,
7880
created_at DateTime('UTC'),
7981
job_id Nullable(UUID)
8082
)
@@ -120,6 +122,7 @@ CREATE TABLE umami.website_event_stats_hourly
120122
min_time SimpleAggregateFunction(min, DateTime('UTC')),
121123
max_time SimpleAggregateFunction(max, DateTime('UTC')),
122124
tag SimpleAggregateFunction(groupArrayArray, Array(String)),
125+
distinct_id,
123126
created_at Datetime('UTC')
124127
)
125128
ENGINE = AggregatingMergeTree
@@ -172,6 +175,7 @@ SELECT
172175
min_time,
173176
max_time,
174177
tag,
178+
distinct_id,
175179
timestamp as created_at
176180
FROM (SELECT
177181
website_id,
@@ -209,6 +213,7 @@ FROM (SELECT
209213
min(created_at) min_time,
210214
max(created_at) max_time,
211215
arrayFilter(x -> x != '', groupArray(tag)) tag,
216+
distinct_id String,
212217
toStartOfHour(created_at) timestamp
213218
FROM umami.website_event
214219
GROUP BY website_id,
@@ -224,6 +229,7 @@ GROUP BY website_id,
224229
region,
225230
city,
226231
event_type,
232+
distinct_id,
227233
timestamp);
228234

229235
-- projections
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- AlterTable
2+
ALTER TABLE `session` ADD COLUMN `distinct_id` VARCHAR(50) NULL;
3+
4+
-- AlterTable
5+
ALTER TABLE `session_data` ADD COLUMN `distinct_id` VARCHAR(50) NULL;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
22
# It should be added in your version-control system (e.g., Git)
3-
provider = "mysql"
3+
provider = "mysql"

db/mysql/schema.prisma

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ model User {
3030
}
3131

3232
model Session {
33-
id String @id @unique @map("session_id") @db.VarChar(36)
34-
websiteId String @map("website_id") @db.VarChar(36)
35-
browser String? @db.VarChar(20)
36-
os String? @db.VarChar(20)
37-
device String? @db.VarChar(20)
38-
screen String? @db.VarChar(11)
39-
language String? @db.VarChar(35)
40-
country String? @db.Char(2)
41-
region String? @db.Char(20)
42-
city String? @db.VarChar(50)
43-
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0)
33+
id String @id @unique @map("session_id") @db.VarChar(36)
34+
websiteId String @map("website_id") @db.VarChar(36)
35+
browser String? @db.VarChar(20)
36+
os String? @db.VarChar(20)
37+
device String? @db.VarChar(20)
38+
screen String? @db.VarChar(11)
39+
language String? @db.VarChar(35)
40+
country String? @db.Char(2)
41+
region String? @db.Char(20)
42+
city String? @db.VarChar(50)
43+
distinct_id String? @db.VarChar(50)
44+
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0)
4445
4546
websiteEvent WebsiteEvent[]
4647
sessionData SessionData[]
@@ -166,6 +167,7 @@ model SessionData {
166167
numberValue Decimal? @map("number_value") @db.Decimal(19, 4)
167168
dateValue DateTime? @map("date_value") @db.Timestamp(0)
168169
dataType Int @map("data_type") @db.UnsignedInt
170+
distinct_id String? @db.VarChar(50)
169171
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamp(0)
170172
171173
website Website @relation(fields: [websiteId], references: [id])
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- AlterTable
2+
ALTER TABLE "session" ADD COLUMN "distinct_id" VARCHAR(50);
3+
4+
-- AlterTable
5+
ALTER TABLE "session_data" ADD COLUMN "distinct_id" VARCHAR(50);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
22
# It should be added in your version-control system (e.g., Git)
3-
provider = "postgresql"
3+
provider = "postgresql"

db/postgresql/schema.prisma

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ model User {
3030
}
3131

3232
model Session {
33-
id String @id @unique @map("session_id") @db.Uuid
34-
websiteId String @map("website_id") @db.Uuid
35-
browser String? @db.VarChar(20)
36-
os String? @db.VarChar(20)
37-
device String? @db.VarChar(20)
38-
screen String? @db.VarChar(11)
39-
language String? @db.VarChar(35)
40-
country String? @db.Char(2)
41-
region String? @db.VarChar(20)
42-
city String? @db.VarChar(50)
43-
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
33+
id String @id @unique @map("session_id") @db.Uuid
34+
websiteId String @map("website_id") @db.Uuid
35+
browser String? @db.VarChar(20)
36+
os String? @db.VarChar(20)
37+
device String? @db.VarChar(20)
38+
screen String? @db.VarChar(11)
39+
language String? @db.VarChar(35)
40+
country String? @db.Char(2)
41+
region String? @db.VarChar(20)
42+
city String? @db.VarChar(50)
43+
distinct_id String? @db.VarChar(50)
44+
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
4445
4546
websiteEvent WebsiteEvent[]
4647
sessionData SessionData[]
@@ -166,6 +167,7 @@ model SessionData {
166167
numberValue Decimal? @map("number_value") @db.Decimal(19, 4)
167168
dateValue DateTime? @map("date_value") @db.Timestamptz(6)
168169
dataType Int @map("data_type") @db.Integer
170+
distinct_id String? @db.VarChar(50)
169171
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
170172
171173
website Website @relation(fields: [websiteId], references: [id])

src/app/(main)/websites/[websiteId]/sessions/SessionsDataTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function SessionsDataTable({
1414
const queryResult = useWebsiteSessions(websiteId);
1515

1616
return (
17-
<DataTable queryResult={queryResult} allowSearch={false} renderEmpty={() => children}>
17+
<DataTable queryResult={queryResult} allowSearch={true} renderEmpty={() => children}>
1818
{({ data }) => <SessionsTable data={data} showDomain={!websiteId} />}
1919
</DataTable>
2020
);

src/lib/constants.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,7 @@ export const FILTER_REFERRERS = 'filter-referrers';
3333
export const FILTER_PAGES = 'filter-pages';
3434

3535
export const UNIT_TYPES = ['year', 'month', 'hour', 'day', 'minute'];
36-
export const EVENT_COLUMNS = [
37-
'url',
38-
'entry',
39-
'exit',
40-
'referrer',
41-
'title',
42-
'query',
43-
'event',
44-
'tag',
45-
'region',
46-
];
36+
export const EVENT_COLUMNS = ['url', 'entry', 'exit', 'referrer', 'title', 'query', 'event', 'tag'];
4737

4838
export const SESSION_COLUMNS = [
4939
'browser',
@@ -53,6 +43,7 @@ export const SESSION_COLUMNS = [
5343
'language',
5444
'country',
5545
'city',
46+
'region',
5647
'host',
5748
];
5849

0 commit comments

Comments
 (0)