Skip to main content
PYSPARK โ€ข LESSON 288

Scenario: High-Scale Session Dwell Time Analytics

How can we compute the total active engagement time per user session by summing page view durations capped at 10 minutes?

Productionโฑ 3 Minutesโญ 870 XP
๐Ÿค” THE QUESTION

How can we compute the total active engagement time per user session by summing page view durations capped at 10 minutes?

๐Ÿ’ก WHAT IS IT?

Computing pageview intervals with lag(), capping outlier intervals with least(), and aggregating sums active dwell time.

๐ŸŽฏ WHAT IS IT USED FOR?

Media publisher analytics calculating true content engagement and ad viewability metrics.

๐Ÿ’ป EXAMPLE
w = Window.partitionBy("session_id").orderBy("view_time")
df_dwell = df_views.withColumn("next_time", lead("view_time", 1).over(w)).withColumn("raw_duration", col("next_time").cast("long") - col("view_time").cast("long")).withColumn("duration_sec", when(col("raw_duration").isNull(), 30).otherwise(least(col("raw_duration"), lit(600))))
df_session_totals = df_dwell.groupBy("session_id").agg(sum("duration_sec").alias("total_session_seconds"))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Scenario: High-Scale Session Dwell Time Analytics.

  • โœ“ Interval duration math
  • โœ“ least() outlier capping
  • โœ“ Session engagement aggregation