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
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";
}
}
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');