Skip to main content
Requires PHP 5.0 or greater.

Installation

Add to composer.json:
{
  "require": {
    "mixpanel/mixpanel-php": "2.*"
  }
}
Then run:
composer update
Initialize:
<?php
require 'vendor/autoload.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
?>
<?php
$mp->people->setOnce("12345", array(
  "First Purchase" => "2024-01-01"
), $ip = 0);
?>

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"
));
?>

Platform Considerations

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

Build docs developers (and LLMs) love