PHPackages                             palmans/tracker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. palmans/tracker

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

palmans/tracker
===============

Simple site visit/statistics tracker for Laravel

v4.0.0(10mo ago)045MITPHPPHP ^8.2CI passing

Since Aug 1Pushed 10mo agoCompare

[ Source](https://github.com/epalmans/Tracker)[ Packagist](https://packagist.org/packages/palmans/tracker)[ RSS](/packages/palmans-tracker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (9)Used By (0)

Tracker
=======

[](#tracker)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ed8b249e97f40ac880394883f8b4c222c7e97a1443a5de1a2dfa8501e28a653c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70616c6d616e732f747261636b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/palmans/tracker)

Simple site visit/statistics tracker for Laravel.
-------------------------------------------------

[](#simple-site-visitstatistics-tracker-for-laravel)

Tracker provides a simple way to track your site visits and their statistics. This is a fork of [Arrtrust/Tracker](https://github.com/Arrtrust/Tracker) - providing the same functionality, but modernized.

Features
--------

[](#features)

- Compatible with Laravel 11 and 12
- Middleware for automatically recording the site view
- Associate site views to Eloquent models to track their views
- Persists unique views based on URL, method, and IP address
- Helper method, Facade, and trait for easing access to services
- Handy 'Cruncher' for number crunching needs
- Flushing and selecting site views with given time spans
- A [phpunit](https://www.phpunit.de) test suite for easy development

Installation
------------

[](#installation)

Installing Tracker is simple.

1. Pull this package in through [Composer](https://packagist.org/packages/palmans/tracker).

    ```
    composer require palmans/tracker
    ```

    ```
    {
        "require": {
            "palmans/tracker": "^4.0"
        }
    }
    ```
2. You may configure the default behaviour of Tracker by publishing and modifying the configuration file. To do so, use the following command.

    ```
    php artisan vendor:publish --provider=Palmans\Tracker\TrackerServiceProvider
    ```

    Than, you will find the configuration file on the `config/tracker.php` path. Information about the options can be found in the comments of this file. All of the options in the config file are optional, and falls back to default if not specified; remove an option if you would like to use the default.

    This will also publish the migration file for the default `SiteView` model. Do not forget to migrate your database before using Tracker.
3. You may now access Tracker either by the Facade or the helper function.

    ```
    tracker()->getCurrent();
    Tracker::saveCurrent();

    tracker()->isViewUnique();
    tracker()->isViewValid();

    tracker()->addTrackable($post);

    Tracker::flushAll();
    Tracker::flushOlderThan(Carbon::now());
    Tracker::flushOlderThenOrBetween(Carbon::now(), Carbon::now()->subYear());
    ```
4. It is important to record views by using the supplied middleware to record correct app runtime and memory information. To do so register the middleware in `app\Http\Kernel`.

    ```
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'guard' => \App\Http\Middleware\Guard::class,
        'track' => \Palmans\Tracker\TrackerMiddleware::class,
    ];
    ```

    It is better to register this middleware as a routeMiddleware instead of a global middleware and use it in routes or route groups definitions as it may not be necessary to persist all site view. This will persist and attach any Trackable that is added to stack to site views automatically when the request has been handled by Laravel.
5. To attach views to any model or class, you should implement the `Palmans\Tracker\TrackableInterface` interface. Tracker provides `Palmans\Tracker\Trackable` trait to be used by Eloquent models.

    ```

        use Illuminate\Database\Eloquent\Model as Eloquent;
        use Palmans\Tracker\Trackable;
        use Palmans\Tracker\TrackableInterface;

        class Node extends Eloquent implements TrackableInterface
        {
            use Trackable;

            // ...
        }
    ```

    The `Trackable` trait uses Eloquent's `belongsToMany` relationship which utilizes pivot tables. Here is a sample migration for the pivot table:

    ```
