Skip to content

Add Google Analytics tracking to documentation site #122

@teetangh

Description

@teetangh

Summary

Add analytics tracking to the Docusaurus documentation site to measure page views and user engagement across all pages, including client-side (SPA) navigations.

Background

The docs site is built with Docusaurus v3, which is a Single Page Application (SPA). After the initial page load, navigating between pages uses client-side routing — the browser doesn't do a full page reload. This means analytics scripts injected via <head> only fire once on the first load. Subsequent page navigations are invisible unless the analytics solution explicitly listens for client-side route changes.

Implementation

1. Couchbase GTM (Google Tag Manager)

Add the Couchbase GTM script (GTM-MVPNN2) via Docusaurus headTags in docusaurus.config.js. This provides corporate-level tracking (ads, Marketo, etc.) consistent with other Couchbase documentation sites.

2. Docusaurus gtag Plugin (GA4)

Enable the built-in @docusaurus/plugin-google-gtag via the preset-classic configuration. This plugin hooks into Docusaurus's router and automatically sends a page_view event on every client-side navigation, solving the SPA tracking gap.

Why both? GTM alone doesn't track SPA navigations unless the GTM container has a History Change trigger configured. The gtag plugin reliably tracks every page view, including SPA navigations, while GTM handles Couchbase-wide corporate tracking.

Changes

  • website/docusaurus.config.js:
    • Add headTags with Couchbase GTM script
    • Add gtag configuration to preset-classic options

Verification

  • GTM script (metrics.couchbase.com/gtm.js?id=GTM-MVPNN2) loads on initial page
  • gtag fires page_view on initial load with correct page URL/title
  • gtag fires page_view on SPA navigation (client-side link clicks) with correct page URL/title
  • No console errors
  • Build passes cleanly

Reference

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions