posthog
Integrates PostHog Analytics with Drupal, providing comprehensive product analytics, and user behavior tracking. Supports both client-side (JavaScript) and server-side (PHP) tracking, with integrations for Drupal Commerce, Webform, consent management, and more.
Features
The PostHog Analytics Integration module provides integration of PostHog's product analytics platform with your Drupal site. This module enables you to track user behavior, analyze product metrics and gain insights into how users interact with your Drupal site.
Core Functionality
- Dual API Support: Integrates both the PostHog JavaScript SDK (client-side) and PHP SDK (server-side) for comprehensive tracking across frontend and backend interactions.
- User Identification: Enhanced user identification options including identification by Drupal user ID, email, or username, with configurable user properties.
- Anonymous User Tracking: Optional identification of anonymous users to create person profiles for all visitors.
Use Cases
- Track user behavior and product analytics on your Drupal site
- Monitor e-commerce performance with Commerce integration
- Analyze form submission patterns with Webform integration
- Comply with privacy regulations through consent management integration
- Track server-side events and errors for comprehensive monitoring
- Track ECA actions in PostHog
Submodules
The module includes several submodules that extend functionality for specific use cases:
- PostHog JS Tracking: Integrates the PostHog JavaScript SDK for client-side event tracking. Supports loading from CDN or local installation. Automatically tracks page views and user interactions in the browser.
- PostHog PHP SDK: Provides server-side integration using the PostHog PHP SDK. Enables tracking events from Drupal's backend, identifying users on login, and sending events from PHP code.
- PostHog PHP Events: Provides custom Drupal related server-side events.
- PostHog COOKiES: Integrates PostHog with the COOKiES Consent Management module, allowing you to manage PostHog tracking based on user consent preferences. Supports cookieless server hash mode for anonymous tracking when consent is denied.
- PostHog Commerce: Integrates PostHog with Drupal Commerce, automatically tracking e-commerce events such as cart additions, order placements, and purchase completions.
- PostHog Webform: Integrates PostHog with the Webform module, tracking form submissions and user interactions with webforms.
- PostHog ECA: Integrates with the ECA (Events-Conditions-Actions) module, providing PostHog event actions that can be used in ECA workflows.
- PostHog PHP Error Tracking (TBD): Provides server-side PHP error tracking integration with PostHog, automatically capturing and reporting PHP errors and exceptions.
- PostHog Feature Flags (TBD): Adds support for PostHog feature flags, including a dedicated Condition API Plugin that allows you to conditionally show/hide content or features based on PostHog feature flag states.
- PostHog Dashboards (TBD): Provides PostHog analytics dashboards directly within the Drupal administration interface, allowing you to view analytics without leaving your Drupal site.
Post-Installation
After installing the module, follow these steps to configure PostHog:
- Configure Basic Settings: Navigate to Administration » Configuration » Web services » PostHog settings to configure:
- PostHog host (e.g., https://eu.i.posthog.com or https://us.i.posthog.com)
- PostHog project API key (found in your PostHog project settings)
- User identification preferences (how to identify logged-in users)
- User properties to send to PostHog (email, name, UID)
- Anonymous user identification settings
- Enable Submodules: Enable the submodules you need based on your requirements:
- Enable PostHog JS SDK & Tracking for client-side tracking
- Enable PostHog PHP SDK for server-side tracking
- Enable PostHog COOKiES if you use consent management
- Enable PostHog Commerce for e-commerce tracking
- Enable PostHog Webform for form tracking
Note: Some submodules are marked as experimental. These modules are still in development and not yet implemented!
Additional Requirements
Required
- PostHog Account: A PostHog account and project API key (available from PostHog project settings)
Optional (for submodules)
- PostHog JS Library: The
npm-asset/posthog-jslibrary is optional if you want to load PostHog JS locally instead of from CDN - PostHog PHP Library: The
posthog/posthog-phplibrary (version ^3) is automatically installed via Composer when you install the main module - COOKiES: Required for the PostHog COOKiES submodule
- Drupal Commerce: Required for the PostHog Commerce submodule
- Webform: Required for the PostHog Webform submodule
- ECA: Required for the PostHog ECA submodule
- Markdown: Optional, for prettier help page formatting
Similar projects
- Google Tags / Analytics modules: For Google Tags / Analytics integration
- Matomo modules: For self-hosted analytics solutions
Supporting this module
Support DROWL's ♥ FOSS work on this module on OpenCollective!
Drupal and this module are FOSS. However, it takes dedicated people to develop and maintain. And they need YOU to give back!
We're committed to building and maintaining Drupal modules that benefit the entire community.
Supporting us on OpenCollective helps us continue to improve, innovate and contribute to Drupal's future. Every pledge makes a difference!
If this module has helped you, we would be very grateful for your donation to support its further development and maintenance.
Support our FOSS development ♥️
You can also speed up the development of features or bugfixes you'd love to see, by sponsoring and giving back!
Let's make Drupal even better, together!