6 min read

How to Calculate Page Views in PostHog

Page views are a baseline metric—without them, you don't know what's actually being visited. PostHog captures page views automatically as $pageview events, but the calculation depends on whether you're counting total views or unique users, and how you filter by page.

Enable Automatic Page View Tracking

PostHog's web snippet automatically records a $pageview event whenever the page URL changes. This works for traditional page loads and client-side navigation in SPAs.

Step 1: Add the PostHog Web Snippet

Copy your PostHog initialization snippet into your HTML head or use the JavaScript SDK. The snippet automatically listens for page changes and sends $pageview events to PostHog.

javascript
import posthog from 'posthog-js';

posthog.init('phc_YOUR_API_KEY', {
  api_host: 'https://us.i.posthog.com',
  autocapture: true,  // Enables automatic $pageview tracking
});

// For SPAs, ensure client-side navigation is detected
posthog.pageView();
Initialize PostHog with automatic page view capture

Step 2: Verify `$pageview` Events Are Coming In

Go to Events in the left sidebar and check the event feed. You should see $pageview events appearing as you navigate your site. Each entry shows the timestamp and the page URL in the $current_url property.

javascript
// PostHog automatically captures:
// {
//   event: '$pageview',
//   properties: {
//     $current_url: 'https://example.com/pricing',
//     $referrer: 'https://example.com/home'
//   }
// }
Automatic page view event structure in PostHog
Watch out: Single-page applications (SPAs) with hash-based routing (#/page) won't trigger new $pageview events unless you set hash: false or manually call posthog.pageView() on route changes.

Count Page Views with Insights

Once events are flowing, use PostHog's Insights to aggregate and count page views by URL, time, and user segments.

Step 1: Create a Trends Insight

Go to InsightsNew insightTrends. Select $pageview as the event and PostHog will display total page views over time as a line or bar chart.

javascript
// Behind the scenes, PostHog executes:
// SELECT count(*) as page_views 
// FROM events 
// WHERE event = '$pageview' 
//   AND timestamp BETWEEN ? AND ?
Trends aggregates all $pageview events in your date range

Step 2: Filter by URL

Add a filter: where $current_url matches your target page. You can use contains (/pricing), exact match, or regex. This limits the count to specific pages.

javascript
// Count page views only on /pricing
// Filter: event = '$pageview' AND $current_url CONTAINS '/pricing'

// Result: Total page views to that URL
// You can also filter: $current_url CONTAINS 'blog' to count all blog page views
Filter page views by URL pattern or exact match

Step 3: Segment by Properties

Add a breakdown by User properties (e.g., plan_type) or Event properties (e.g., $browser, $os, $country). This splits the page view count across that dimension.

javascript
// Segment page views by browser
// SELECT $browser, count(*) 
// FROM events 
// WHERE event = '$pageview' 
// GROUP BY $browser

// Result: Page views split into Chrome, Safari, Firefox, etc.
Break down page views by a property to see traffic distribution
Tip: Save your insight as a dashboard card to monitor page views in real time. Your saved insight updates automatically when new data arrives.

Count Unique Visitors Instead of Page Views

Total page views include repeat visitors. If you need unique visitor count instead, switch the aggregation in Insights.

Step 1: Switch to Unique Users Aggregation

In your Trends insight, toggle the metric from Total count to Unique users. PostHog will count distinct users (by distinct_id) rather than total page view events.

javascript
// Total page views: All $pageview events (1 user = multiple events)
// Unique users: Distinct count of distinct_id values in $pageview events

// Example:
// - User A views 5 pages → Total = 5, Unique = 1
// - User B views 3 pages → Total = 3, Unique = 1
// - Total page views = 8, Unique users = 2
Understand the difference between page views and unique users

Step 2: Identify Logged-In Users

For accurate unique user counts, call posthog.identify() with a stable user ID (email or user ID) when someone logs in. Without this, PostHog can't distinguish repeat visitors.

javascript
// After login, identify the user
posthog.identify('[email protected]', {
  email: '[email protected]',
  plan: 'pro',
  company: 'Acme Inc'
});

// Now all page views from this user are grouped under '[email protected]'
// Unique user count will be accurate
Call identify() with a unique user identifier for accurate counts
Watch out: If you change a user's distinct_id without merging, PostHog treats them as two separate users. Always call identify() consistently—use email or a stable user ID, not session tokens.

Common Pitfalls

  • Forgetting to call identify() for logged-in users—they'll be counted as multiple unique users if they return in a new session
  • Counting total page views when you need unique users—these are different metrics and lead to inflated traffic estimates
  • Not filtering by date—your page view count includes all historical data unless you set a date range in Insights
  • Hash-based SPAs without hash: false config—client-side navigation won't trigger new $pageview events

Wrapping Up

Page views in PostHog start with automatic $pageview events and are queried through Insights. The key is understanding whether you need total views or unique users, setting distinct_id correctly for logged-in users, and filtering by the properties that matter. If you want to calculate page views consistently across tools and dashboards, Product Analyst can help.

Track these metrics automatically

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

Try Product Analyst — Free