A Vite plugin that simplifies integrating PostHog analytics into Vite-based applications. Current stable version is 2.1.0 (July 2024). Provides automatic injection of the PostHog snippet, environment-aware configuration, and optional React hooks/components. Released under MIT license with active maintenance (several releases in 2024). Differentiates from direct PostHog usage by handling Vite-specific optimizations (e.g., tree-shaking, environment variables). Peer dependencies include React (>=16.8) for optional React support.
npm install vite-plugin-posthogVerified import paths — ran on the pinned version, not inferred.
Shows how to configure the plugin in vite.config.ts with an API key from environment variable, and how to use the usePostHog React hook to capture events.
Use default import VitePluginPosthog from 'vite-plugin-posthog' and named exports for hooks.
Rely solely on the plugin to initialize PostHog; avoid manual posthog.init() in application code.
Wrap your app with <PostHogProvider> imported from 'vite-plugin-posthog'.
Ensure <PostHogProvider> wraps the component tree and vite-plugin-posthog is configured in vite.config.ts.
Run 'npm install vite-plugin-posthog' and verify import statement: import VitePluginPosthog from 'vite-plugin-posthog'.
Remove any manual posthog.init() calls; the plugin handles initialization.