starfolksoftware/analytics

This package is abandoned and no longer maintained. No replacement package was suggested.

Add analytics to your Laravel application

v0.9.1 2020-11-04 12:08 UTC

This package is auto-updated.

Last update: 2021-09-04 14:43:00 UTC


README

Latest Version on Packagist Build Status Total Downloads

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.