- This package adds the multiple routes under
genealabs/laravel-mixpanel/*
. Please verify that these don't collide with your existing routes.
- Install MixPanel via composer Laravel 5.2.x and above:
composer require genealabs/laravel-mixpanel:~0.5.1
For Laravel 5.0.x through 5.1.x:
composer require genealabs/laravel-mixpanel:0.4.14
- Add the service provider entry in
config\app.php
:
'GeneaLabs\LaravelMixpanel\Providers\LaravelMixpanelServiceProvider',
- If you are using Laravel 5.2 or above, add the following entry to
config/auth.php
. Be sure to use the correct namespace for your application. (Yes, this is a duplicate ofproviders.users.model
, but necessary for now, in case a different driver is used.
'model' => App\User::class,
- Update your
.env
file with your MixPanel token (it will automatically be picked up by the in-built configuration):
MIXPANEL_TOKEN=xxxxxxxxxxxxxxxxxxxxxx
- If you are running Laravel < 5.2, or if you want to disable the in-built default tracking and implement your own,
add the following to your services configuration (
config\services.php
):
'mixpanel' => [
'token' => env('MIXPANEL_TOKEN'),
'enable-default-tracking' => false|true,
],
Disabling the default hooks will not disable the Stripe web-hook functionality.
- If to track the user's names, make sure a
name
attribute is available on your user model. For example, if you only have ausername
field that contains the users' first and last names, you could add the following to your user model:
public function getNameAttribute()
{
return $this->username;
}
- We need to disable CSRF checking for the stripe webhook endpoints. To do that, open
app/HTTP/Middleware/VerifyCsrfToken.php
and add the following above the return statement:
if ($request->is('genealabs/laravel-mixpanel/*')) {
return $next($request);
}
For Laravel 5.2+:
protected $except = [
'genealabs/laravel-mixpanel/*',
// your other CSRF token exceptions
];
- Configure Stripe webhook (if you're using Stripe): Log into your Stripe account: https://dashboard.stripe.com/dashboard, and open your account settings' webhook tab:
Enter your MixPanel webhook URL, similar to the following: http://<your server.com>/genealabs/laravel-mixpanel/stripe/transaction
:
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 the mixpanel endpoint can make full use of the Stripe data.
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:
- Page View
- 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::make('GeneaLabs\LaravelMixPanel\LaravelMixPanel');
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 ...
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
-
View loaded:
Track: Page View: - Url: <page URL> - Route: <route name> - Referrer: <referring URL> - Referring Domain: <referring domain> - IP (for geolocation) - Browser - Operating System - Hardware
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:
-
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>
-
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>