• Reading time ~ 1 min
  • 24.08.2022

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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297