I’m proud to announce that our team has released a new package called spatie/laravel-stats. This package is a lightweight solution for summarizing changes in your database over time.

Tracking changes

Here’s a quick example where we will track the number of subscriptions and cancellations over time.

First, you should create a stats class.

<span class="hljs-keyword">use</span> <span class="hljs-title">Spatie</span><span class="hljs-title">Stats</span><span class="hljs-title">BaseStats</span>;
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SubscriptionStats</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseStats</span> </span>{}
</p>

Next, you can call increase when somebody subscribes and decrease when somebody cancels their plan.

SubscriptionStats::increase(); <span class="hljs-comment">// execute whenever somebody subscribes</span>
SubscriptionStats::decrease() <span class="hljs-comment">// execute whenever somebody cancels the subscription;</span>

With this in place, you can query the stats. Here’s how you can get the subscription stats for the past two months,
grouped by week.

<span class="hljs-keyword">use</span> <span class="hljs-title">Spatie</span><span class="hljs-title">Stats<span class="hljs-title">StatsQuery;
<p>$stats = StatsQuery::for(SubscriptionStats::class)
->start(now()->subMonths(<span class="hljs-number">2</span>))
->end(now()->subSecond())
->groupByWeek()
->get();
</p>

This will return an array like this one:

[
[
<span class="hljs-string">'start'</span> => <span class="hljs-string">'2020-01-01'</span>,
<span class="hljs-string">'end'</span> => <span class="hljs-string">'2020-01-08'</span>,
<span class="hljs-string">'value'</span> => <span class="hljs-number">102</span>,
<span class="hljs-string">'increments'</span> => <span class="hljs-number">32</span>,
<span class="hljs-string">'decrements'</span> => <span class="hljs-number">20</span>,
<span class="hljs-string">'difference'</span> => <span class="hljs-number">12</span>,
],
[
<span class="hljs-string">'start'</span> => <span class="hljs-string">'2020-01-08'</span>,
<span class="hljs-string">'end'</span> => <span class="hljs-string">'2020-01-15'</span>,
<span class="hljs-string">'value'</span> => <span class="hljs-number">114</span>,
<span class="hljs-string">'increments'</span> => <span class="hljs-number">63</span>,
<span class="hljs-string">'decrements'</span> => <span class="hljs-number">30</span>,
<span class="hljs-string">'difference'</span> => <span class="hljs-number">33</span>,
],
]

Instead of manually increasing and decreasing the stat, you can directly set it. This is useful when your particular stat does not get calculated by your own app but lives elsewhere. Using the subscription example, let’s imagine that subscriptions live elsewhere and that there’s an API call to get the count.

$count = AnAPi::getSubscriptionCount();
<p>SubscriptionStats::set($count);
</p>

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

SubscriptionStats::increase(<span class="hljs-number">1</span>, $subscription->created_at);

How it works on under the hood

The implementation of this package is simple. The basic principles of event sourcing are being used: we don’t store a result, but only the changes.

The package stores all “events” in the stats_event table

screenshot

Inside the StatsQuery class, you’ll find the heart of the package. In its get function, you can see that all events for a given period are retrieved, summarized and mapped to DataPoint classes.

In closing

We are going to use laravel-stats in Flare, our exception tracker for Laravel/PHP/JavaScript projects, to keep tracker of changes in subscribers and other key metrics. We hope that the package can be helpful in your projects as well.

As mentioned above, the package uses a lightweight event sourcy approach. If you want to know more about event sourcing, check out our upcoming premium course on event sourcing in Laravel.

I’d like to thank my colleague Alex, who did the bulk of the work creating spatie/laravel-stats.

Do also take a look at this list of packages our team has created previously. I’m sure there’s something there for your next project.

Categories: PHP