@@ -2,6 +2,7 @@ CREATE TABLE analytics.analytics_events(
2
2
-- EventContext fields
3
3
client_id String,
4
4
session_id String,
5
+ duration UInt32,
5
6
app_version String,
6
7
system_os String,
7
8
system_arch String,
@@ -53,8 +54,20 @@ CREATE TABLE analytics.sessions(
53
54
date date ,
54
55
client_id String,
55
56
session_id String,
57
+ app_version String,
58
+ system_os String,
59
+ system_arch String,
60
+ system_locale String,
61
+ system_timezone String,
62
+ user_id Nullable(String),
63
+ ip Nullable(String),
64
+ user_agent Nullable(String),
65
+ geo_country Nullable(String),
66
+ geo_region Nullable(String),
67
+ geo_city Nullable(String),
56
68
session_start SimpleAggregateFunction(min, DateTime64(3 )),
57
69
session_end SimpleAggregateFunction(max, DateTime64(3 )),
70
+ session_length SimpleAggregateFunction(sum, UInt64),
58
71
total_events UInt32) ENGINE = SummingMergeTree()
59
72
ORDER BY
60
73
(
@@ -69,8 +82,20 @@ SELECT
69
82
toDate(server_ts) AS date ,
70
83
client_id,
71
84
session_id,
72
- minSimpleState(server_ts) AS session_start,
73
- maxSimpleState(server_ts) AS session_end,
85
+ any(app_version) AS app_version,
86
+ any(system_os) AS system_os,
87
+ any(system_arch) AS system_arch,
88
+ any(system_locale) AS system_locale,
89
+ any(system_timezone) AS system_timezone,
90
+ any(user_id) AS user_id,
91
+ any(ip) AS ip,
92
+ any(user_agent) AS user_agent,
93
+ any(geo_country) AS geo_country,
94
+ any(geo_region) AS geo_region,
95
+ any(geo_city) AS geo_city,
96
+ min (server_ts) AS session_start,
97
+ max (server_ts) AS session_end,
98
+ sum (duration) / 1000 AS session_length,
74
99
count (1 ) AS total_events
75
100
FROM
76
101
analytics .analytics_events
@@ -83,16 +108,22 @@ GROUP BY
83
108
-- INSERT INTO analytics.sessions...;
84
109
-- query sessions table
85
110
SELECT
86
- * ,
87
- dateDiff(' second' , session_start, session_end) AS session_length
111
+ date ,
112
+ client_id,
113
+ session_id,
114
+ session_start,
115
+ session_end,
116
+ session_length,
117
+ total_events
88
118
FROM
89
119
analytics .sessions FINAL;
90
120
91
121
CREATE TABLE analytics .daily_sessions(
92
122
date date ,
93
123
client_id String,
94
124
total_session_length SimpleAggregateFunction(sum, UInt64),
95
- total_events SimpleAggregateFunction(sum, UInt64)) ENGINE = SummingMergeTree()
125
+ total_session_events SimpleAggregateFunction(sum, UInt64),
126
+ unique_users AggregateFunction(uniq, Nullable(String))) ENGINE = SummingMergeTree()
96
127
ORDER BY
97
128
(
98
129
date ,
@@ -103,14 +134,27 @@ CREATE MATERIALIZED VIEW analytics.daily_sessions_mv TO analytics.daily_sessions
103
134
SELECT
104
135
date ,
105
136
client_id,
106
- sumSimpleState(dateDiff(' second' , session_start, session_end)) AS total_session_length,
107
- sumSimpleState(total_events) AS total_events
137
+ sum (session_length) AS total_session_length,
138
+ sum (total_events) AS total_session_events,
139
+ uniqState(user_id) AS unique_users
108
140
FROM
109
141
analytics .sessions
110
142
GROUP BY
111
143
date ,
112
144
client_id;
113
145
146
+ SELECT
147
+ date ,
148
+ client_id,
149
+ sum (total_session_length) AS total_session_length,
150
+ sum (total_session_events) AS total_session_events,
151
+ uniqMerge(unique_users) AS unique_users
152
+ FROM
153
+ analytics .daily_sessions
154
+ GROUP BY
155
+ date ,
156
+ client_id;
157
+
114
158
-- Insert sample data for AppStartEvent
115
159
INSERT INTO analytics .analytics_events (client_id, session_id, app_version, system_os, system_arch, system_locale, system_timezone, client_ts, server_ts, event_type)
116
160
VALUES (' client_001' , ' session_001' , ' 1.0.0' , ' macOS' , ' x86_64' , ' en-US' , ' America/New_York' , now(), now(), ' AppStart' );
0 commit comments