PostHog

Track page views, custom events, and session recordings with PostHog.

PostHog is an open-source product analytics platform that gives you full control over your data. Boltdocs injects the PostHog JavaScript snippet automatically when you configure it.

Configuration ReferenceLink

PropertyTypeDefaultDescription
apiKeystring✓Your PostHog project API key (e.g. 'phc_xxx')
hoststring'https://us.i.posthog.com'PostHog API host. Use 'https://eu.i.posthog.com' for the EU cloud
capturePageviewbooleantrueCapture page views automatically
capturePageleavebooleantrueCapture page leave events automatically
sessionRecordingbooleanfalseEnable session recording
autocapturebooleanfalseEnable automatic click capture

Minimal ExampleLink

ts
integrations: {
  analytics: {
    posthog: {
      apiKey: 'phc_xxxxxxxxxxxxxxxxxxxx',
    },
  },
}

EU CloudLink

If your PostHog instance is in the EU region:

ts
integrations: {
  analytics: {
    posthog: {
      apiKey: 'phc_xxxxxxxxxxxxxxxxxxxx',
      host: 'https://eu.i.posthog.com',
    },
  },
}

With Session RecordingLink

ts
integrations: {
  analytics: {
    posthog: {
      apiKey: 'phc_xxxxxxxxxxxxxxxxxxxx',
      sessionRecording: true,
    },
  },
}

What Gets TrackedLink

Automatic EventsLink

EventDescription
$pageviewFires on every page navigation (including client-side navigation)
$pageleaveFires when the user leaves a page

Custom EventsLink

You can track custom events using the useTrackEvent() hook:

tsx
import { useTrackEvent } from 'boltdocs'

export function MyComponent() {
  const trackEvent = useTrackEvent()

  const handleClick = () => {
    trackEvent('button_click', {
      button_id: 'downloadPdf',
      button_location: 'sidebar',
    })
  }

  return <button onClick={handleClick}>Download PDF</button>
}

Using the HooksLink

useAnalytics()Link

Access the full analytics API:

tsx
import { useAnalytics } from 'boltdocs'

function MyComponent() {
  const { trackEvent, trackPageView } = useAnalytics()

  // Track a custom event
  trackEvent('search', { query: 'installation' })

  // Track a page view manually
  trackPageView('/docs/guides/getting-started/installation', 'Installation')
}

useTrackPageView()Link

Simplified page view tracking:

tsx
import { useTrackPageView } from 'boltdocs'

function DocPage({ title, path }) {
  const trackPageView = useTrackPageView()

  useEffect(() => {
    trackPageView(path, title)
  }, [path, title])
}

useTrackEvent()Link

Track arbitrary events:

tsx
import { useTrackEvent } from 'boltdocs'

function SearchBox() {
  const trackEvent = useTrackEvent()

  return (
    <input
      onChange={(e) => {
        trackEvent('search_input', { query: e.target.value })
      }}
    />
  )
}

Respecting User PrivacyLink

PostHog supports opting out of data capture. You can use the PostHog API to respect user consent:

tsx
import { useEffect } from 'react'

function ConsentHandler({ hasConsent }) {
  useEffect(() => {
    if (window.posthog) {
      if (hasConsent) {
        window.posthog.opt_in_capturing()
      } else {
        window.posthog.opt_out_capturing()
      }
    }
  }, [hasConsent])

  return null
}

TroubleshootingLink

Events don't appearLink

  1. Verify the API key is correct (starts with phc_)
  2. Check the browser console for errors
  3. Use PostHog's Live Events panel to see real-time events

Scripts don't appear in developmentLink

PostHog scripts are only injected in production builds. Run boltdocs build && boltdocs preview to test locally.

Duplicate page viewsLink

Page views are tracked automatically on navigation. Don't call trackPageView manually unless necessary.

Build errorsLink

Make sure integrations.analytics.posthog.apiKey is a non-empty string.

Last updated on October 18, 2026

Was this page helpful?