-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4803860
Showing
23 changed files
with
2,048 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var gulp = require('gulp'); | ||
var phpunit = require('gulp-phpunit'); | ||
var run = require('gulp-run'); | ||
var notify = require('gulp-notify'); | ||
|
||
gulp.task('test', function() { | ||
gulp.src('phpunit.xml') | ||
.pipe(run('clear')) | ||
.pipe(phpunit('', { notify: true })) | ||
.on('error', notify.onError({ | ||
title: 'Red!', | ||
message: 'Your tests failed...' | ||
})) | ||
.pipe(notify( | ||
{ | ||
title: 'Green!', | ||
message: 'Your tests succeeded!' | ||
} | ||
)); | ||
}); | ||
|
||
gulp.task('watch', function() { | ||
gulp.watch(['tests/**/*.php'], ['test']); | ||
}); | ||
|
||
gulp.task('default', ['test', 'watch']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 kenarkose | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# Tracker | ||
Simple site visit/statistics tracker for Laravel 5. | ||
|
||
--- | ||
|
||
Tracker provides a simple way to track your site visits and their statistics. | ||
|
||
## 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 | ||
Installing Tracker is simple. | ||
|
||
1. Pull this package in through [Composer](https://getcomposer.org). | ||
|
||
```js | ||
{ | ||
"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. | ||
```php | ||
'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. | ||
```bash | ||
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. | ||
```php | ||
'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. | ||
```php | ||
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`. | ||
```php | ||
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. | ||
```php | ||
|
||
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: | ||
```php | ||
<?php | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
class CreateNodeSiteViewPivotTable extends Migration { | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('node_site_view', function (Blueprint $table) | ||
{ | ||
$table->integer('node_id')->unsigned(); | ||
$table->integer('site_view_id')->unsigned(); | ||
$table->foreign('node_id') | ||
->references('id') | ||
->on('nodes') | ||
->onDelete('cascade'); | ||
$table->foreign('site_view_id') | ||
->references('id') | ||
->on('site_views') | ||
->onDelete('cascade'); | ||
$table->primary(['node_id', 'site_view_id']); | ||
}); | ||
} | ||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('node_site_view'); | ||
} | ||
} | ||
``` | ||
8. Check the `Arrtrust\Tracker\Cruncher` class and test for statistics number crunching. It is equipped with a number of methods for different types of statistics (mostly counts) in different time spans. | ||
Please check the tests and source code for further documentation, as the source code of Tracker is well tested and documented. | ||
## License | ||
Tracker is released under [MIT License](https://github.com/arrtrust/Tracker/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "arrtrust/tracker", | ||
"description": "Simple site visit/statistics tracker for Laravel 5", | ||
"license": "MIT", | ||
"keywords": [ | ||
"tracker", | ||
"analytics", | ||
"visitor", | ||
"statistics", | ||
"laravel", | ||
"laravel5" | ||
], | ||
"version": "1.6.3", | ||
"require": { | ||
"php": ">=5.4.0" | ||
}, | ||
"require-dev": { | ||
"orchestra/testbench": "~3.0", | ||
"phpunit/phpunit": "~4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Arrtrust\\Tracker\\": "src/" | ||
}, | ||
"files": [ | ||
"src/helpers.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"classmap": [ | ||
"tests" | ||
] | ||
}, | ||
"suggest": { | ||
"laravel/framework": "This package requires Laravel 5." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"devDependencies": { | ||
"gulp": "^3.8.11", | ||
"gulp-notify": "^2.2.0", | ||
"gulp-phpunit": "^0.8.1", | ||
"gulp-run": "^1.6.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
<exclude>./tests/TestBase.php</exclude> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
Oops, something went wrong.