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