wp-media / wp-mixpanel
WordPress Mixpanel Integration
Installs: 77 891
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
pkg:composer/wp-media/wp-mixpanel
Requires
- php: >=7.3
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-06 10:14:03 UTC
README
A library for integrating Mixpanel analytics into WordPress projects of Group.One.
Overview
WP Mixpanel provides seamless integration between WordPress and Mixpanel, allowing you to track events in your WordPress projects.
Installation
Via Composer
Add the package to your project using Composer:
composer require wp-media/wp-mixpanel
Configuration
The library is composed of 3 main classes:
Optinhandles the status of the opt-in for analyticsTrackingis the base class for interaction with MixpanelTrackingPluginextends theTrackingclass with some specific configuration for usage in WordPress plugins
Initialize the Tracking class alone
new WPMedia\Mixpanel\Tracking( $mixpanel_token, $options = [] );
The $mixpanel_token is the token provided by Mixpanel corresponding to the project you want to send data to.
For Group.One, we have a sandbox project and a production project.
The $options parameter is an optional array which can be used to configure further the Mixpanel PHP library configuration.
Initialize the TrackingPlugin in a WordPress plugin
new WPMedia\Mixpanel\TrackingPlugin( $mixpanel_token, $plugin, $brand = '', $app = '' );
- The
$pluginis the plugin name + the version - The
$brandand$appare optional, but should be specified based on the analytics requirements of the plugin you implement the library in.
Initialize the Optin class
new WPMedia\Mixpanel\Optin( $plugin_slug, $capability );
- The
$plugin_slugis the slug used in your plugin as the prefix for options - The
$capabilityis the capability required to be able to modify the value of the optin
Usage Examples
Track a simple event without optin
$tracking->identify( $user_id ); $tracking->track( 'Event Name', $properties );
Calling identify() is required to associate sent events with a user ID. The $user_id provided is automatically hashed with the appropriate algorithm.
The track() method takes 2 required arguments:
- The
$event_namecorresponding to the event name which should be displayed in Mixpanel. Events use Start Case formatting. - The
$propertiesis an array of properties to associate with the event in the formatproperty_name => value. Properties use small caps formatting and underscores.
Track an event with optin check in a plugin
if ( ! $optin->can_track() ) { return; } $tracking_plugin->identify( $user_id ); $tracking_plugin->track( 'Event Name', $properties, $event_capability = '' );
The track() method of the TrackingPlugin class is a bit different than its parent:
First, it takes an additional optional parameter $event_capability. By default, the capability required for all events is manage_options. This can be changed in two different ways:
- Using the filter
wp_mixpanel_event_capabilityto modify the value for all events - Passing the capability as the parameter
$event_capability, to set it on a specific event
Second, the method will automatically associate the following properties to the event:
domain: hashed value of the current hostnamewp_version: current WP versionphp_version: current PHP versionplugin: Plugin name and version (set in constructor)brand: Brand name (set in constructor)application: Application name (set in constructor)
wp_mixpanel_event_capability Filter usage
The filter can takes 3 arguments:
$capabilitythe capability for all events$eventthe current event name$appthe current app name
Read more about MixPanel at group.one
More information about how MixPanel is used at group.one is available in our internal documentation.