1. Home
  2. ChevronRightSEO & Robots
  3. ChevronRightGoogle Analytics 4

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 insights into how users navigate your documentation.


Quick StartLink

Step 1: Get your Measurement IDLink

Create a GA4 property in Google Analytics and get your Measurement ID (starts with G-).

Step 2: Configure in boltdocs.config.tsLink

boltdocs.config.ts
export default defineConfig({
  integrations: {
    ga4: {
      measurementId: 'G-XXXXXXXXXX',
    },
  },
})

Step 3: DeployLink

The GA4 script is automatically injected into every page. Page views are tracked automatically.


What Gets TrackedLink

Automatic EventsLink

EventDescription
page_viewFired 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:

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
measurementIdstringYour GA4 Measurement ID (e.g., 'G-XXXXXXXXXX')
debugbooleanEnable GA4 debug mode (logs to console)
anonymizeIpbooleanAnonymize user IP addresses (defaults to true)
sendPageViewbooleanEnable automatic initial page view tracking (defaults to true)
cookieFlagsstringCustom cookie flags (e.g., 'SameSite=None;Secure')
autoTrackAutoTrackConfigCustom configuration for automatic tracking (see below)

Auto-Track Options (autoTrack)Link

Sub-PropertyTypeDefaultDescription
pageViewsbooleantrueTrack page views automatically on client-side route changes
downloadsbooleanfalseTrack file download links
externalLinksbooleanfalseTrack clicks on outbound external links
searchbooleantrueTrack searches performed on the site

Advanced ExampleLink

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:

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:

import { useTrackPageView } from 'boltdocs'

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

  // Auto-track on mount
  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

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

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

Duplicate page viewsLink

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

Build errorsLink

Ensure integrations.ga4.measurementId is a string starting with G-.

Last updated on July 27, 2026

Was this page helpful?