From a5f4154de3cce46a3228d9dab458cbe8ce5b99a3 Mon Sep 17 00:00:00 2001 From: Eddie Palmans Date: Fri, 5 Aug 2022 11:03:37 +0200 Subject: [PATCH 1/2] rename method --- src/Tracker.php | 6 +++--- tests/TrackerTest.php | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Tracker.php b/src/Tracker.php index ba60aac..29c644e 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -138,7 +138,7 @@ protected function collectVisitData() */ public function saveCurrent() { - if ($this->saveEnabled() && $this->isViewValid() && $this->isViewUnique()) { + if ($this->saveEnabled() && $this->isViewValid() && $this->isViewUniqueForSession()) { $success = $this->saveCurrentModel(); // Keep on only if the model save has succeeded @@ -225,11 +225,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(); diff --git a/tests/TrackerTest.php b/tests/TrackerTest.php index 6278904..0cc1c96 100644 --- a/tests/TrackerTest.php +++ b/tests/TrackerTest.php @@ -8,6 +8,7 @@ class TrackerTest extends TestBase { use ProphecyTrait; + /** * Setup test */ @@ -76,13 +77,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() ); } From c53178b057b3a8930f841b698d7bb8b9f8fa02af Mon Sep 17 00:00:00 2001 From: Eddie Palmans Date: Fri, 5 Aug 2022 12:44:09 +0200 Subject: [PATCH 2/2] store/handle tracking uniqueness --- config/config.php | 10 ++++++ ...0_00_00_000000_create_site_views_table.php | 5 +-- src/SiteView.php | 1 + src/Tracker.php | 33 ++++++++++++------- tests/TestBase.php | 5 +-- tests/TrackerTest.php | 28 +++++++++++++++- 6 files changed, 65 insertions(+), 17 deletions(-) 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 29c644e..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->isViewUniqueForSession()) { - $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; } /** @@ -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 0cc1c96..9f01929 100644 --- a/tests/TrackerTest.php +++ b/tests/TrackerTest.php @@ -2,6 +2,8 @@ namespace Palmans\Tracker\Tests; +use Illuminate\Support\Facades\Config; +use Palmans\Tracker\SiteView; use Palmans\Tracker\Tracker; use Prophecy\PhpUnit\ProphecyTrait; @@ -116,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() {