KatInterview

Event Stream Analytics: Sessions, Funnels, and Segment Performance

SQL & DataModel answer
MediumModel answerSQL & Datapandasdata-analysissessionization

Last updated

Process user activity logs to build sessions, compute conversion metrics by segment, construct a deduplicated funnel, and identify high-performing segments with confidence intervals.

You're analyzing user behavior from activity logs to produce actionable metrics for a product team.

Data Schema

You have two tables:

activity_log: records every user action

  • log_id (unique identifier for each event)
  • timestamp_utc (when the action occurred, UTC)
  • user_id (guest identifier)
  • platform (either "desktop" or "mobile")
  • channel (acquisition channel: "direct", "seo", "sem", "email", "partner")
  • action_type (one of: "page_view", "add_to_cart", "checkout_start", "order_completed")
  • url (page URL)
  • agent_string (browser user agent)

users: basic user metadata

  • user_id
  • registration_date_utc

Analysis Window

Use the 7-day period from August 26, 2025 through September 1, 2025 (inclusive), treating September 1 as "today."

Required Tasks

1. Sessionization

Create sessions for each user using a 30-minute inactivity threshold. Sessions should be consistent across platform switches (a user moving from mobile to desktop within 30 minutes stays in the same session). Assign stable session identifiers and compute start/end times.

2. Segment-Level Metrics

For each (channel, platform) combination, calculate:

  • Distinct users
  • Total sessions
  • Session conversion rate (% of sessions containing at least one order)
  • User conversion rate (% of users who completed at least one order)
  • Average number of sessions before first order (for converting users)
  • Median time from initial page view to first order

3. Funnel Analysis

Build a user-level funnel with these steps: page_view → add_to_cart → checkout_start → order_completed. Each user should be counted once at their furthest step reached. Report the number reaching each step, step-through percentages, and absolute drop-off between consecutive steps.

4. Top Performing Segments

Identify the top 5 segments by conversion lift compared to the overall site average. Exclude segments with fewer than 100 sessions. For each, report the lift multiplier and a 95% Wilson confidence interval for the segment's conversion rate.

Data Quality Rules

  • Filter out automated traffic: remove any events where agent_string contains "bot" (case-insensitive)
  • After filtering, exclude users with fewer than 2 valid events in the window
  • Handle edge case: users whose first in-window event is an order
    • Look back up to 24 hours before the window for their most recent prior event
    • If found, include that event in the analysis
    • If not found, attribute the order to "direct" channel and treat as valid

Attribution Logic

  • Sessions inherit the (channel, platform) from their first event
  • Users are attributed to their first-touch (channel, platform) across all their activity
  • Time-to-order is only meaningful when a page view precedes the order

Deliverables

Provide vectorized Pandas code (avoid row-by-row iteration) with clear variable names and comments. Briefly discuss how your approach would translate to PySpark for production-scale data.

Hints

Hint 1

Start by cleaning and sorting the entire event stream once—sessionization and all downstream metrics depend on a single canonical sort order.

Hint 2

Use cumulative sum on gap indicators to generate session IDs; this vectorized approach scales much better than iterating through rows.

Hint 3

For the funnel, assign each user to their maximum step reached, then compute reverse cumulative sums to get 'reached at least step N' counts.