Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schöps committed Feb 13, 2021
1 parent 6d1ccbf commit f8633da
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/TrackRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TrackRequest
{
public function handle($request, $next)
{
Tracker::track($request);
Tracker::track();

return $next($request);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 7 additions & 8 deletions src/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Tracker
protected $visitor;
protected $nextOption;

public function track($request)
public function track()
{
$agent = new Agent();
if($agent->isRobot()) {
Expand All @@ -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();
}

Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
}

0 comments on commit f8633da

Please sign in to comment.