Skip to content

lancodev/laravel-analytics

Repository files navigation

Laravel Analytics

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a simple way to capture site analytics within your existing Laravel application.

Installation

Install the package via composer:

composer require lancodev/laravel-analytics

Publish the components and views:

php artisan vendor:publish --tag="laravel-analytics-components"
php artisan vendor:publish --tag="analytics-views"

Run the package's install command:

php artisan analytics:install

Usage

Trackable Trait

The package provides an optional Trackable trait that can be used to assign page view analytics to a model. The trait provides a pageViews relationship that can be used to retrieve the page views for the model.

This would typically be implemented by the User model. However, it could also be implemented by any other model that you wish to track page views for.

To use the trait, simply add the following to your model:

namespace App\Models;

use Lancodev\LaravelAnalytics\Traits\Trackable;

class User
{
    use Trackable;
}

Tracking

To begin tracking analytics, add the <x-analytics-analytics /> component to your layout file(s). This will add the necessary JavaScript to your application.

<html>
    <head>
        <title>My Application</title>
    </head>
    <body>
        ``` content ```

        <x-analytics-analytics />
    </body>
</html>

The <x-analytics-analytics /> component accepts the following props:

  • trackable: the morphable entity to track
    • Example: <x-analytics-analytics :trackable="auth()->user()" />
    • This will associate all page view analytics with the current user

Analytics Dashboard

The package also provides a simple dashboard component that can be used to display analytics for your application.

The dashboard can be added to any view by adding the following:

<x-analytics-dashboard />

This will add the following to your page:

analyticsdashboardscreenshot.png

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.