- How do I set up spatie/laravel-stats for tracking user signups in Laravel?
- First, create a stats class extending `BaseStats` (e.g., `SignupStats`). Then call `SignupStats::increase()` in your signup controller or event listener. Run `php artisan migrate` to set up the database tables. Query results later using the fluent `StatsQuery` API with time ranges like `groupByDay()` or `groupByWeek()`.
- Does this package work with Laravel 11 or 12? What’s the latest supported version?
- Yes, `spatie/laravel-stats` supports Laravel 10 through 13, including Laravel 11 and 12. The package is actively maintained, with the latest release in April 2026. Check the [GitHub repo](https://github.com/spatie/laravel-stats) for version-specific notes.
- Can I track custom metrics like API call counts or payment failures with this package?
- Absolutely. Create a dedicated stats class (e.g., `ApiCallStats` or `PaymentFailureStats`) and use `increase()`/`decrease()` to log events. The package supports any metric type, including custom attributes or relationships, as long as you extend `BaseStats`.
- How do I query stats for a specific time range, like the last 30 days grouped by day?
- Use the fluent `StatsQuery` API: `YourStats::query()->start(now()->subDays(30))->end(now())->groupByDay()->get()`. This returns an array of objects with `value`, `increments`, `decrements`, and `difference` for each day. You can chain methods like `groupByWeek()` or `groupByMonth()` for other granularities.
- Will this package slow down my Laravel app if I track thousands of metrics?
- The package is lightweight and optimized for performance, but heavy queries (e.g., large time ranges) may impact speed. For production, cache frequent queries with Redis or implement Laravel Scheduler jobs to batch updates. The package also supports bulk inserts to reduce database load.
- How do I clean up old stats data to avoid bloating my database?
- There’s no built-in retention policy, so you’ll need to manually delete old stats. Use Laravel Scheduler to run a cleanup job (e.g., `StatsCleanup::deleteOlderThan(Carbon::now()->subYears(1))`). Alternatively, add a `deleted_at` column and use soft deletes with Eloquent.
- Can I integrate this with Laravel Nova or Filament for a dashboard?
- Yes, but you’ll need to build a custom resource or widget. The package provides structured data (e.g., `value`, `increments`) that you can display in Nova/Filament tables, charts, or cards. Example: Use Nova’s `DetailResource` to show stats over time or Filament’s `LineChart` for visualizations.
- Does spatie/laravel-stats support real-time analytics like live counters?
- No, this package is designed for batch updates (e.g., `increase()`/`decrease()` calls) and aggregated queries, not real-time streaming. For live counters, consider pairing it with Laravel Echo or a dedicated real-time analytics tool like Laravel Analytics or Prometheus.
- How do I test my stats logic in PHPUnit? Can I mock the database?
- Use Laravel’s testing helpers to mock the `BaseStats` class or its database interactions. For example, in a test: `$this->partialMock(YourStats::class, ['increase'])->expects($this->once())->method('increase');`. The package’s stateless design makes it easy to test without a full database setup.
- Are there alternatives to spatie/laravel-stats for Laravel analytics?
- Yes. For lightweight self-hosted analytics, consider `laravel-analytics` or custom PostgreSQL timeseries tables. For real-time metrics, try Prometheus or Grafana. If you need external integrations (e.g., Mixpanel), use their SDKs alongside this package for hybrid tracking.