A minimalistic analytics package for Laravel with seamless view tracking for Eloquent models
Eloquent Viewable is a flexible and minimalistic analytics package for Laravel that allows seamless tracking of views for Eloquent models. Rather than incrementing a single counter, it stores each view as its own database record, so you can analyze totals, unique visitors, and custom time periods entirely within your own application. Whether you're running a blog, an e-commerce store, or a custom Laravel application, this package lets you log and analyze views without relying on external analytics services.
Once installed, you can track and retrieve views effortlessly:
// Record a view
views($post)->record();
// Return total view count
views($post)->count();
// Return unique view count of the past 7 days, compared with the 7 days before
views($post)->unique()->period(Period::pastDays(7))->compare();
// Daily counts for a chart
views($post)->period(Period::pastDays(30))->countByInterval(Granularity::Day);- Track total and unique views for any Eloquent model, from a controller, a route middleware or the browser when the page comes from a full-page cache
- Query views by custom periods, compare with the previous period and group them per hour, day, week, month or year
- Order and filter models by views, rank the most viewed content across every model, and find what's trending right now
- Show what people who viewed this also viewed, and recommend to each viewer what the visitors of the models they viewed recently went on to view, with the reason for every recommendation
- Know who viewed what by linking views to the signed-in user
- Show who is looking right now, "12 people are viewing this", with a live counter on cached pages
- Prevent duplicate views with a configurable cooldown system
- Count unique visitors without a cookie, with a daily rotating fingerprint
- Ignore views from crawlers, blocked IPs, prefetches, and visitors who opt out with Do Not Track, Global Privacy Control or a setting on their account
- Scale with caching, queued recording or a Redis buffer, and test with
Views::fake()
Everything else is on the documentation site: cyrildewit.github.io/eloquent-viewable.
- Quick start, from an empty install to a trending list
- Recording, Counting and Rankings and recommendations
- Viewers and dimensions, Testing, Scaling and Extending
- Recipes, complete features built from the samples
- The configuration, Artisan commands and events
| Package Version | Laravel | PHP |
|---|---|---|
| 9.x | 13.x | 8.5+ |
| 8.x | 13.x | 8.5+ |
| 7.x | 6.x – 13.x | 7.4+ |
On MySQL the package needs version 8.0 or newer, for the window function the rollups of a dimension rank their values with. MariaDB, Postgres and SQLite have one on every version Laravel supports.
The package supports Laravel Octane. Nothing about one request, such as its visitor, viewer, cooldowns or config, carries over to the next request a worker handles.
Install the package via Composer, publish the migrations and run them:
composer require cyrildewit/eloquent-viewable:^9
php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations"
php artisan migrateThis publishes six migrations, one per table: the views table and the tables that retention, rollups, pairs, milestones and spikes use. Those features stay off until you configure them, so their tables stay empty until then. You can delete the migration of a feature you will not use before running it. Publishing again adds only the migrations whose files are missing.
Optionally publish the config file:
php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config"Once it is set up, php artisan views:doctor checks the setup and says what to fix.
See Installation for models keyed by a UUID or ULID, and Preparing your model for the next step.
Please see UPGRADING for detailed upgrade guide.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
- Cyril de Wit - Author - cyrildewit
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details.