Add the ability to associate analytics to your Laravel Eloquent models.
$post = Post::find(1);
event(new Viewed($post))
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',
],
];
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;
...
}
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.
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;
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email frknasir@yahoo.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.