> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mixpanel/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Track events from PHP servers with the Mixpanel server-side SDK

<Note>
  Requires PHP 5.0 or greater.
</Note>

## Installation

<Tabs>
  <Tab title="Composer">
    Add to `composer.json`:

    ```json theme={null}
    {
      "require": {
        "mixpanel/mixpanel-php": "2.*"
      }
    }
    ```

    Then run:

    ```bash theme={null}
    composer update
    ```

    Initialize:

    ```php theme={null}
    <?php
    require 'vendor/autoload.php';

    $mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN");
    ?>
    ```
  </Tab>

  <Tab title="Manual">
    Download from [GitHub](https://github.com/mixpanel/mixpanel-php) and extract:

    ```php theme={null}
    <?php
    require 'mixpanel-php/lib/Mixpanel.php';

    $mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN");
    ?>
    ```
  </Tab>
</Tabs>

## Configuration

```php theme={null}
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
  "max_batch_size" => 100,
  "debug" => true
));
?>
```

## Track Events

```php theme={null}
<?php
$mp->track("Purchase", array(
  "distinct_id" => "12345",
  "product" => "Premium Subscription",
  "amount" => 49.99,
  "currency" => "USD"
));
?>
```

### Identify

```php theme={null}
<?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 theme={null}
<?php
$mp->people->set("12345", array(
  "name" => "John Doe",
  "$email" => "john@example.com",
  "plan" => "Premium"
), $ip = 0); // Disable geolocation
?>
```

<Tabs>
  <Tab title="setOnce()">
    ```php theme={null}
    <?php
    $mp->people->setOnce("12345", array(
      "First Purchase" => "2024-01-01"
    ), $ip = 0);
    ?>
    ```
  </Tab>

  <Tab title="increment()">
    ```php theme={null}
    <?php
    $mp->people->increment("12345", "login_count", 1);
    $mp->people->increment("12345", "age", 1);
    ?>
    ```
  </Tab>

  <Tab title="append()">
    ```php theme={null}
    <?php
    $mp->people->append("12345", "transactions", "purchase_123");
    ?>
    ```
  </Tab>
</Tabs>

## Group Analytics

### Send Events

```php theme={null}
<?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 theme={null}
<?php
$mp->people->set("12345", array(
  "company" => "Acme Inc"
), $ip = 0);
?>
```

## Privacy Controls

### EU Data Residency

```php theme={null}
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
  "host" => "api-eu.mixpanel.com"
));
?>
```

### India Data Residency

```php theme={null}
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
  "host" => "api-in.mixpanel.com"
));
?>
```

### Disable Geolocation

```php theme={null}
<?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 theme={null}
<?php
$mp = Mixpanel::getInstance("YOUR_PROJECT_TOKEN", array(
  "debug" => true
));
?>
```

## Custom Consumers

Create custom consumers for advanced data routing:

```php theme={null}
<?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

<Warning>
  All server-side calls originate from your server's IP. Set `$ip = 0` to disable geolocation.
</Warning>

* 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

* [Full API Reference](https://mixpanel.github.io/mixpanel-php/)
* [GitHub Repository](https://github.com/mixpanel/mixpanel-php)
* [Example Scripts](https://github.com/mixpanel/mixpanel-php/tree/master/examples)
