3. Agenda
• What is sessionization?
• Why is it important?
• How do I implement it?
• Now what?
• Questions
3
4. 4
What is an Event?
Any type of user interaction on a website or
product
signed up
logged in
viewed product
add to cart
purchase
logged in
viewed history
viewed product
clicked help
logged in
viewed product
5. 5
What is Sessionization?
Grouping events from the same user based
on a period of activity
signed up
logged in
viewed product
add to cart
purchase
logged in
viewed history
viewed product
clicked help
logged in
viewed product
6. 6
Why Should I Sessionize my Data?
To observe user behavior on your website, game, mobile app, etc
• Understand and increase conversion rates and retention
• Lead marketing campaigns
• Detect fraud
• Improve product
8. 8
What Is It Going To Tell Me?
• Which events cause bounces?
• Which events end a session or trigger a user to leave our site or product?
• What features and areas in the application are used and where in the
workflow?
• How long do users use the application?
• Which sessions encourage or discourage retention?
• Where do users spend most of their time?
14. 14
How Do We Define a Session?
User
A
A
A
A
A
B
B
• User
• Unknown visitor
• Known visitor (logged in)
15. 15
How Do We Define a Session?
User Event
A log in
A view category
A view product
A view product
A go to cart
B sign up
B log in
• User
• Unknown visitor
• Known visitor (logged in)
• Event
• View a page
• Click a button
• Play a video
16. 16
How Do We Define a Session?
User Event Time
A log in 3:04
A view category 3:05
A view product 3:07
A view product 5:44
A go to cart 5:46
B sign up 5:48
B log in 6:03
• User
• Unknown visitor
• Known visitor (logged in)
• Event
• View a page
• Click a button
• Play a video
• Timestamp
• When event occurred
17. 17
How Do We Define a Session?
User Event Time
A log in 3:04
A view category 3:05
A view product 3:07
A view product 5:44
A go to cart 5:46
B sign up 5:48
B log in 6:03
Session 1
Session 2
Session 3
• User
• Unknown visitor
• Known visitor (logged in)
• Event
• View a page
• Click a button
• Play a video
• Timestamp
• When event occurred
• Session
• 30 minute timeout
window
• 5 minute timeout window
• Event trigger
18. 18
Defining a Session
• Identify the time difference between each event
User Time Idle Time (min)
1 3:04 ⌀
1 3:05 1
1 3:07 2
1 4:07 60
19. 19
Defining a Session
• Identify the time difference between each event
datediff( minute, lag(events.created_at) OVER ( partition BY events.user_id
ORDER BY events.created_at ), events.created_at ) AS idle_time
User Time Idle Time (min)
1 3:04 ⌀
1 3:05 1
1 3:07 2
1 4:07 60
20. 20
Defining a Session
• Assign a new session ID to each event that has idle time > 30 minutes
• That event timestamp becomes the session start time
Session Start Idle Time User ID Session ID Next Session Start
2017-08-21 3:04 ⌀ 1 1 2017-08-21 4:07
2017-08-21 4:07 60 1 2 2017-08-21 6:37
2017-08-21 6:37 150 1 3 ⌀
21. 21
Defining a Session
Session Start Idle Time User ID Session ID Next Session Start
2017-08-21 3:04 ⌀ 1 1 2017-08-21 4:07
2017-08-21 4:07 60 1 2 2017-08-21 6:37
2017-08-21 6:37 150 1 3 ⌀
• Assign a new session ID to each event that has idle time > 30 minutes
• That event timestamp becomes the session start time
, ROW_NUMBER () OVER (ORDER BY find_idle_time.created_at) AS unique_session_id
FROM find_idle_time
WHERE (find_idle_time.idle_time > 30 OR find_idle_time.idle_time IS NULL)
23. 23
Mapping Events to Sessions
• Knowing the event timestamp, session start time, and next session start time, we can map an event to
a session
User ID Event ID Event Time Session Start Next Session Session ID
1 25 2017-08-21 3:04 2017-08-21 3:04 2017-08-21 5:02 1
1 32 2017-08-21 3:20 2017-08-21 3:04 2017-08-21 5:02 1
1 45 2017-08-21 5:10 2017-08-21 5:02 2017-08-22 9:35 2
24. 24
Mapping Events to Sessions
• Knowing the event timestamp, session start time, and next session start time, we can map an event to
a session
FROM events
INNER JOIN sessions
ON events.user_id = sessions.user_id
AND events.created_at >= sessions.session_start
AND events.created_at < sessions.next_session_start
User ID Event ID Event Time Session Start Next Session Session ID
1 25 2017-08-21 3:04 2017-08-21 3:04 2017-08-21 5:02 1
1 32 2017-08-21 3:20 2017-08-21 3:04 2017-08-21 5:02 1
1 45 2017-08-21 5:10 2017-08-21 5:02 2017-08-22 9:35 2
25. 25
Mapping Events to Sessions
User ID Event ID Event Time Session ID Event Sequence
1 25 2017-08-21 3:04 1 1
1 32 2017-08-21 3:20 1 2
1 45 2017-08-21 5:10 2 1
• Based on the event timestamp, we can sequence the events
26. 26
Mapping Events to Sessions
User ID Event ID Event Time Session ID Event Sequence
1 25 2017-08-21 3:04 1 1
1 32 2017-08-21 3:20 1 2
1 45 2017-08-21 5:10 2 1
• Based on the event timestamp, we can sequence the events
, ROW_NUMBER() OVER (PARTITION BY sessions.unique_session_id ORDER BY
events.created_at) AS event_sequence_within_session
29. 29
Session Facts
Session ID Timestamp User ID Event ID Session Start Session End
1 2017-08-21 3:04 1 25 2017-08-21 3:04 2017-08-21 3:20
1 2017-08-21 3:20 1 32 2017-08-21 3:04 2017-08-21 3:20
2 2017-08-21 5:10 1 45 2017-08-21 5:10 2017-08-21 6:30
• Now we can add additional detail, like the session end time
• Which can be used to calculate session duration
30. 30
Session Facts
Session ID Timestamp User ID Event ID Session Start Session End
1 2017-08-21 3:04 1 25 2017-08-21 3:04 2017-08-21 3:20
1 2017-08-21 3:20 1 32 2017-08-21 3:04 2017-08-21 3:20
2 2017-08-21 5:10 1 45 2017-08-21 5:10 2017-08-21 6:30
• Now we can add additional detail, like the session end time
• Which can be used to calculate session duration
, FIRST_VALUE (created_at) OVER (PARTITION BY unique_session_id ORDER BY created_at
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS session_start
, LAST_VALUE (created_at) OVER (PARTITION BY unique_session_id ORDER BY created_at ROWS
BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS session_end
32. 32
Putting It All Together
Session ID User ID Session Start Session End Session Seq Events in Session
1 1 2017-08-21 3:04 2017-08-21 3:20 1 2
1 1 2017-08-21 3:04 2017-08-21 3:20 1 2
2 1 2017-08-21 5:10 2017-08-21 6:30 2 1
• Add in the number of events in each session
33. 33
Putting It All Together
Session ID User ID Session Start Session End Session Seq Events in Session
1 1 2017-08-21 3:04 2017-08-21 3:20 1 2
1 1 2017-08-21 3:04 2017-08-21 3:20 1 2
2 1 2017-08-21 5:10 2017-08-21 6:30 2 1
• Add in the number of events in each session
, ROW_NUMBER () OVER (PARTITION BY session_facts.user_id ORDER BY MIN(session_start))
AS session_sequence_for_user
, count(1) AS events_in_session
FROM session_facts
INNER JOIN events_sessionized