Record impressions and clicks of any model with Lara Click Insights
- Install the package via composer:
composer require prajwal89/lara-click-insights
- Publish frontend assets:
php artisan vendor:publish --tag=lara-click-insights-assets
- Publish Database migrations:
php artisan vendor:publish --tag=lara-click-insights-migrations
- Publish Database config (optional):
php artisan vendor:publish --tag=lara-click-insights-config
Alternatively, you can publish all of the above with the following command:
php artisan vendor:publish
When prompted, select Prajwal89\LaraClickInsights\LaraClickInsightsProvider.
In your HTML <head>
tag, add the following directive and meta tag:
<html>
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
...
@LaraClickInsightsJs
</head>
Do not forget to run php artisan migrate
The Eloquent models you want to track impressions for should use the Prajwal89\LaraClickInsights\Traits\ImpressionTrackable
trait.
namespace App;
use Prajwal89\LaraClickInsights\Traits\ImpressionTrackable;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use ImpressionTrackable;
...
}
The trait contains an abstract method trackingAttribute()
that you must implement yourself on your frontend. this should look something like this
<!-- cards that you want to track -->
@foreach($yourEloquentModels as $yourEloquentModel)
<a href="/xyz" {!! $yourEloquentModel->trackingAttribute() !!}>
<!-- card content -->
</a>
@foreach
this will add data attribute for identifying the clickable link like data-clickable="yourEloquentModels:23:default"
do not forget to use {!! !!}
The MIT License (MIT). Please see License File for more information.