diff --git a/config/config.php b/config/config.php index 1a5336f..6ba26c8 100644 --- a/config/config.php +++ b/config/config.php @@ -14,6 +14,16 @@ */ 'enabled' => true, + /* + |-------------------------------------------------------------------------- + | Unique visits + |-------------------------------------------------------------------------- + | + | When enabled, only unique page-visits per session are persisted. + | + */ + 'only_unique' => false, + /* |-------------------------------------------------------------------------- | Site View Model diff --git a/migrations/0000_00_00_000000_create_site_views_table.php b/migrations/0000_00_00_000000_create_site_views_table.php index 29fd158..c89b4c1 100644 --- a/migrations/0000_00_00_000000_create_site_views_table.php +++ b/migrations/0000_00_00_000000_create_site_views_table.php @@ -16,7 +16,7 @@ public function up() Schema::create('site_views', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('user_id')->nullable(); + $table->unsignedBigInteger('user_id')->nullable()->index(); $table->string('request_method', 16); $table->string('url', 2000); @@ -27,8 +27,9 @@ public function up() $table->string('http_referer', 2000)->nullable(); $table->string('http_user_agent')->nullable(); $table->string('http_accept_language', 64)->nullable(); - $table->string('locale', 8)->index(); + $table->string('locale', 8); $table->string('ip')->nullable()->index(); + $table->boolean('unique'); $table->timestamp('requested_at')->nullable(); $table->integer('app_time'); diff --git a/src/SiteView.php b/src/SiteView.php index 66ed8ef..d74c93d 100644 --- a/src/SiteView.php +++ b/src/SiteView.php @@ -13,6 +13,7 @@ class SiteView extends Eloquent protected $casts = [ 'requested_at' => 'datetime', 'created_at' => 'datetime', + 'unique' => 'boolean', ]; protected $guarded = []; diff --git a/src/Tracker.php b/src/Tracker.php index ba60aac..462609c 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -127,6 +127,7 @@ protected function collectVisitData() 'http_accept_language' => $request->server('HTTP_ACCEPT_LANGUAGE'), 'locale' => $this->app->getLocale(), 'ip' => $request->ip(), + 'unique' => $this->isViewUniqueForSession(), 'requested_at' => Carbon::createFromTimestamp($request->server('REQUEST_TIME')), ]; } @@ -138,23 +139,31 @@ protected function collectVisitData() */ public function saveCurrent() { - if ($this->saveEnabled() && $this->isViewValid() && $this->isViewUnique()) { - $success = $this->saveCurrentModel(); + if (! $this->saveEnabled() || ! $this->isViewValid()) { + return false; + } - // Keep on only if the model save has succeeded - if ($success) { - $this->storeCurrentHash(); + $isUnique = $this->isViewUniqueForSession(); + + if ($this->config->get('tracker.only_unique', false) && ! $isUnique) { + return false; + } + + $success = $this->saveCurrentModel(); - $this->saveTrackables( - $this->getCurrent(), - $success - ); + // Keep on only if the model save has succeeded + if ($success) { + if ($isUnique) { + $this->storeCurrentHash(); } - return $success; + $this->saveTrackables( + $this->getCurrent(), + $success + ); } - return false; + return $success; } /** @@ -225,11 +234,11 @@ public function getBotFilter() } /** - * Checks if the current request is unique + * Checks if the current request is unique for the session * * @return bool */ - public function isViewUnique() + public function isViewUniqueForSession() { $hash = $this->getCurrentHash(); @@ -251,7 +260,7 @@ protected function getCurrentHash() $this->currentHash = md5( $this->request->fullUrl(). $this->request->method(). - $this->request->getClientIp() + $this->request->ip() ); } diff --git a/tests/TestBase.php b/tests/TestBase.php index 082ac5a..0e59053 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -43,7 +43,7 @@ protected function resetDatabase() Schema::create('site_views', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('user_id')->nullable(); + $table->unsignedBigInteger('user_id')->nullable()->index(); $table->string('request_method', 16); $table->string('url', 2000); @@ -54,8 +54,9 @@ protected function resetDatabase() $table->string('http_referer', 2000)->nullable(); $table->string('http_user_agent')->nullable(); $table->string('http_accept_language', 64)->nullable(); - $table->string('locale', 8)->index(); + $table->string('locale', 8); $table->string('ip')->nullable()->index(); + $table->boolean('unique'); $table->timestamp('requested_at')->nullable(); $table->integer('app_time'); diff --git a/tests/TrackerTest.php b/tests/TrackerTest.php index 6278904..9f01929 100644 --- a/tests/TrackerTest.php +++ b/tests/TrackerTest.php @@ -2,12 +2,15 @@ namespace Palmans\Tracker\Tests; +use Illuminate\Support\Facades\Config; +use Palmans\Tracker\SiteView; use Palmans\Tracker\Tracker; use Prophecy\PhpUnit\ProphecyTrait; class TrackerTest extends TestBase { use ProphecyTrait; + /** * Setup test */ @@ -76,13 +79,13 @@ public function it_checks_if_the_current_view_is_unique() $this->get('/home'); $this->assertTrue( - $tracker->isViewUnique() + $tracker->isViewUniqueForSession() ); $tracker->saveCurrent(); $this->assertFalse( - $tracker->isViewUnique() + $tracker->isViewUniqueForSession() ); } @@ -115,19 +118,43 @@ public function it_saves_the_current_view() } /** @test */ - public function it_saves_only_if_unique() + public function it_saves_only_once_if_uniqueness_configured() { + $this->app->config->set('tracker.only_unique', true); + $tracker = $this->getTracker(); $this->assertTrue( $tracker->saveCurrent() ); + // $this->assertTrue($tracker->getCurrent()->unique); + $this->assertFalse( $tracker->saveCurrent() ); } + /** @test */ + public function it_saves_if_uniqueness_not_configured() + { + $this->app->config->set('tracker.only_unique', false); + + $tracker = $this->getTracker(); + + $this->assertTrue( + $tracker->saveCurrent() + ); + + // $this->assertTrue($tracker->getCurrent()->unique); + + $this->assertTrue( + $tracker->saveCurrent() + ); + + // $this->assertFalse($tracker->getCurrent()->unique); + } + /** @test */ public function it_adds_and_saves_trackables() {