starfolksoftware / analytics
Add analytics to your Laravel application
Requires
- php: ^7.3.0
- illuminate/support: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^3.6|^5.0
- phpunit/phpunit: ^7.0|^8.0
This package is auto-updated.
Last update: 2021-09-04 14:43:00 UTC
README
Add the ability to associate analytics to your Laravel Eloquent models.
$post = Post::find(1); event(new Viewed($post))
Installation
You can install the package via composer:
composer require starfolksoftware/analytics
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="StarfolkSoftware\Analytics\AnalyticsServiceProvider" --tag="migrations"
After the migration has been published you can create the media-table by running the migrations:
php artisan migrate
You can publish the config-file with:
php artisan vendor:publish --provider="StarfolkSoftware\Analytics\AnalyticsServiceProvider" --tag="config"
To register the Viewed
event, CaptureView
and CaptureVisit
listeners, edit your EventServiceProvider
as in the following:
... /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'StarfolkSoftware\Analytics\Events\Viewed' => [ 'StarfolkSoftware\Analytics\Listeners\CaptureView', 'StarfolkSoftware\Analytics\Listeners\CaptureVisit', ], ];
Usage
Registering Models
To let your models be able to have analytics, add the HasViews
, HasVisits
traits to the model classes.
namespace App\Models; use Illuminate\Database\Eloquent\Model; use StarfolkSoftware\Analytics\Traits\{HasViews, HasVisits}; class Post extends Model { use HasViews, HasVisits; ... }
Usage
To trigger the viewed event on your model, you can call the event()
helper method. It receives the intance of the Viewed
.
$post = Post::find(1); event(new Viewed($post))
This event triggers the CaptureView
and CaptureVisit
listeners.
Retrieving Analytics
The models that use the HasViews
and HasVisits
traits have access to it's analytics using the views
and visits
relations respectively:
$post = Post::find(1); // Retrieve $views = $post->views; $visits = $post->visits;
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email frknasir@yahoo.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.