Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Stats

Laravel Stats Laravel Package

spatie/laravel-stats

Lightweight Laravel package to track and summarize stat changes over time. Define a stats class, call increase/decrease on events, then query totals and increments/decrements across date ranges grouped by day/week/month.

View on GitHub
Deep Wiki
Context7

Easily track application stats like orders, subscriptions and users and their change over time

Frequently asked questions about Laravel Stats
How do I track user subscriptions or cancellations in Laravel using this package?
Create a stats class extending `BaseStats` (e.g., `SubscriptionStats`), then call `SubscriptionStats::increase()` on subscription events and `SubscriptionStats::decrease()` on cancellations. The package records these changes with timestamps for later analysis.
Which Laravel versions does spatie/laravel-stats support?
The package supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub repo](https://github.com/spatie/laravel-stats) for the latest compatibility details, as minor updates may align with new Laravel releases.
Can I query stats for a custom time range (e.g., last 30 days) grouped by day?
Yes. Use the fluent `StatsQuery` API: `SubscriptionStats::query()->start(now()->subDays(30))->end(now())->groupByDay()->get()`. This returns an array with `start`, `end`, `value`, `increments`, `decrements`, and `difference` for each day.
Does this package work with multi-tenant applications (e.g., tenant-specific metrics)?
Yes, but you’ll need to customize the `statistic_name` in your `BaseStats` class (e.g., `tenant_id:subscriptions`) to avoid collisions. The package doesn’t enforce tenant isolation by default, so ensure your queries filter by tenant as needed.
How do I handle high-frequency stat updates (e.g., thousands of events per minute)?
For high-volume writes, defer updates using Laravel queues (e.g., `dispatch(new RecordStats(SubscriptionStats::class, 'increase'))`). The package itself doesn’t batch writes, so consider queueing or database partitioning for scalability.
Can I integrate this with Laravel’s event system or observers?
Absolutely. Trigger `increase()` or `decrease()` in event listeners or observers. For example, in a `SubscriptionCreated` event listener, call `SubscriptionStats::increase()` to automatically track new subscriptions.
What databases does spatie/laravel-stats support?
The package is database-agnostic and officially supports MySQL, PostgreSQL, and SQLite. It uses Laravel’s query builder, so it works with any database supported by Laravel, but performance may vary.
How do I clean up old stats to avoid bloating the database?
The package doesn’t include built-in retention. Schedule a Laravel command (e.g., via `schedule:run`) to delete old stats using `Stats::where('created_at', '<=', now()->subYears(1))->delete()`. Alternatively, use database partitioning or soft deletes.
Are there alternatives to spatie/laravel-stats for analytics in Laravel?
Yes. For lightweight tracking, consider `spatie/laravel-analytics` (for Google Analytics). For time-series data, `laravel-echo` + `pusher-channels` or dedicated tools like InfluxDB with `laravel-influxdb` may fit better. Choose based on whether you need simple increments or complex time-series queries.
How do I test stat tracking in my Laravel application?
Mock the `BaseStats` class in tests and assert calls to `increase()`/`decrease()`. For queries, use `StatsQuery` with fixed dates (e.g., `start(now())->end(now())`) and verify returned arrays match expected values. The package includes unit tests as a reference.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony