Google Analytics plugin for VuePress that provides tracking integration and automatic page view monitoring
npx @tessl/cli install tessl/npm-vuepress--plugin-google-analytics@1.9.0The VuePress Google Analytics plugin provides seamless Google Analytics integration for VuePress documentation sites. It automatically injects tracking code during production builds, enables automatic page view tracking for single-page application navigation, and provides anonymized IP tracking for privacy compliance.
npm install @vuepress/plugin-google-analytics// Plugin is not imported directly, but configured in VuePress config
module.exports = {
plugins: [
['@vuepress/plugin-google-analytics', {
ga: 'UA-XXXXX-X' // Your tracking ID
}]
]
}// .vuepress/config.js
module.exports = {
plugins: [
['@vuepress/plugin-google-analytics', {
ga: 'UA-XXXXX-X' // Universal Analytics tracking ID
}]
]
}Alternative configuration using site-level config:
// .vuepress/config.js
module.exports = {
ga: 'UA-XXXXX-X', // Site-level configuration
plugins: [
'@vuepress/plugin-google-analytics'
]
}The main plugin factory function that creates a VuePress plugin instance with Google Analytics integration.
/**
* Creates a VuePress plugin instance for Google Analytics integration
* @param {PluginOptions} options - Plugin configuration options (default: {})
* @param {Context} context - VuePress context object
* @returns {PluginObject} VuePress plugin object
*/
module.exports = (options = {}, context) => PluginObject;
interface PluginOptions {
/** Google Analytics tracking ID (e.g., 'UA-XXXXX-X' or 'G-XXXXXXXXXX') */
ga?: string;
}
interface Context {
/** VuePress site configuration object */
siteConfig?: {
/** Alternative location for Google Analytics tracking ID */
ga?: string;
};
}
interface PluginObject {
/** Function that defines variables available to client code */
define(): { GA_ID: string | false };
/** Path to client enhancement file for Google Analytics injection */
enhanceAppFiles: string;
}Usage Pattern:
options.ga) or site configuration (siteConfig.ga)false and tracking will be disabledThe client-side enhancement function that injects Google Analytics tracking code and sets up automatic page view tracking.
/**
* Client-side enhancement function for Google Analytics integration
* @param {EnhancementContext} context - Client enhancement context
*/
export default function clientEnhancement(context: EnhancementContext): void;
interface EnhancementContext {
/** Vue Router instance for tracking navigation */
router: VueRouter;
}
interface VueRouter {
/** Hook that fires after each route navigation */
afterEach(callback: (to: Route) => void): void;
/** Current Vue app instance */
app: {
/** VuePress helper function for base path resolution */
$withBase(path: string): string;
};
}
interface Route {
/** Full path of the current route */
fullPath: string;
}Behavior:
process.env.NODE_ENV === 'production')GA_ID is available and browser environment is detectedanalytics.js)anonymizeIp: true) for privacy compliance$withBase helper to resolve proper base paths for trackinginterface PluginOptions {
/**
* Google Analytics tracking ID
* Supports both Universal Analytics (UA-XXXXX-X) and GA4 (G-XXXXXXXXXX) formats
* Takes precedence over site-level configuration
*/
ga?: string;
}interface SiteConfig {
/**
* Alternative location for Google Analytics tracking ID
* Used when plugin is configured without explicit options
* Plugin options take precedence over this setting
*/
ga?: string;
}The plugin gracefully handles various edge cases: