PHPackages                             secrethash/laravel-mixpanel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. secrethash/laravel-mixpanel

ActiveLibrary

secrethash/laravel-mixpanel
===========================

Mixpanel Bridge for Laravel

v0.1.3(1y ago)1103↓100%[3 PRs](https://github.com/secrethash/laravel-mixpanel/pulls)MITPHPPHP ^8.2CI passing

Since Nov 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/secrethash/laravel-mixpanel)[ Packagist](https://packagist.org/packages/secrethash/laravel-mixpanel)[ Docs](https://github.com/secrethash/laravel-mixpanel)[ GitHub Sponsors](https://github.com/secrethash)[ RSS](/packages/secrethash-laravel-mixpanel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (8)Used By (0)

Mixpanel for Laravel
====================

[](#mixpanel-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6b9f894952ce25d14fdebfc6033ce177c3bb77178584af90108e15bd75f25bf0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736563726574686173682f6c61726176656c2d6d697870616e656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/secrethash/laravel-mixpanel)[![Tests](https://camo.githubusercontent.com/00ef9674134064bd2c96faf9c483c70841ed40db81c1cbd27f5d8f25e04308c1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736563726574686173682f6c61726176656c2d6d697870616e656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/secrethash/laravel-mixpanel/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/399dad49af56109c6b9c1c5451ad7b3b973e4a203256a7eedcb8b4bf944cbb99/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736563726574686173682f6c61726176656c2d6d697870616e656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/secrethash/laravel-mixpanel)

---

> 🚧 Under active development, but might be useable.
>
> 🌟 Contributions are welcomed &amp; appreciated.

---

[![secrethash/laravel-mixpanel Banner](https://camo.githubusercontent.com/82187a320f65fbdc2b866394015a1b967ca0049dd96e735f1653b42ae7d42fac/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304d697870616e656c2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d736563726574686173682532466c61726176656c2d6d697870616e656c267061747465726e3d67726170685061706572267374796c653d7374796c655f31266465736372697074696f6e3d4d697870616e656c2b5048502b53444b2b6272696467652b666f722b4c61726176656c266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d63686172742d706965)](https://camo.githubusercontent.com/82187a320f65fbdc2b866394015a1b967ca0049dd96e735f1653b42ae7d42fac/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304d697870616e656c2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d736563726574686173682532466c61726176656c2d6d697870616e656c267061747465726e3d67726170685061706572267374796c653d7374796c655f31266465736372697074696f6e3d4d697870616e656c2b5048502b53444b2b6272696467652b666f722b4c61726176656c266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d63686172742d706965)

This package provides a sane [Mixpanel PHP SDK](https://github.com/mixpanel/mixpanel-php/) bridge for Laravel applications.

Pre-requisites
--------------

[](#pre-requisites)

- Laravel: `^9.x`
- PHP: `^8.2`
- Mixpanel API Project Token

Installation
------------

[](#installation)

1. Install the package via composer:

    ```
    composer require secrethash/laravel-mixpanel
    ```
2. Publish the Migrations and Config:

    - Publish all resources \[`migrations`, `config`\]:

        ```
        php artisan vendor:publish --provider="Secrethash\Mixpanel\MixpanelServiceProvider"
        ```
    - Publish only migrations:

        ```
        php artisan vendor:publish --tag="laravel-mixpanel-migrations"
        ```
    - Publish only config:

        ```
        php artisan vendor:publish --tag="laravel-mixpanel-config"
        ```

Idealogies
----------

[](#idealogies)

### 1. Events &amp; Listeners

[](#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

[](#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

[](#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

[](#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:

ConsumerProviderDescriptionLiveDebugging`dry`CustomUsed for Dry Runs❌✅`socket`MixpanelSocket Connection based consumer✅✅`curl`MixpanelcUrl based consumer✅✅`file`MixpanelFile based consumer✅✅> Debugging can be enabled by setting config `laravel-mixpanel.debug.enabled` to `true`. More details in the [debugging section](#debugging)

Usage
-----

[](#usage)

1. Tracking an event is as easy as:

    ```
    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:

    ```
    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:

    ```
    php artisan about
    ```

    ```
    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
--------

[](#examples)

### 1. Tracking an Order Successful Event

[](#1-tracking-an-order-successful-event)

- Order **Event**

    ```
