6 min read

How to Monitor Session Duration in PostHog

PostHog captures session duration automatically—but most teams don't realize it's there, ready to use. Every event carries a $session_duration property that tells you exactly how long the user has been in the session when that event fired. The trick is turning that raw data into actionable insights that show you when engagement is dropping.

Enable Session Tracking and Access Duration Data

PostHog records session duration by default. Your first step is to verify it's working and understand where to find the data.

Step 1: Initialize PostHog with Session Recording Enabled

When you install the PostHog SDK, session tracking is on by default. Sessions are stored locally and identified by a unique ID. Every event is timestamped relative to the session start, giving you $session_duration.

javascript
import posthog from 'posthog-js'

posthog.init('phc_your_api_key', {
  api_host: 'https://us.posthog.com',
  session_recording: {
    recordOnlyOnInteraction: false
  }
})
Session recording captures duration automatically—no manual session start/stop needed

Step 2: Capture Events Within Sessions

Every posthog.capture() call happens within a session. PostHog automatically attaches the current $session_duration (in milliseconds) to that event. You don't write code to set it—it's calculated server-side.

javascript
// Capture events normally—$session_duration is added automatically
posthog.capture('button_clicked', {
  button_name: 'checkout',
  product_id: '12345'
})

// In PostHog's UI, you'll see:
// { event: 'button_clicked', $session_duration: 45000, ... }

Step 3: View Session Duration in the Event Stream

Go to Data > Events in PostHog and click any event to see its properties. Scroll down and you'll find $session_duration. Click on an event from a longer session and you'll see a larger number (in milliseconds).

Watch out: $session_duration is in milliseconds. Divide by 1000 to get seconds, or by 60000 to get minutes.

Create a Session Duration Insight

Use PostHog's Trends view to see session duration distribution over time and across user segments.

Step 1: Build a Trends Insight with Session Duration Breakdown

Navigate to Insights > New insight > Trends. Select an event (like $pageview or your custom event). In the Breakdown section, click + Add breakdown and search for $session_duration. PostHog will automatically bucket durations and show the distribution.

javascript
// The UI creates a query like this behind the scenes:
// SELECT COUNT(*) FROM events 
// GROUP BY $session_duration 
// PostHog buckets this: 0-10s, 10-30s, 30-120s, 120s+
// You see a histogram showing which duration ranges are most common

Step 2: Segment by Plan or Cohort

Add another breakdown by User properties > plan (or any user property). This shows whether premium users have longer sessions than free users. You'll see a stacked breakdown bar showing duration distribution per segment.

Step 3: Add a Filter to Exclude Bots

Click Add filter and select Properties > $session_duration > is greater than > 10000 (10 seconds). This filters out accidental page visits and bot traffic, giving you a cleaner signal on real engagement.

javascript
// In PostHog's query builder, this becomes:
// WHERE $session_duration > 10000
// Applied at query time—no SDK changes needed

Step 4: Save and Set an Alert

Click Save to store your insight as a dashboard card. Then click More options (...) and select Set up alert. Configure it to notify you when average session duration drops more than 20% from the weekly baseline.

Tip: Session duration is a lagging indicator. A sudden drop often means users hit a bug or confusing UX. Pair this with error rate alerts.

Use Session Duration to Diagnose Funnel Drops

Short sessions often predict funnel abandonment. Use session duration to find where users are bailing out early.

Step 1: Create Funnels Filtered by Session Duration

Build a funnel in Insights > Funnel: Page viewButton clickForm submit. Add a filter: Properties > $session_duration > is greater than > 60000 (60 seconds). This shows conversion rates only for engaged users.

javascript
// Make sure you're capturing each step explicitly:
posthog.capture('page_view', { page: '/checkout' })
posthog.capture('cta_clicked', { cta_name: 'buy_now' })
posthog.capture('form_submitted', { form: 'payment' })

// PostHog uses event order + timestamp to build the funnel
// $session_duration filters applied automatically

Step 2: Compare Conversion by Duration Cohort

Create three separate insights: one for sessions under 30 seconds, another for 30–120 seconds, and one for 120+ seconds. Run the same funnel for each. If short-session users have 2% conversion and long-session users have 30%, you know engagement (not messaging) is the bottleneck.

javascript
// Optional: Set a user property based on session duration for easier segmentation
if (posthog.get_property('$session_duration') > 180000) {
  posthog.setPersonProperties({ engagement_level: 'high' })
} else if (posthog.get_property('$session_duration') > 60000) {
  posthog.setPersonProperties({ engagement_level: 'medium' })
} else {
  posthog.setPersonProperties({ engagement_level: 'low' })
}

Common Pitfalls

  • Forgetting that $session_duration is in milliseconds—always divide by 1000 when comparing to user-facing times or dashboard labels.
  • Comparing average session duration across days with different traffic patterns (weekday vs. weekend); the median is often more reliable.
  • Assuming a short session is bad without context—30 seconds on a product page is healthy; 30 seconds on onboarding is a red flag.
  • Setting up a session duration alert but not pairing it with an error rate or page load time alert—engagement drops are symptoms, not root causes.

Wrapping Up

Session duration is free data in PostHog—captured automatically, no extra instrumentation needed. Use trends to spot engagement drops over time, segment by user cohorts to find which groups are leaving early, and filter funnels by duration to isolate real conversion issues. If you want to monitor session quality across all your analytics tools and correlate engagement with product metrics, Product Analyst can help.

Track these metrics automatically

Product Analyst connects to your stack and surfaces the insights that matter.

Try Product Analyst — Free