Laravel Stats is a package by Spatie to easily track application stats like orders, subscriptions, and users, and their change over time.
This package is simple to use and get started with—you extend the package's BaseStats
class, and you are ready to use this package:
use Spatie\Stats\BaseStats;
class SubscriptionStats extends BaseStats {}
Next, whenever you want to track a stat, you can increase or decrease the statistic:
// execute whenever somebody subscribes
SubscriptionStats::increase();
// execute whenever somebody cancels the subscription
SubscriptionStats::decrease();
// Set the stat directly
SubscriptionStats::set($count);
Once you've collected stats, you can query statistics with this package using the provided StatsQuery API:
$stats = SubscriptionStats::query()
->start(now()->subMonths(2))
->end(now()->subSecond())
->groupByWeek()
->get();
/*
Example results:
[
[
'start' => '2020-01-01',
'end' => '2020-01-08',
'value' => 102,
'increments' => 32,
'decrements' => 20,
'difference' => 12,
],
[
'start' => '2020-01-08',
'end' => '2020-01-15',
'value' => 114,
'increments' => 63,
'decrements' => 30,
'difference' => 33,
],
]
*/
You can learn more about this package, get full installation instructions, and view the source code on GitHub. The package author also wrote a detailed post about this package.