Installation:
composer require gordalina/mixpanel-bundle:~3.0
Register the bundle in config/bundles.php:
Castrocrea\MixpanelBundle\CarstrocreaMixpanelBundle::class => ['all' => true],
Configure:
Add your Mixpanel token to config/packages/castrocrea_mixpanel.yaml:
castrocrea_mixpanel:
projects:
default:
token: "%env(MIXPANEL_TOKEN)%"
(Use .env for sensitive tokens.)
First Use Case: Track a user event in a controller:
use Castrocrea\MixpanelBundle\Service\Mixpanel;
class UserController extends AbstractController
{
public function trackEvent(Mixpanel $mixpanel)
{
$mixpanel->track('User Registered', [
'user_id' => '123',
'email' => 'user@example.com',
'plan' => 'premium'
]);
}
}
Dependency Injection:
Inject Mixpanel service into controllers/services:
public function __construct(private Mixpanel $mixpanel) {}
Event Tracking:
$this->mixpanel->track('Product Viewed', ['product_id' => 42]);
$this->mixpanel->peopleSet('user123', ['plan' => 'free']);
$this->mixpanel->peopleIncrement('user123', 'login_count');
Annotations (Legacy):
Use @Mixpanel\Track on controller methods (if annotations are enabled in config):
/**
* @Mixpanel\Track(event="Checkout Started")
*/
public function checkout() {}
Symfony Profiler Integration: Enable in config:
castrocrea_mixpanel:
profiler: true
View tracked events in the Symfony Profiler toolbar.
Environment-Specific Tokens:
Use .env variables and parameter bags:
token: "%env(MIXPANEL_TOKEN)%"
php bin/console debug:container castrocrea_mixpanel
Batching Events: For high-traffic apps, batch events to reduce API calls:
$this->mixpanel->batch([
['event' => 'View', 'properties' => ['page' => 'home']],
['event' => 'Click', 'properties' => ['button' => 'cta']]
]);
User Identification:
Always include distinct_id (e.g., user_id or device_id) for accurate tracking:
$this->mixpanel->track('Custom Event', ['distinct_id' => $user->id]);
Async Tracking: Offload tracking to a queue (e.g., Symfony Messenger) for performance:
$this->messageBus->dispatch(new TrackEvent('event_name', $properties));
Token Leaks:
.env or parameter bags..env to gitignore.Rate Limits:
$this->mixpanel->setBatchSize(10); // Default is 1
Annotations Deprecation:
The @Mixpanel\Track annotation is undocumented and may break. Prefer DI-based tracking.
Profiler Overhead: Disabling profiler in production:
castrocrea_mixpanel:
profiler: "%kernel.debug%" # Only in dev
Enable Logging:
Add to config/packages/monolog.yaml:
handlers:
mixpanel:
type: stream
path: "%kernel.logs_dir%/mixpanel.log"
level: debug
Then enable logging in the bundle:
castrocrea_mixpanel:
debug: true
Validate Events: Use Mixpanel’s debugger to verify events.
Common Errors:
InvalidTokenException: Check your token in .env or config.NetworkError: Ensure your server can reach mixpanel.com.Custom Event Builders:
Create a decorator for Mixpanel service to add domain-specific logic:
class CustomMixpanel extends Mixpanel
{
public function trackPurchase(User $user, Product $product)
{
$this->track('Purchase', [
'user_id' => $user->id,
'product_id' => $product->id,
'amount' => $product->price,
'currency' => 'USD'
]);
}
}
Register as a service:
services:
App\Service\CustomMixpanel:
decorates: 'castrocrea_mixpanel'
arguments: ['@castrocrea_mixpanel.inner']
Event Normalization: Use a subscriber to standardize event properties before sending:
class MixpanelEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => 'onKernelView',
];
}
public function onKernelView(ViewEvent $event)
{
$event->getController()?->__invoke($event->getRequest());
// Normalize properties here
}
}
Multi-Tenant Support: Extend the config to support multiple projects:
castrocrea_mixpanel:
projects:
project_a:
token: "%env(MIXPANEL_TOKEN_A)%"
project_b:
token: "%env(MIXPANEL_TOKEN_B)%"
Switch projects dynamically:
$this->mixpanel->setProject('project_b');
How can I help you explore Laravel packages today?