liveBook · Manning

Session duration

As explained in The Well-Grounded Data Analyst

Session duration is the elapsed time between the beginning and end of a browsing session.

Session duration is the elapsed time from a browsing session’s first recorded event to its last. It turns event-level app activity into a session-level measure, so questions about browsing time can be answered without modeling every rapid app open and close. section 12.5.1

Build the metric from cleaned event data:

  • Check whether a session ID can occur for multiple users; group by both user ID and session ID when needed to keep sessions distinct. section 12.5.1
  • For each session, take the minimum timestamp as its start and the maximum as its end, then subtract start from end and convert to minutes. section 12.5.1
  • Aggregate session durations per user with a median to create an average-session-length feature for user-level analysis. section 12.5.2
  • Record cleaning choices: removing Closed events changes the reconstructed duration distribution and can prevent an exact match with an earlier analysis. section 12.5.1

Reading the distribution

A histogram of users’ typical session durations shows most sessions below two minutes, alongside a long tail that includes sessions longer than ten minutes. This makes unusually long browsing behavior visible rather than hiding it inside a single overall average. section 12.5.1

sessions = (
    app_data_reduced
    .groupby(["user_id", "session_id"])
    .agg(start=("timestamp", "min"),
         end=("timestamp", "max"))
    .reset_index()
    .assign(
        duration_mins=lambda _df: (_df["end"] - _df["start"]).dt.seconds/60
    )
)

sessions.head()

This transformation produces one row per user session with start time, end time, and duration in minutes. section 12.5.1

How The Well-Grounded Data Analyst covers it

How deep it goes
Derives durations through identifier checks, timestamp aggregation, cleaning choices, and per-user median aggregation.
Examples
Uses timestamped app events, a session-duration table, and a histogram of users’ median session lengths.
What it assumes
Assumes timestamped event records and user and session identifiers while demonstrating their validation and aggregation.
What you can do after
Apply it via pandas to create a per-user session-length feature for segmentation.
How it connects
Connects event data, session IDs, user IDs, data cleaning, median aggregation, and user segmentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Data Analyst. The section links point to the parts of the book it draws on.