michaelnabil230/laravel-analytics

This is my package laravel-analytics

dev-main 2023-07-03 07:55 UTC

README

This is my package laravel-analytics

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

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require michaelnabil230/laravel-analytics

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-analytics-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-analytics-config"

This is the contents of the published config file:

return [

    'ip_model' => Ip::class,
    'session_visiter_model' => SessionVisiter::class,
    'visiter_model' => Visiter::class,

    /*
     * Which route paths are not trackable?
     */
    'ignore_paths' => [
        // 'api/*',
    ],

    /*
     * The Do Not Track Ips is used to disable Analytics for some IP addresses:
     *
     *     '127.0.0.1', '192.168.1.1'
     *
     * You can set ranges of IPs
     *     '192.168.0.1-192.168.0.100'
     *
     * And use net masks
     *      127.0.0.0/24 -> range  127.0.0.1 - 127.0.0.255
     *     '172.17.0.0/255.255.0.0'
     */
    'do_not_track_ips' => [
        '127.0.0.2',
    ],

    /*
    |--------------------------------------------------------------------------
    | Drivers GeoIp
    |--------------------------------------------------------------------------
    |
    | Should the geoip data be collected?
    |
    | Set the geoip driver.
    |
    | Supported: "free_geo_ip", "ip_stack"
    */
    'geo_ip' => [
        'enabled' => true,
        'default' => 'free_geo_ip',
        'drivers' => [
            'ip_stack' => [
                'driver' => MichaelNabil230\LaravelAnalytics\GeoIp\IpStack::class,
                'options' => [
                    'api_key' => '',
                    'headers' => [
                        // 'X-First' => 'foo',
                    ],
                ],
            ],
            'free_geo_ip' => [
                'driver' => MichaelNabil230\LaravelAnalytics\GeoIp\FreeGeoIp::class,
                'options' => [
                    'headers' => [
                        // 'X-First' => 'foo',
                    ],
                ],
            ],
        ],
    ],

    /*
     * All bot if you want defected one.
     */
    'bot_browsers' => [
        'curl',
        'python-requests',
        'python-urllib',
        'wget',
        'unk',
        'perl',
        'go-http-client',
    ],

    /*
     * Laravel internal variables on user authentication and login.
     */
    'authentication' => [
        'guards' => [], // defaults to ['web']
    ],
];

Register Middleware

You may register the package middleware in the app/Http/Kernel.php file:

<?php 

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {
    /**
    * The application's route middleware.
    *
    * @var array
    */
    protected $routeMiddleware = [
        /**** OTHER MIDDLEWARE ****/
        'analytics' => \MichaelNabil230\LaravelAnalytics\Middleware\Analytics::class,
    ];
}

You can add the page view middleware to a specific route group, e.g. web.php like so:

Route::middleware('analytics')->group(function () {
    // ...
});

Then you can pass typeRequest in your routes and default is web-request using middleware:

Route::middleware('analytics:api-request')->group(function () {
    // ...
});

Usage

use Illuminate\Support\Carbon;
use MichaelNabil230\LaravelAnalytics\LaravelAnalyticQueries;

$topEvents = LaravelAnalyticQueries::topVisiterEvent()
        // ->where('event', 'test event')
        // ->is('bots')
        // ->is('ajax', false)
        // ->uniqueSession()
        // ->uniqueIp()
        // ->with(['data'])
        // ->take(10)
        // ->select('id') // Is very important: Don't use `select` here pass what u need in `topVisiterEvent`
        // ->addSelect([DB::raw("COUNT(*) as 'count'")])
        ->period(Carbon::now(), Carbon::now()->addDays(7))
        ->limit(10)
        ->get();

Testing

composer test

Support

michaelnabil230 michaelnabil230



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.