Blog Detail

26

Jul
Track Application Stats and Their Change Over Time cover image

arrow_back Track Application Stats and Their Change Over Time

Spatie comes up with a new package known as Laravel Stats, which can be utilized to easily track application stats like orders, subscriptions, and users and their change over time. It is a lightweight and user-friendly solution for compiling changes in your database over time.

Installation

You can install the package via composer by running this command:

composer require spatie/laravel-stats

Next, You’ve to publish and run the migrations with:

php artisan vendor:publish --provider="Spatie\Stats\StatsServiceProvider" --tag="stats-migrations"
php artisan migrate

Tracking changes

In this example, we will track the number of subscriptions and cancellations over time.
Initially, you should create a stats class.

use Spatie\Stats\BaseStats;
class SubscriptionStats extends BaseStats {
}

After that, you can increase when someone subscribes and decrease when someone cancels their plan.

SubscriptionStats::increase(); // execute whenever somebody subscribes
SubscriptionStats::decrease() // execute whenever somebody cancels the subscription;

Once you have assembled stats, you can query statistics with this package using the given StatsQuery API:

use Spatie\Stats\StatsQuery;
$stats = StatsQuery::for(SubscriptionStats::class)
    ->start(now()->subMonths(2))
    ->end(now()->subSecond())
    ->groupByWeek()
    ->get();

It will pass an array like this one:

[
    [
        '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,
    ],
]

Instead of manually increasing and decreasing the stat, you can instantly set it. It is beneficial when your particular stat does not get estimated by your own app but exists elsewhere. If we use the subscription example, let’s assume that subscriptions live elsewhere and that there’s an API call to get the count.

$count = AnAPi::getSubscriptionCount(); 

SubscriptionStats::set($count);

By default, that increase, decrease and sets methods to believe that the event that induced your stats to change occurred right now. Optionally, you can return a date-time as a second parameter to these methods. Your stat change will be recorded as if it happened at that time.

SubscriptionStats::increase(1, $subscription->created_at); 

If you want to study more about this package, You can get its full installation instructions and the source code on GitHub. spatie / laravel-stats

Published at : 26-07-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project