PHP code example of plusinfolab / cashier-saas-metrics

1. Go to this page and download the library: Download plusinfolab/cashier-saas-metrics library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

plusinfolab / cashier-saas-metrics example snippets


return [
    'provider' => env('SAAS_METRICS_PROVIDER', 'stripe'),
    'base_currency' => env('SAAS_METRICS_BASE_CURRENCY', 'USD'),
    // ... other configuration
];

use PlusInfoLab\CashierSaaSMetrics\Facades\Metrics;

// Get current MRR
$mrr = Metrics::mrr()->calculate();
echo $mrr->formattedAsCurrency(); // "$42,500.00"

// Get churn rate
$churnRate = Metrics::churnRate()->calculate();
echo $churnRate->formattedAsPercentage(); // "4.25%"

// Get LTV
$ltv = Metrics::lifetimeValue()->calculate();
echo $ltv->formattedAsCurrency(); // "$1,200.00"

// Get ARPU
$arpu = Metrics::arpu()->calculate();
echo $arpu->formattedAsCurrency(); // "$85.00"

// Filter by period
$mrrThisMonth = Metrics::mrr()
    ->period('last_month')
    ->calculate();

// Filter by plan
$proMrr = Metrics::mrr()
    ->plan('pro')
    ->calculate();

// Group by field
$mrrByPlan = Metrics::mrr()
    ->groupBy('plan')
    ->calculate();

// Filter by currency
$usdMrr = Metrics::mrr()
    ->currency('USD')
    ->calculate();

$cohorts = Metrics::cohorts()
    ->period('last_6_months')
    ->by('signup_month')
    ->retentionMetric('mrr')
    ->calculate();

// Returns cohort data with retention curves
foreach ($cohorts->value as $cohortKey => $cohortData) {
    foreach ($cohortData['periods'] as $period) {
        echo "Period {$period['period']}: {$period['retention_percentage']}% retention\n";
    }
}

$dashboard = Metrics::dashboard('this_month');

/*
[
    'mrr' => MetricResult(42500),
    'churn_rate' => MetricResult(0.0425),
    'ltv' => MetricResult(1200),
    'arpu' => MetricResult(85),
    'period' => 'this_month',
    'currency' => 'USD',
]
*/

// config/saas-metrics.php
'provider' => 'stripe',

'providers' => [
    'stripe' => [
        'api_key' => env('STRIPE_SECRET'),
        'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
    ],
],

'provider' => 'paddle',

'providers' => [
    'paddle' => [
        'vendor_id' => env('PADDLE_VENDOR_ID'),
        'auth_code' => env('PADDLE_AUTH_CODE'),
    ],
],

'provider' => 'custom',

'providers' => [
    'custom' => [
        'models' => [
            'subscription' => App\Models\Subscription::class,
            'payment' => App\Models\Payment::class,
        ],
        'field_map' => [
            'id' => 'id',
            'status' => 'status',
            'amount' => 'amount',
            // ... map your model fields
        ],
    ],
],

use PlusInfoLab\CashierSaaSMetrics\Events\SubscriptionCreated;
use PlusInfoLab\CashierSaaSMetrics\Events\SubscriptionCancelled;
use PlusInfoLab\CashierSaaSMetrics\Events\SubscriptionUpdated;

// When a subscription is created
event(new SubscriptionCreated($subscriptionId, $customerId));

// When a subscription is cancelled
event(new SubscriptionCancelled($subscriptionId, $customerId));

// When a subscription is updated
event(new SubscriptionUpdated($subscriptionId, $customerId, $changes));

// Clear all metrics cache
Metrics::clearCache();

// Clear specific metric cache
Metrics::clearMetricCache('mrr');
bash
php artisan vendor:publish --tag="saas-metrics-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="saas-metrics-config"