runtime
Social Product Analytics · milestone 03 / 04

milestone 3/4 Sessionize an event stream

Group consecutive events into user sessions.

Social Product Analytics · Clean event retries, reconstruct ordered funnels, sessionize activity, and compare experiment conversions.

approach

A session starts on a member’s first event or after a gap of at least 30 minutes. LAG finds the prior timestamp, CASE flags boundaries, and a running SUM numbers sessions. Here we use physical rows, including retries. Identical timestamps do not start a new session.

worked-example.sql
WITH previous AS(
  SELECT
    *,
    LAG(event_at) OVER(PARTITION BY member_id ORDER BY event_at,id) AS previous_at
  FROM social_events
), marked AS(
  SELECT
    *,
    CASE
      WHEN previous_at IS NULL
        OR unixepoch(event_at)-unixepoch(previous_at)>=1800
        THEN 1
      ELSE 0
    END AS new_session
  FROM previous
), numbered AS(
  SELECT
    *,
    SUM(new_session) OVER(PARTITION BY member_id ORDER BY event_at,id ROWS UNBOUNDED PRECEDING) AS session_number
  FROM marked
)
SELECT member_id,MAX(session_number) AS session_count
FROM numbered
GROUP BY member_id;

data for this milestone

social_events6 columns · 22 rows
idinteger · keyevent_keytextmember_idintegerevent_typetextevent_attextingested_attext
1a1view2025-01-01 10:00:002025-01-01 10:00:01
2b1signup2025-01-01 10:03:002025-01-01 10:03:01
3c1purchase2025-01-01 10:05:002025-01-01 10:05:01
4a1view2025-01-01 10:00:002025-01-01 10:10:00
5d2view2025-01-02 09:00:002025-01-02 09:00:01

showing 5 of 22 rows

challenge 1

Using raw social_events, return member_id and session_count.

Start a new session on the first event or a gap of at least 30 minutes; order by event_at then id per member.

what to return

Columns
member_idsession_count in this order
Rows
any order
challenge-1.sql
 
3 steps left · check both queries and pass the concept checkOpen the reportNext milestone
Sessionize an event stream — Social Product Analytics · Runtime