Google Analytics 4

Track page views and custom events in your documentation using Google Analytics 4.

Boltdocs integrates with Google Analytics 4 (GA4) to give you insight into how users navigate your documentation.

What Gets TrackedLink

Automatic EventsLink

EventDescription
page_viewFires on every page navigation (including client-side navigation)
first_visitNew user session
session_startNew session

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>
}

Configuration ReferenceLink

PropertyTypeRequiredDescription
measurementIdstring✓Your GA4 Measurement ID (e.g. 'G-XXXXXXXXXX')
debugboolean—Enables GA4 debug mode (logs to console)
anonymizeIpboolean—Anonymizes user IP addresses (defaults to true)
sendPageViewboolean—Enables automatic initial page view tracking (defaults to true)
cookieFlagsstring—Custom cookie flags (e.g. 'SameSite=None;Secure')
autoTrackAutoTrackConfig—Custom settings for automatic tracking (see below)

Automatic Tracking Options (autoTrack)Link

Sub-PropertyTypeDefaultDescription
pageViewsbooleantrueAutomatically tracks page views on client-side route changes
downloadsbooleanfalseTracks file download links
externalLinksbooleanfalseTracks clicks on outbound external links
searchbooleantrueTracks searches performed on the site

Advanced ExampleLink

ts
integrations: {
  ga4: {
    measurementId: 'G-XXXXXXXXXX',
    debug: false,
    anonymizeIp: true,
    sendPageView: true,
    cookieFlags: 'SameSite=None;Secure',
    autoTrack: {
      pageViews: true,
      downloads: true,
      externalLinks: true,
      search: true,
    },
  },
}

Using the HooksLink

useAnalytics()Link

Access the full analytics API:

tsx
import { useAnalytics } from 'boltdocs'

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

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

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

  // Set user properties
  setUserProperties({ plan: 'enterprise' })
}

useTrackPageView()Link

Simplified page view tracking:

tsx
import { useTrackPageView } from 'boltdocs'

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

  // Automatic tracking on mount
  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

Boltdocs does not implement cookie consent. For GDPR compliance, consider:

  1. Adding a cookie consent banner in your custom layout
  2. Only calling GA4 functions after consent is granted
  3. Configuring GA4 to respect Do Not Track

TroubleshootingLink

Events don't appearLink

  1. Verify the Measurement ID is correct
  2. Check the browser console for errors
  3. Use GA4 DebugView to see real-time events

Duplicate page viewsLink

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

Build errorsLink

Make sure integrations.ga4.measurementId is a string starting with G-.

Last updated on October 18, 2026

Was this page helpful?