-
Notifications
You must be signed in to change notification settings - Fork 2
/
sessions.model1.jsonnet
95 lines (94 loc) · 2.71 KB
/
sessions.model1.jsonnet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local util = import 'util.libsonnet';
{
name: 'sessions',
hidden: false,
label: 'Sessions',
sql: |||
SELECT unique_session_id,
user_session_id,
user_pseudo_id,
MAX(TIMESTAMP_MICROS(event_timestamp)) as session_end,
MIN(TIMESTAMP_MICROS(event_timestamp)) as session_start,
(MAX(event_timestamp) - MIN(event_timestamp))/(60 * 1000 * 1000) AS session_length_minutes
FROM (
SELECT user_pseudo_id,
event_timestamp,
SUM(is_new_session) OVER (ORDER BY user_pseudo_id, event_timestamp) AS unique_session_id,
SUM(is_new_session) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) AS user_session_id
FROM (
SELECT *,
CASE WHEN TIMESTAMP_DIFF(TIMESTAMP_MICROS(event_timestamp), TIMESTAMP_MICROS(last_event),MINUTE) >= 20 --session timout = 20 minutes
OR last_event IS NULL
THEN 1 ELSE 0 END AS is_new_session
FROM (
SELECT user_pseudo_id,
event_timestamp,
LAG(event_timestamp,1) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) AS last_event
FROM `%(target)s`
) last
) final
) session
GROUP BY 1,2,3
||| % { target: util.generate_target_reference(std.mergePatch(std.extVar('schema'), { table: 'events_*' })) },
mappings: {
eventTimestamp: 'session_start',
userId: 'user_pseudo_id',
},
dimensions: {
user_pseudo_id: {
type: 'string',
column: 'user_pseudo_id',
},
session_end: {
type: 'timestamp',
column: 'session_end',
},
session_start: {
type: 'timestamp',
column: 'session_start',
},
unique_session_id: {
type: 'integer',
column: 'unique_session_id',
},
user_session_id: {
type: 'integer',
column: 'user_session_id',
},
session_length_minutes: {
type: 'double',
column: 'session_length_minutes',
},
},
measures: {
sum_of_unique_session_id: {
column: 'unique_session_id',
aggregation: 'sum',
type: 'double',
},
sum_of_user_session_id: {
column: 'user_session_id',
aggregation: 'sum',
type: 'double',
},
sum_of_session_length_minutes: {
column: 'session_length_minutes',
aggregation: 'sum',
type: 'double',
},
number_of_sessions: {
aggregation: 'count',
type: 'double',
},
average_first_session_length: {
column: 'session_length_minutes',
aggregation: 'average',
type: 'double',
},
session_length_minutes: {
column: 'session_length_minutes',
aggregation: 'average',
type: 'double',
},
},
}