Requires PHP 5.0 or greater.
Installation
Add to composer.json:{
"require": {
"mixpanel/mixpanel-php": "2.*"
}
}
Then run:Initialize:<?php
require 'vendor/autoload.php';
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN");
?>
Download from GitHub and extract:<?php
require 'mixpanel-php/lib/Mixpanel.php';
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN");
?>
Configuration
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
"max_batch_size" => 100,
"debug" => true
));
?>
Track Events
<?php
$mp->track("Purchase", array(
"distinct_id" => "12345",
"product" => "Premium Subscription",
"amount" => 49.99,
"currency" => "USD"
));
?>
Identify
<?php
// Set distinct_id for subsequent events
$mp->identify("12345");
// Track event (distinct_id included automatically)
$mp->track("Page View", array(
"page" => "Dashboard"
));
?>
User Profiles
<?php
$mp->people->set("12345", array(
"name" => "John Doe",
"$email" => "[email protected]",
"plan" => "Premium"
), $ip = 0); // Disable geolocation
?>
setOnce()
increment()
append()
<?php
$mp->people->setOnce("12345", array(
"First Purchase" => "2024-01-01"
), $ip = 0);
?>
<?php
$mp->people->increment("12345", "login_count", 1);
$mp->people->increment("12345", "age", 1);
?>
<?php
$mp->people->append("12345", "transactions", "purchase_123");
?>
Group Analytics
Send Events
<?php
$mp->track("Feature Used", array(
"distinct_id" => "12345",
"company" => "Acme Inc",
"feature" => "Reports"
));
?>
Set Group Properties
Group properties are set via the People endpoint with group keys:
<?php
$mp->people->set("12345", array(
"company" => "Acme Inc"
), $ip = 0);
?>
Privacy Controls
EU Data Residency
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
"host" => "api-eu.mixpanel.com"
));
?>
India Data Residency
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
"host" => "api-in.mixpanel.com"
));
?>
Disable Geolocation
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
"events_endpoint" => "/track?ip=0"
));
// For profiles
$mp->people->set("12345", array(
"name" => "John"
), $ip = 0);
?>
Debug Mode
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
"debug" => true
));
?>
Custom Consumers
Create custom consumers for advanced data routing:
<?php
class MyLoggingConsumer extends ConsumerStrategies_AbstractConsumer {
public function persist($batch) {
echo "<pre>";
echo "Would send batch:\n";
echo json_encode($batch) . "\n";
echo "</pre>";
return true;
}
}
$mp = new Mixpanel("YOUR_PROJECT_TOKEN", array(
"consumers" => array("logger" => "MyLoggingConsumer"),
"consumer" => "logger"
));
?>
All server-side calls originate from your server’s IP. Set $ip = 0 to disable geolocation.
- Events are queued and flushed automatically
- Default batch size: 1000 items
- You manage
distinct_id yourself
- No automatic identity management
- Custom consumers for scaling
Resources