laravel-mixpanel-api maintained by jeffersongoncalves

Laravel Mixpanel API
A Laravel client for Mixpanel's HTTP API: track events, update user profiles, and query insights, funnels, retention and raw event exports through a simple, typed API built on Laravel's Http client.
This is a server-side API client only. If you need the Mixpanel JS SDK / Blade view integration instead, see jeffersongoncalves/laravel-mixpanel.
Features
- Ingestion:
trackEvent,setProfile - Query API:
queryEvents,getFunnel,getRetention - Export API:
exportEvents(raw newline-delimited JSON) - Throws
InvalidArgumentExceptionwhen required credentials for a call are missing
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-mixpanel-api
Publish the config file:
php artisan vendor:publish --tag=mixpanel-api-config
Set your Mixpanel credentials in .env:
MIXPANEL_TOKEN=your-project-token
MIXPANEL_API_KEY=your-api-key
MIXPANEL_SECRET=your-api-secret
MIXPANEL_PROJECT_ID=your-project-id
The project token is used for ingestion (trackEvent, setProfile). The API key, secret and project ID are used for the query/export endpoints (queryEvents, getFunnel, getRetention, exportEvents). Find them under Project Settings > Access Keys.
Configuration
// config/mixpanel-api.php
return [
'token' => env('MIXPANEL_TOKEN', ''),
'api_key' => env('MIXPANEL_API_KEY', ''),
'secret' => env('MIXPANEL_SECRET', ''),
'project_id' => env('MIXPANEL_PROJECT_ID', ''),
];
Usage
The package is resolved via the MixpanelApi facade or by injecting JeffersonGoncalves\MixpanelApi\MixpanelApi.
Track an event
use JeffersonGoncalves\MixpanelApi\Facades\MixpanelApi;
MixpanelApi::trackEvent('signup', $user->id, [
'plan' => 'pro',
]);
Update a user profile
MixpanelApi::setProfile($user->id, [
'$email' => $user->email,
'$name' => $user->name,
]);
Query events (Insights)
$insights = MixpanelApi::queryEvents('purchase', '2026-08-01', '2026-08-31');
fromDate/toDate default to the last 30 days when omitted.
Get a funnel
$funnel = MixpanelApi::getFunnel('42', '2026-08-01', '2026-08-31');
Get retention data
$retention = MixpanelApi::getRetention('2026-08-01', '2026-08-31', 'signup');
Export raw events
$ndjson = MixpanelApi::exportEvents('2026-08-01', '2026-08-31', 'signup');
foreach (explode(PHP_EOL, trim($ndjson)) as $line) {
$event = json_decode($line, true);
}
The export endpoint returns newline-delimited JSON (one event per line), not a single JSON document, so the raw response body is returned for the caller to parse.
Error handling
Calling a method without the credentials it requires throws InvalidArgumentException:
try {
MixpanelApi::trackEvent('signup', $user->id);
} catch (InvalidArgumentException $e) {
logger()->error($e->getMessage());
}
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome. Please open an issue or pull request on GitHub.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.