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.
BaseStats, allowing customization for domain-specific metrics (e.g., RevenueStats, FeatureUsageStats).groupByWeek, groupByMonth) and range filtering, enabling ad-hoc reporting.BaseStats subclass per metric.increase()/decrease() calls in business logic.StatsQuery extensions for advanced aggregations.DATE_TRUNC for grouping).increase()/decrease() calls could bloat the underlying table (e.g., stats table with 1 row per event).groupBy logic may require raw SQL for non-standard time buckets (e.g., fiscal quarters).StatsQuery or use Laravel’s DB::raw() for edge cases.increase()/decrease() calls idempotent? (Race conditions possible without locks.)stats table conflict with existing schemas? (Consider naming conventions.)increase()/decrease() calls to background jobs.UserSignups, Revenue).increase()/decrease() in high-impact endpoints (e.g., checkout, signup flows).stats migration (php artisan vendor:publish --tag=stats-migrations).statable_type, statable_id, and created_at.groupBy performance with large time ranges (e.g., 1M+ rows).use statements).BaseStats for multi-dimensional stats (e.g., SubscriptionStats::increase($planId)).getTable() to use custom table names (e.g., tenant_stats).| Phase | Task | Dependencies |
|---|---|---|
| Discovery | Identify 3–5 key metrics to track. | Business stakeholders |
| Setup | Install package, publish migration, configure .env. |
Laravel project running |
| Instrument | Add increase()/decrease() calls to business logic. |
API endpoints or event listeners |
| Query | Test StatsQuery for reporting needs. |
Initial data populated |
| Optimize | Add indexes, caching, or batching for scale. | Usage patterns observed |
| Monitor | Set up alerts for table growth or query latency. | Production deployment |
stats table size (e.g., via Laravel Telescope or custom health checks).increase()/decrease() call latency (e.g., with Laravel Debugbar).StatsQuery::toSql() to inspect generated queries.UserCancellations?").stats table by created_at for large datasets (PostgreSQL: CREATE TABLE stats (created_at TIMESTAMP) PARTITION BY RANGE (created_at)).increase() calls (e.g., 100 calls → 1 DB write).StatsQuery::get() results in Redis with TTL).| Risk | Impact | Mitigation |
|---|---|---|
| Database Locks | High concurrency on stats table. |
Use DB::transaction() for batches. |
| Query Timeouts | Large time ranges or groups. | Limit default query ranges (e.g., 1 year max). |
| Data Corruption | Manual INSERT/UPDATE conflicts. |
Use only increase()/decrease(). |
| Retention Bloat | Unbounded table growth. | Implement TTL (e.g., soft-delete old rows). |
| Schema Conflicts | Custom statable_type collisions. |
Use namespaced types (e.g., app.UserSignups). |
StatsQuery for reporting teams.statable_type values.How can I help you explore Laravel packages today?