1. Home
  2. ChevronRightSEO & Robots
  3. ChevronRightPostHog

PostHog

Track page views, custom events, and session replays with PostHog product analytics.

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.


Quick StartLink

Step 1: Get your API keyLink

Create a project in PostHog and copy your project API key (starts with phc_).

Step 2: Configure in boltdocs.config.tsLink

boltdocs.config.ts
export default defineConfig({
  integrations: {
    analytics: {
      posthog: {
        apiKey: 'phc_xxxxxxxxxxxxxxxxxxxx',
      },
    },
  },
})

Step 3: DeployLink

The PostHog snippet is automatically injected into every page. Page views and page leaves are tracked by default.


Configuration ReferenceLink

PropertyTypeDefaultDescription
apiKeystringYour PostHog project API key (e.g., 'phc_xxx')
hoststring'https://us.i.posthog.com'PostHog API host. Use 'https://eu.i.posthog.com' for EU cloud
capturePageviewbooleantrueAutomatically capture page views
capturePageleavebooleantrueAutomatically capture page leave events
sessionRecordingbooleanfalseEnable session recording
autocapturebooleanfalseEnable autocapture of click events

Minimal ExampleLink

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

EU CloudLink

If your PostHog instance is hosted in the EU region:

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

With Session RecordingLink

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

What Gets TrackedLink

Automatic EventsLink

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

Custom EventsLink

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

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:

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:

import { useTrackPageView } from 'boltdocs'

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

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

useTrackEvent()Link

Track arbitrary events:

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 opt-out of data capture. You can use the PostHog API to respect user consent:

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 not appearingLink

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

Scripts not appearing 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 needed.

Build errorsLink

Ensure integrations.analytics.posthog.apiKey is a non-empty string.

Last updated on July 27, 2026

Was this page helpful?