• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Intuitive drop-in analytics.

MixPanel for Laravel

Scrutinizer Coveralls GitHub (pre-)release Packagist GitHub license

Mixpanel for Laravel masthead image.

Sponsors

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

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

  • PHP >= 7.2
  • Laravel >= 8.0

Legacy Versions

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_TOKEN=xxxxxxxxxxxxxxxxxxxxxx

Configuration

Default Values

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

Upgrade Notes

Version 0.7.0 for Laravel 5.5

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

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 > Autotrack > Web > etc. and enable Autotracking.

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

    If you wish to take advantage of the Stripe web-hook and track revenue per user, you should install Cashier: https://www.laravel.com/docs/5.5/billing

    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: https://dashboard.stripe.com/dashboard, and open your account settings' webhook tab:

    Enter your MixPanel web-hook URL, similar to the following: http://<your server.com>/genealabs/laravel-mixpanel/stripe: screen shot 2015-05-31 at 1 35 01 pm

    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 & Auto-Track

    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

    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

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: <user's first name>
      - $last_name: <user's last name>
      - $email: <user's email address>
      - $created: <date user registered>
    
  • User is updated:

    People:
      - $first_name: <user's first name>
      - $last_name: <user's last name>
      - $email: <user's email address>
      - $created: <date user registered>
    
  • 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: <user's first name>
      - $last_name: <user's last name>
      - $email: <user's email address>
      - $created: <date user registered>
    
  • User login fails:

    Track:
      Session:
        - Status: Login Failed
    People:
      - $first_name: <user's first name>
      - $last_name: <user's last name>
      - $email: <user's email address>
      - $created: <date user registered>
    
  • User logs out:

    Track:
      Session:
        - Status: Logged Out
    

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:

<?php

namespace App;

use GeneaLabs\LaravelMixpanel\Interfaces\DataCallback;

class MixpanelUserData implements DataCallback
{
    public function process(array $data = []) : array
    {
        $data["test"] = "value";

        return $data;
    }
}

Then register this class in your services configuration:

    'mixpanel' => [
      // ...
        "data_callback_class" => \App\MixpanelUserData::class,
    ]

Stripe Integration

Many L5 sites are running Cashier to manage their subscriptions. This package creates an API webhook endpoint that keeps vital payment analytics recorded in MixPanel to help identify customer churn.

Out of the box it will record the following Stripe events in MixPanel for you:

Charges

  • Authorized Charge (when only authorizing a payment for a later charge date):

    Track:
      Payment:
        - Status: Authorized
        - Amount: <amount authorized>
    
  • Captured Charge (when completing a previously authorized charge):

    Track:
      Payment:
        - Status: Captured
        - Amount: <amount of payment>
    People TrackCharge: <amount of intended payment>
    
  • Completed Charge:

    Track:
      Payment:
        - Status: Successful
        - Amount: <amount of payment>
    People TrackCharge: <amount of payment>
    
  • Refunded Charge:

    Track:
      Payment:
        - Status: Refunded
        - Amount: <amount of refund>
    People TrackCharge: -<amount of refund>
    
  • Failed Charge:

    Track:
      Payment:
        - Status: Failed
        - Amount: <amount of intended payment>
    

