PHPackages                             arrtrust/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. arrtrust/tracker

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

arrtrust/tracker
================

Simple site visit/statistics tracker for Laravel 5

1.6.3(6y ago)0351MITPHPPHP &gt;=5.4.0CI failing

Since Nov 12Pushed 6y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Tracker
=======

[](#tracker)

Simple site visit/statistics tracker for Laravel 5.

---

Tracker provides a simple way to track your site visits and their statistics.

Features
--------

[](#features)

- Compatible with Laravel 5
- 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](http://www.phpunit.de) test suite for easy development

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

[](#installation)

Installing Tracker is simple.

1. Pull this package in through [Composer](https://getcomposer.org).

    ```
    {
        "require": {
            "arrtrust/tracker": "~1.6"
        }
    }
    ```
2. In order to register Tracker Service Provider add `'Arrtrust\Tracker\TrackerServiceProvider'` to the end of `providers` array in your `config/app.php` file.

    ```
    'providers' => array(

        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        ...
        'Arrtrust\Tracker\TrackerServiceProvider',

    ),
    ```
3. 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
    ```

    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.
4. In order to register the Facade add the following line to the end of `aliases` array in your `config/app.php` file.

    ```
    'aliases' => array(

        'App'        => 'Illuminate\Support\Facades\App',
        'Artisan'    => 'Illuminate\Support\Facades\Artisan',
        ...
        'Tracker'   => 'Arrtrust\Tracker\TrackerFacade'

    ),
    ```
5. 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());
    ```
6. 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' => \Arrtrust\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.
7. To attach views to any model or class, you should implement the `Arrtrust\Tracker\TrackableInterface` interface. Tracker provides `Arrtrust\Tracker\Trackable` trait to be used by Eloquent models.

    ```

        use Illuminate\Database\Eloquent\Model as Eloquent;
        use Arrtrust\Tracker\Trackable;
        use Arrtrust\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:

    ```
