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 stat changes in your app over time. Create a stats class, call increase/decrease on events (e.g., subscriptions), then query totals, increments, decrements, and differences grouped by day/week/month for any date range.

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 signups or cancellations with spatie/laravel-stats?
Extend `BaseStats` to create a class like `SignupStats`. Call `SignupStats::increase()` in your signup logic and `SignupStats::decrease()` for cancellations. The package automatically records changes with timestamps for later querying.
Does this package work with Laravel 8.x or older versions?
No, spatie/laravel-stats requires **PHP 8.0+** and is officially tested with **Laravel 9+**. For older versions, you may need to manually adapt the code or check for legacy branches.
Can I query stats for a custom time range (e.g., last 30 days) or specific dates?
Yes. Use the fluent `StatsQuery` API: `SignupStats::query()->start(now()->subDays(30))->end(now())->get()`. You can also chain methods like `groupByWeek()` or `groupByMonth()` for time-series aggregation.
Will this package slow down my application if I track high-frequency events (e.g., API calls)?
For high-volume tracking, consider **offloading `increase()`/`decrease()` calls to Laravel Queues** to avoid blocking requests. The package itself is lightweight, but frequent writes to the `stats` table may require indexing or batching.
How do I group stats by month or week for reporting?
Use the `groupByWeek()` or `groupByMonth()` methods on your query. Example: `Stats::query()->groupByMonth()->get()` returns an array with monthly aggregates including `value`, `increments`, `decrements`, and `difference`.
Can I track stats per user, tenant, or only globally?
By default, stats are global. To track per-user or per-tenant, extend `BaseStats` and override methods like `getStatableType()` and `getStatableId()` to include user/tenant IDs in the `stats` table.
Does spatie/laravel-stats support custom aggregations like moving averages?
The package provides basic aggregates (`value`, `increments`, `decrements`). For advanced metrics (e.g., moving averages), extend `StatsQuery` or use raw SQL with `DB::raw()` in your queries.
How do I install and publish the database migration?
Run `composer require spatie/laravel-stats` and publish the migration with `php artisan vendor:publish --tag=stats-migrations`. Then run `php artisan migrate` to create the `stats` table.
Are there alternatives to this package for Laravel analytics?
Yes. For simple counters, consider `spatie/laravel-activitylog`. For complex analytics, explore **Laravel Scout**, **Google Analytics API**, or **Mixpanel**. This package is ideal for lightweight, self-hosted time-series tracking.
How do I optimize queries for large datasets (e.g., 1M+ rows)?
Add indexes to `statable_type`, `statable_id`, and `created_at` in the `stats` table. For frequent queries, cache results in **Redis** or use **materialized views**. Avoid deep time ranges without grouping.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport