PHPackages                             mike-bronner/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. mike-bronner/laravel-mixpanel

ActiveLibrary

mike-bronner/laravel-mixpanel
=============================

MixPanel wrapper for Laravel.

13.0.0(1mo ago)122056MITPHPPHP ^8.2CI passing

Since May 30Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/mike-bronner/laravel-mixpanel)[ Packagist](https://packagist.org/packages/mike-bronner/laravel-mixpanel)[ GitHub Sponsors](https://github.com/mikebronner)[ RSS](/packages/mike-bronner-laravel-mixpanel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (107)Used By (0)

MixPanel for Laravel
====================

[](#mixpanel-for-laravel)

[![Scrutinizer](https://camo.githubusercontent.com/91850facd38e3bba3a498a00b174938538b246d9564fb84a92d3a9512e55fbc6/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f47656e65614c6162732f6c61726176656c2d6d697870616e656c2e737667)](https://scrutinizer-ci.com/g/GeneaLabs/laravel-mixpanel)[![Coveralls](https://camo.githubusercontent.com/d4a16f40011a8ec05f2b12022bfb29c00842b9dc6a125b17f68f01b30c2a8c8b/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f47656e65614c6162732f6c61726176656c2d6d697870616e656c2e737667)](https://coveralls.io/github/GeneaLabs/laravel-mixpanel)[![GitHub (pre-)release](https://camo.githubusercontent.com/658aa49b0144f74865975e5bec33b52c73733cffb7e5305d9343173e0332ffa3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f47656e65614c6162732f6c61726176656c2d6d697870616e656c2f616c6c2e737667)](https://github.com/GeneaLabs/laravel-mixpanel)[![Packagist](https://camo.githubusercontent.com/f42ed15acef7cdc9b3bbfea0d8f996e546114a812f9be4509cb2cd00ceaf03a7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f47656e65614c6162732f6c61726176656c2d6d697870616e656c2e737667)](https://packagist.org/packages/genealabs/laravel-mixpanel)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/GeneaLabs/laravel-mixpanel/master/LICENSE)

[![Mixpanel for Laravel masthead image.](https://repository-images.githubusercontent.com/42419266/0f534200-f1b5-11e9-9ca7-57b0e1fe7764)](https://repository-images.githubusercontent.com/42419266/0f534200-f1b5-11e9-9ca7-57b0e1fe7764)

Sponsors
--------

[](#sponsors)

We like to thank the following sponsors for their generosity. Please take a moment to check them out.

- [LIX](https://lix-it.com)

Features
--------

[](#features)

- Asynchronous data transmission to Mixpanel's services. This prevents any delays to your application if Mixpanel is down, or slow to respond.
- Drop-in installation and configuration into your Laravel app, tracking the most common events out of the box.
- Simple Stripe integration allowing you to track revenues at the user level.
- Front-end-ready Mixpanel JS library, both for Laravel Elixir inclusion or Blade template use.

Requirements and Compatibility
------------------------------

[](#requirements-and-compatibility)

LaravelPHPPackage11.x^8.2^2.012.x^8.2^2.013.x^8.3^2.0### Legacy Versions

[](#legacy-versions)

- [Laravel 5.2](https://github.com/GeneaLabs/laravel-mixpanel/tree/afcf3737412c1aebfa9dd1d7687001f78bdb3956)
- [Laravel 5.0](https://github.com/GeneaLabs/laravel-mixpanel/tree/ce110ebd89658cbf8a91f2cfb5db57e2b449e7f3)

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

[](#installation)

1. Install the package: ```
    composer require genealabs/laravel-mixpanel
    ```
2. Add your Mixpanel API Token to your `.env` file: ```
    MIXPANEL_TOKEN=xxxxxxxxxxxxxxxxxxxxxx
    ```
3. Add the MixPanel Host domain only if you need to change your MixPanel host from the default: ```
    MIXPANEL_HOST=xxxxxxxxxxxxxxxxxxxxxx
    ```

Configuration
-------------

[](#configuration)

### Publishing the Config

[](#publishing-the-config)

You can publish the configuration file to customize it:

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

This will create a `config/mixpanel.php` file in your application.

### Default Values

[](#default-values)

- `mixpanel.host`: pulls the 'MIXPANEL\_HOST' value from your `.env`file.
- `mixpanel.token`: pulls the 'MIXPANEL\_TOKEN' value from your `.env`file.
- `mixpanel.enable-default-tracking`: (default: true) enable or disable Laravel user event tracking.
- `mixpanel.consumer`: (default: socket) set the Guzzle adapter you want to use.
- `mixpanel.connect-timeout`: (default: 2) set the number of seconds after which connections timeout.
- `mixpanel.timeout`: (default: 2) set the number of seconds after which event tracking times out.
- `mixpanel.data_callback_class`: (default: null) manipulate the data being passed back to mixpanel for the track events.

### Migrating from services.mixpanel

[](#migrating-from-servicesmixpanel)

If you are upgrading from a previous version that used `config/services.php`for Mixpanel configuration, the package will automatically fall back to your existing `services.mixpanel.*` values. However, we recommend migrating to the new `config/mixpanel.php` file:

1. Run `php artisan vendor:publish --tag=mixpanel-config`
2. Remove the `mixpanel` key from your `config/services.php`
3. Update any direct references from `config('services.mixpanel.*')` to `config('mixpanel.*')`

Upgrade Notes
-------------

[](#upgrade-notes)

### 🚨 Version 13.0.0 (Breaking Change)

[](#-version-1300-breaking-change)

Configuration has moved from `config/services.php` (`services.mixpanel.*`) to a dedicated `config/mixpanel.php` file. If you reference Mixpanel config values directly (e.g. `config('services.mixpanel.token')`), you must update them to `config('mixpanel.token')`. The package will automatically fall back to legacy `services.mixpanel.*` values for keys that are not set in the new config, but this fallback will be removed in a future release. See [Migrating from services.mixpanel](#migrating-from-servicesmixpanel) for steps.

### Version 0.7.0 for Laravel 5.5

[](#version-070-for-laravel-55)

- Remove the service provider from `/config/app.php`. The service provider is now auto-discovered in Laravel 5.5.

### Page Views

[](#page-views)

- Page view tracking has been removed in favor of Mixpanels in-built Autotrack functionality, which tracks all page views. To turn it on, visit your Mixpanel dashboard, click *Applications &gt; Autotrack &gt; Web &gt; etc.* and enable Autotracking.

Usage
-----

[](#usage)

MixPanel is loaded into the IoC as a singleton. This means you don't have to manually call $mixPanel::getInstance() as described in the MixPanel docs. This is already done for you in the ServiceProvider.

Common user events are automatically recorded:

- User Registration
- User Deletion
- User Login
- User Login Failed
- User Logoff
- Cashier Subscribed
- Cashier Payment Information Submitted
- Cashier Subscription Plan Changed
- Cashier Unsubscribed

To make custom events, simple get MixPanel from the IoC using DI:

```
use GeneaLabs\LaravelMixpanel\LaravelMixpanel;

class MyClass
{
    protected $mixPanel;

    public function __construct(LaravelMixPanel $mixPanel)
    {
        $this->mixPanel = $mixPanel;
    }
}
```

If DI is impractical in certain situations, you can also manually retrieve it from the IoC:

```
$mixPanel = app('mixpanel'); // using app helper
$mixPanel = Mixpanel::getFacadeRoot(); // using facade
```

After that you can make the usual calls to the MixPanel API:

- `$mixPanel->identify($user->id);`
- `$mixPanel->track('User just paid!');`
- `$mixPanel->people->trackCharge($user->id, '9.99');`
- `$mixPanel->people->set($user->id, [$data]);`

    And so on ...

    ### Stripe Web-Hook

    [](#stripe-web-hook)

    If you wish to take advantage of the Stripe web-hook and track revenue per user, you should install Cashier:

    Once that has been completed, exempt the web-hook endpoint from CSRF-validation in `/app/Http/Middleware/VerifyCsrfToken.php`:

    ```
        protected $except = [
            'genealabs/laravel-mixpanel/stripe',
        ];
    ```

    The only other step remaining is to register the web-hook with Stripe: Log into your Stripe account: , and open your account settings' webhook tab:

    Enter your MixPanel web-hook URL, similar to the following: `http:///genealabs/laravel-mixpanel/stripe`: [![screen shot 2015-05-31 at 1 35 01 pm](https://cloud.githubusercontent.com/assets/1791050/7903765/53ba6fe4-079b-11e5-9f92-a588bd81641d.png)](https://cloud.githubusercontent.com/assets/1791050/7903765/53ba6fe4-079b-11e5-9f92-a588bd81641d.png)

    Be sure to select "Live" if you are actually running live (otherwise put into test mode and update when you go live). Also, choose "Send me all events" to make sure Laravel Mixpanel can make full use of the Stripe data.

    ### JavaScript Events &amp; Auto-Track

    [](#javascript-events--auto-track)

    #### Blade Template (Recommended)

    [](#blade-template-recommended)

    First publish the necessary assets:

    ```
    php artisan mixpanel:publish --assets
    ```

    Then add the following to the head section of your layout template (already does the init call for you, using the token from your .env file):

    ```
    @include('genealabs-laravel-mixpanel::partials.mixpanel')
    ```

    #### Laravel Elixir

    [](#laravel-elixir)

    Add the following lines to your `/resources/js/app.js` (or equivalent), and don't forget to replace `YOUR_MIXPANEL_TOKEN` with your actual token:

    ```
    require('./../../../public/genealabs-laravel-mixpanel/js/mixpanel.js');
    mixpanel.init("YOUR_MIXPANEL_TOKEN");
    ```

### Laravel Integration

[](#laravel-integration)

Out of the box it will record the common events anyone would want to track. Also, if the default `$user->name` field is used that comes with Laravel, it will split up the name and use the last word as the last name, and everything prior for the first name. Otherwise it will look for `first_name` and `last_name` fields in the users table.

- User registers:

    ```
    Track:
      User:
        - Status: Registered
    People:
      - $first_name:
      - $email:
      - $last_name:
      - $created:

    ```
- User is deleted:

    ```
    Track:
      User:
        - Status: Deactivated

    ```
- User is restored (from soft-deletes):

    ```
    Track:
      User:
        - Status: Reactivated

    ```
- User logs in:

    ```
    Track:
      Session:
        - Status: Logged In
    People:
      - $first_name:
      - $email:
      - $last_name:
      - $created:

    ```
- User logs out:

    ```
    Track:
      Session:
        - Status: Logged Out

    ```

### Tracking Data Manipulation

[](#tracking-data-manipulation)

If you need to make changes or additions to the data being tracked, create a class that implements `\GeneaLabs\LaravelMixpanel\Interfaces\DataCallback`:

```