Subscriptions

  • Customer subscribed:

    Track:
      Subscription:
        - Status: Created
    People:
      - Subscription: <plan name>
    
  • Customer unsubscribed:

    Track:
      Subscription:
        - Status: Canceled
        - Upgraded: false
      Churn! :(
    People:
      - Subscription: None
      - Churned: <date canceled>
      - Plan When Churned: <subscribed plan when canceled>
      - Paid Lifetime: <number of days from subscription to cancelation> days
    
  • Customer started trial:

    Track:
      Subscription:
        - Status: Trial
    People:
      - Subscription: Trial
    
  • Customer upgraded plan:

    Track:
      Subscription:
        - Upgraded: true
      Unchurn! :-)
    People:
      - Subscription: <new plan name>
    
  • Customer downgraded plan (based on dollar value compared to previous plan):

    Track:
      Subscription:
        - Upgraded: false
      Churn! :-(
    People:
      - Subscription: <new plan name>
      - Churned: <date plan was downgraded>
      - Plan When Churned: <plan name prior to downgrading>
    

The Fine Print

Commitment to Quality

During package development I try as best as possible to embrace good design and development practices to try to ensure that this package is as good as it can be. My checklist for package development includes:

  • ✅ Achieve as close to 100% code coverage as possible using unit tests.
  • ✅ Eliminate any issues identified by SensioLabs Insight and Scrutinizer.
  • Be fully PSR1, PSR2, and PSR4 compliant.
  • ✅ Include comprehensive documentation in README.md.
  • Provide an up-to-date CHANGELOG.md which adheres to the format outlined at http://keepachangelog.com.
  • Have no PHPMD or PHPCS warnings throughout all code.

Contributing

Please observe and respect all aspects of the included Code of Conduct https://github.com/GeneaLabs/laravel-model-caching/blob/master/CODE_OF_CONDUCT.md.

Reporting Issues

When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.

Submitting Pull Requests

Please review the Contribution Guidelines https://github.com/GeneaLabs/laravel-model-caching/blob/master/CONTRIBUTING.md. Only PRs that meet all criterium will be accepted.

❤️ Open-Source Software - Give ⭐️

We have included the awesome symfony/thanks composer package as a dev dependency. Let your OS package maintainers know you appreciate them by starring the packages you use. Simply run composer thanks after installing this package. (And not to worry, since it's a dev-dependency it won't be installed in your live environment.)

More Repositories

1

laravel-model-caching

Eloquent model-caching made easy.
PHP
2,246
star
2

laravel-caffeine

Keeping Your Laravel Forms Awake.
PHP
921
star
3

laravel-sign-in-with-apple

Provide "Sign In With Apple" functionality to your Laravel app.
PHP
443
star
4

laravel-governor

Manage authorization with granular role-based permissions in your Laravel Apps.
PHP
183
star
5

laravel-messenger

Notifying your users doesn't have to be a lot of work.
PHP
135
star
6

nova-map-marker-field

Provides an visual interface for editing latitude and longitude coordinates.
Vue
131
star
7

laravel-pivot-events

PHP
114
star
8

nova-gutenberg

Implementation of the Gutenberg editor as a Laravel Nova Field based on Laraberg.
Vue
107
star
9

laravel-socialiter

Automatically manage user persistence and resolution for any Laravel Socialite provider.
PHP
101
star
10

laravel-casts

Form builder for Laravel.
PHP
93
star
11

laravel-maps

Easy - peasy - maps for Laravel
PHP
81
star
12

laravel-overridable-model

Provide a uniform method of allowing models to be overridden in Laravel.
PHP
63
star
13

nova-file-upload-field

The easiest drag-and-drop file uploading field for Laravel Nova.
Vue
53
star
14

laravel-multi-step-progressbar

Quickly implement a multi-step progress-bar in Laravel.
PHP
45
star
15

laravel-impersonator

Package to allow user-impersonation in your Laravel and Nova apps.
PHP
45
star
16

laravel-optimized-postgres

Implement all textual fields as `text` in Postgres databases.
PHP
41
star
17

laravel-null-carbon

Carbon null-class.
PHP
39
star
18

nova-prepopulate-searchable

A tool to allow BelongsTo searchable fields to be pre-populated with data
Vue
37
star
19

laravel-changelog

Easily integrate and display your changelog in your app.
PHP
37
star
20

laravel-appleseed

Prevent the pesky Apple Touch Icon error log entries.
PHP
30
star
21

Phpgmaps

Larvel 5 implementation of BIOSTALL/CodeIgniter-Google-Maps-V3-API-Library.
PHP
27
star
22

nova-passport-manager

Manage Passport clients and tokens from within Nova.
Vue
26
star
23

nova-multi-tenant-manager

Manage tenants and their settings in Laravel Nova.
Vue
21
star
24

interesting-packages-and-articles

List of packages and articles we are interested in using that fulfill specific needs.
17
star
25

nova-telescope

Integrate Laravel Telescope into Laravel Nova.
PHP
17
star
26

tailwind-mac-colors

Color palette and swatches for macOS's color picker.
16
star
27

php-coding-standards

Custom PHPCS sniffs that support all our coding standards.
Shell
15
star
28

action-reviewdog-phpstan

🐾 Run PHPStan with ReviewDog.
Shell
12
star
29

nova-horizon

Integrate Laravel Horizon into Laravel Nova.
PHP
10
star
30

laravel-registrar

PHP
9
star
31

laravel-tawk

Easily and quickly integrate Tawk.to LiveChat into your laravel app in under 5 minutes!
PHP
8
star
32

tailwindcss-sketch-design-system

An atomic design system for TailwindCSS implemented in Sketch.
CSS
8
star
33

cashier-paypal

Laravel Cashier-compatible billing for PayPal
PHP
7
star
34

panic-nova-intelephense.novaextension

JavaScript
6
star
35

laravel-dev-environment

A collection of scripts and tutorials to optimize your Laravel development environment.
Shell
6
star
36

laravel-imagery

Serve contextually optimized images to speed up page rendering and improve the user experience!
PHP
6
star
37

laravel-authorization-addons

Additional helper methods and blade directives to help with more complex authorization queries.
PHP
6
star
38

action-reviewdog-phpmd

🐾 Run PHP Mess Detector with ReviewDog.
Shell
5
star
39

awesome-laravel-ignition-plugins

A curated list of plugins for the Laravel Igniter error package.
5
star
40

documentation

Documentation for all our open-source packages.
CSS
4
star
41

laravel-cashier-braintree

PHP
4
star
42

laravel-whoops-atom

Open Whoops Stack Trace Files in Atom Editor.
PHP
3
star
43

laravel-codespaces-app-template

Template for creating Laravel apps in codespaces.
PHP
3
star
44

action-reviewdog-phpcs

Shell
3
star
45

bootstrap-css-grid-override

Update bootstrap 4 with native CSS-grid with graceful degradation.
CSS
3
star
46

credo

My beliefs on being.
2
star
47

SculptKeyboardMapping

Keyboard mapping for Microsoft Sculpt Keyboard on Mac using KeyRemap4MacBook
2
star
48

panic-nova-phpcs.novaextension

JavaScript
2
star
49

laravel-codespaces-package-template

Template for creating Laravel packages in codespaces.
PHP
2
star
50

bones-macros

useful form macros for Laravel's Blade template engine.
PHP
2
star
51

laravel-nova-morph-many-to-one

Drop-in package that adds a many-to-one (inverse of one-to-many) polymorphic relationship to Eloquent and Nova.
PHP
2
star
52

laravel-collection-macros

PHP
2
star
53

unity-heraldry-generator

Heraldic Generator created in Unity using C# with the goal of providing a common web and in-game mechanism for creating Coats of Arms.
C#
1
star
54

laravel-multi-tenant-manager

PHP
1
star
55

panic-nova-phpmd.novaextension

JavaScript
1
star
56

panic-nova-atom-keybindings

1
star
57

nova-prepopulate-searchable-old

A tool to allow BelongsTo searchable fields to be pre-populated with data.
Vue
1
star
58

laravel-two-way-attribute-casting

Perform attribute casting when saving Eloquent models to the database.
PHP
1
star