- 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.