https://github.com/secrethash/laravel-mixpanel
Mixpanel PHP SDK bridge for Laravel
https://github.com/secrethash/laravel-mixpanel
laravel mixpanel php
Last synced: about 1 year ago
JSON representation
Mixpanel PHP SDK bridge for Laravel
- Host: GitHub
- URL: https://github.com/secrethash/laravel-mixpanel
- Owner: secrethash
- License: mit
- Created: 2024-10-25T04:55:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-14T10:08:57.000Z (about 1 year ago)
- Last Synced: 2025-04-14T20:54:05.766Z (about 1 year ago)
- Topics: laravel, mixpanel, php
- Language: PHP
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Mixpanel for Laravel
[](https://packagist.org/packages/secrethash/laravel-mixpanel)
[](https://github.com/secrethash/laravel-mixpanel/actions/workflows/run-tests.yml)
[](https://packagist.org/packages/secrethash/laravel-mixpanel)
---
> :construction: Under active development, but might be useable.
>
> :star2: Contributions are welcomed & appreciated.
---

This package provides a sane [Mixpanel PHP SDK](https://github.com/mixpanel/mixpanel-php/) bridge for Laravel applications.
## Pre-requisites
- Laravel: `^9.x`
- PHP: `^8.2`
- Mixpanel API Project Token
## Installation
1. Install the package via composer:
```bash
composer require secrethash/laravel-mixpanel
```
2. Publish the Migrations and Config:
- Publish all resources [`migrations`, `config`]:
```bash
php artisan vendor:publish --provider="Secrethash\Mixpanel\MixpanelServiceProvider"
```
- Publish only migrations:
```bash
php artisan vendor:publish --tag="laravel-mixpanel-migrations"
```
- Publish only config:
```bash
php artisan vendor:publish --tag="laravel-mixpanel-config"
```
## Idealogies
### 1. Events & Listeners
We use Laravel's event and listeners to track and send data to Mixpanel. This makes it easier to hook into events as and when needed.
Although the package does not force to use events and listeners in your application while tracking, but it's still recommended for maintainability. Similarly we will also take the example of events and listeners in the examples below.
### 2. Events Naming Consistency
We enforce a validation to make sure all the events sent are an instance of the class set in config key `laravel-mixpanel.tracker.events`. This should be a string backed enum. This is done to avoid event name inconsistency on mixpanel eg: `User Registered` and `User Created`
### 3. User Auto-identification
Enabled by default, we try early identification of the user using a user tracker (uuid). Every user is given a unique uuid which is saved in the database column defined in the config key `laravel-mixpanel.tracker.database_column`.
### 4. Consumers Strategy
Consumer strategies are also bridged with the [Mixpanel PHP SDK](https://github.com/mixpanel/mixpanel-php/tree/master/lib/ConsumerStrategies) Consumer Strategies. Additionally, we have also added an custom consumer implementation `dry` which uses the custom [`Secrethash\Mixpanel\Consumers\DebugConsumer::class`](./config/laravel-mixpanel.php#L63). Currently these consumers are supported:
| Consumer | Provider | Description | Live | Debugging |
|----------|----------|-------------|------|-----------|
| `dry` | Custom | Used for Dry Runs | :x: | :white_check_mark: |
| `socket` | Mixpanel | Socket Connection based consumer | :white_check_mark: | :white_check_mark: |
| `curl` | Mixpanel | cUrl based consumer | :white_check_mark: | :white_check_mark: |
| `file` | Mixpanel | File based consumer | :white_check_mark:| :white_check_mark:|
> Debugging can be enabled by setting config `laravel-mixpanel.debug.enabled` to `true`. More details in the [debugging section](#debugging)
## Usage
1. Tracking an event is as easy as:
```php
use Secrethash\Mixpanel\Mixpanel;
use Secrethash\Mixpanel\Enums\TrackingEvents;
$data = [
'registration_status' => 'success',
'account_status' => 'verified',
];
Mixpanel::track(TrackingEvents::userRegistered, $data);
```
2. The config should to be updated to set according to the requirements. Most of the configurations can be updated from `.env`.
3. A few key configurations can be set or overridden during Runtime for flexibility:
```php
use Secrethash\Mixpanel\Mixpanel;
...
class AppServiceProvider extends ServiceProvider
{
...
public function boot()
{
...
// Enable/disable tracking for specific cases
if (environment('testing')) {
Mixpanel::$track = false;
}
// Add additional User Identity Attributes or Overwrite the set attributes
Mixpanel::$identityAttr = array_merge(
Mixpanel::$identityAttr,
['phone','status']
);
// Although this can be updated from the config in `laravel-mixpanel.tracker.database_column`,
// This can be useful to override the config value if needed
Mixpanel::$userIdentityKey = 'uuid';
}
}
```
4. The package also provides a lot of helpful integration details in the `php artisan about` command:
```bash
php artisan about
```
```txt
Laravel Mixpanel Integration .................................................................
Active .............................................................................. INACTIVE
Current State ......................................... Tracking Disabled due to missing Token
Debugging ........................................................................... DISABLED
Mixpanel Consumer ..................................................................... socket
Mixpanel Host ........................................................................ DEFAULT
Mixpanel Token ......................................................................... UNSET
User Identity Attributes ........................................... name, last_name, username
User Identity Key ........................................................... mixpanel_tracker
```
## Examples
### 1. Tracking an Order Successful Event
- Order **Event**
```php
order;
Mixpanel::track(TrackingEvents::OrderSuccessful, [
'order_id' => $order->id,
'seller' => [
'id' => $order->product->seller?->id,
'name' => $order->product->seller?->full_name,
],
'amount' => $order->amount,
'currency' => $order->currency,
'status' => $order->status,
]);
}
}
```
- **Register the event and listener** by adding the mapping to `App\Providers\EventServiceProvider::$listen`
```php
[
OrderSuccessTrackingListener::class,
],
];
...
}
```
## Debugging
All the debugging consumers are custom implementations to make the development process a breeze. All the above mentioned [consumer strategies](#4-consumers-strategy) have custom debugging consumers implemented too.
Setting the Mixpanel in debugging mode can be done by setting the `.env` variable `MIXPANEL_DEBUG=true`. Custom Debugging Consumers can also be added and replaced directly in the config `laravel-mixpanel.consumers.*`.
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Shashwat Mishra](https://github.com/secrethash)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.