Getting Started
The Mixpanel PHP library is designed to be used for scripting, or in circumstances when a client can’t or won’t run client side scripts The Full API Reference, Library Source Code, and example scripts are documented in our Github repo.Installing the Library
Note: Our library requires PHP 5.0 or greater.
- Composer
- Manual
You can get the library using Composer by including the following in your project’s Once the library is installed, create an instance of the Mixpanel class with the
composer.json requirements and running composer update:getInstance() method using your project token.Library Configuration
The Mixpanel class can be initialized with different configurations. See a complete list of the configuration options and default values here. You can override the default configuration using the$options constructor argument when creating the Mixpanel instance.
Example Usage
Sending Events
Track events using thetrack() method on the Mixpanel class. The track() method accepts two arguments ($event string and $properties array) to generate an event payload, then triggers a request to the /track API endpoint to ingest the event to your project.
The /track endpoint will only validate events with timestamps within the last 5 days of the request. Events with timestamps older than 5 days will not be ingested. See below on best practices for historical imports.
$city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your data center. Learn more about Geolocation best practices.
Importing Historical Events
The PHP SDK is a tracking SDK designed for real-time tracking in a server-side environment. Calling thetrack() method triggers a request to our /track API endpoint, which will validate for events with a timestamp that is within the last 5 days of the request. Events older than 5 days will not be ingested.
For bulk import of historical events older than 5 days, we will need to use the /import API endpoint which is optimized for scripting and supports ingesting historical data. We recommend the Python SDK (see the .import_data() function) and mixpanel-utils module (see the import_events() function) which both leverages the /import API for event ingestion.
Managing User Identity
Mixpanel groups events sent with different distinct_ids, presenting them in reports as different user event streams. You can connect events with different distinct_ids, ultimately attributing them to one user. You will want to check which version of ID management you have enabled within Project Settings -> Identity Merge.- Original API
- Simplified API
If you have Original ID Merge enabled, send your anonymous events with an anonymous ID that you generate. Then merge your anonymous ID and your chosen user ID by calling the
identify() method once they are known (typically at registration and login). This will send an $identify event containing both IDs and merge them under one identity cluster.Learn more about server-side ID management for project on Original ID Merge API.Example UsageStoring User Profiles
Once your users are identified, create user profiles by setting profile properties to describe them. Example profile properties include “name”, “email”, “company”, and any other demographic details about the user. The PHP SDK provides a few methods for setting profile properties, which will trigger requests to the /engage API endpoint. Mixpanel determines default geolocation data ($city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your data center. Learn more about best practices for geolocation.
Setting Profile Properties
The Mixpanel class has a public property called$people that exposes an instance of Producers_MixpanelPeople that you can use to make profile updates.
Use the people->set() method to create properties on a user record. If the profile does not exist, it will be created using the properties defined in the method. If the properties already exist, they will be overwritten.
Example Usage
Other Types of Profile Updates
There are a few other methods for setting profile properties. See a complete reference of the available methods here. A few commonly used people methods are highlighted below:- setOnce()
- append()
- increment()
The
setOnce() method set profile properties only if they do not exist yet. If it is setting a profile property that already exists, it will be ignored.Use this method if you want to set profile properties without the risk of overwriting existing data.Example UsageGroup Analytics
Read more about Group Analytics before proceeding. You will need to have the group key defined in your project settings first.
group_key and group_id.
group_keyis the event property that connects event data to a group. (e.g.company)group_idis the identifier for a specific group. (e.g.mixpanel,company_a,company_b, etc.)
Sending Group Identifiers With Events
All events must have the group key as an event property in order to be attributed to a group. Without the group key, an event cannot be attributed to a group. To send group identifiers with your events, set thegroup_key as an event property with the group_id as the value.
Example Usage
group_key value as a list of multiple group_id values.
Example Usage
Adding Group Identifiers to User Profiles
To connect group information to a user profile, include thegroup_key and group_id as a user profile property using the set() call.
Example Usage
Debug Mode
To enable debug mode, set thedebug configuration option to true when initializing the Mixpanel class.
Example Usage
Privacy-Friendly Tracking
You have control over the data you send to Mixpanel. The PHP SDK have a few configurations to help you protect user data. Since this is a server-side tracking library where you have control of the servers, your server is responsible for determining whether to send data about a particular user or not.EU Data Residency
Route data to Mixpanel’s EU servers by setting ahost configuration during initialization.
Example Usage
India Data Residency
Route data to Mixpanel’s India servers by setting ahost configuration during initialization.
Disable Geolocation
The PHP SDK parse the request IP address to generate geolocation properties for events and profiles. You may want to disable them to prevent the unintentional setting of your data’s geolocation to the location of your server that is sending the request, or to prevent geolocation data from being tracked entirely. To disable geolocation, set the$ip of your profile updates to 0 and set your events_endpoint to /track?ip=0 when initializing the Mixpanel instance.
Example Usage