From f8633da44ff08364f3fe25323763610cb730b591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Scho=CC=88ps?= Date: Sat, 13 Feb 2021 23:28:42 +0100 Subject: [PATCH] Bug fixes --- ...2021_02_13_000001_laravel_track_migrations.php | 9 ++++----- src/Http/Middleware/TrackRequest.php | 2 +- src/Models/Visitor.php | 2 +- src/Tracker.php | 15 +++++++-------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/database/migrations/2021_02_13_000001_laravel_track_migrations.php b/database/migrations/2021_02_13_000001_laravel_track_migrations.php index df64d5b..af45038 100644 --- a/database/migrations/2021_02_13_000001_laravel_track_migrations.php +++ b/database/migrations/2021_02_13_000001_laravel_track_migrations.php @@ -61,10 +61,9 @@ public function up() Schema::create('track_visitor_ab_test_option', function (Blueprint $table) { $table->id(); - $table->foreignId('track_visitor_id')->nullable(); - $table->foreignId('track_page_view_id'); - $table->foreignId('track_ab_test_id')->nullable(); - $table->foreignId('track_ab_test_option_id')->nullable(); + $table->foreignId('track_visitor_id'); + $table->foreignId('track_ab_test_id'); + $table->foreignId('track_ab_test_option_id'); }); Schema::create('track_goals', function (Blueprint $table) { @@ -92,7 +91,7 @@ public function down() Schema::dropIfExists('track_ab_tests'); Schema::dropIfExists('track_ab_test_options'); Schema::dropIfExists('track_page_views'); - Schema::dropIfExists('track_page_view_ab_test_option'); + Schema::dropIfExists('track_visitor_ab_test_option'); Schema::dropIfExists('track_goals'); Schema::dropIfExists('track_page_view_goal'); } diff --git a/src/Http/Middleware/TrackRequest.php b/src/Http/Middleware/TrackRequest.php index bd30d56..7ec9c1e 100644 --- a/src/Http/Middleware/TrackRequest.php +++ b/src/Http/Middleware/TrackRequest.php @@ -8,7 +8,7 @@ class TrackRequest { public function handle($request, $next) { - Tracker::track($request); + Tracker::track(); return $next($request); } diff --git a/src/Models/Visitor.php b/src/Models/Visitor.php index e151941..51d0e01 100644 --- a/src/Models/Visitor.php +++ b/src/Models/Visitor.php @@ -22,7 +22,7 @@ public static function booted() public function abTests() { return $this->belongsToMany(ABTest::class, 'track_visitor_ab_test_option', 'track_visitor_id', 'track_ab_test_id') - ->withPivot('track_ab_test_option_id', 'track_page_view_id'); + ->withPivot('track_ab_test_option_id'); } public function trackPageView() diff --git a/src/Tracker.php b/src/Tracker.php index 2b226d3..e754a6a 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -21,7 +21,7 @@ class Tracker protected $visitor; protected $nextOption; - public function track($request) + public function track() { $agent = new Agent(); if($agent->isRobot()) { @@ -30,12 +30,6 @@ public function track($request) $visitor = $this->getVisitor(); - if(!$visitor) { - $visitor = Visitor::create(); - Cookie::queue(self::COOKIE_NAME, $visitor->key, 5 * 365 * 24 * 60); - $visitor->fillInfo($request); - } - $this->pageView = $visitor->trackPageView(); } @@ -99,7 +93,6 @@ protected function nextOption(ABTest $test) } $visitor->abTests()->attach($test, [ - 'track_page_view_id' => $this->pageView->id, 'track_ab_test_option_id' => $option->id ]); Cache::put(self::CACHE_PREFIX_AB_TESTS . $test->id, $option->id, now()->addMonth()); @@ -119,6 +112,12 @@ protected function getVisitor() : ?Visitor $this->visitor = Visitor::firstWhere('key', $this->getVisitorKey()); } + if(!$this->visitor) { + $this->visitor = Visitor::create(); + Cookie::queue(self::COOKIE_NAME, $this->visitor->key, 5 * 365 * 24 * 60); + $this->visitor->fillInfo(); + } + return $this->visitor; } } \ No newline at end of file