This commit is contained in:
Stefanescu Mihai
2026-03-19 18:54:50 +02:00
parent 82ebb697f3
commit 49b1d3581a
19 changed files with 1496 additions and 11 deletions
+285
View File
@@ -0,0 +1,285 @@
<?php
namespace Tests\Unit;
use App\Models\AnalyticsSession;
use App\Models\DailyAggregate;
use App\Models\PageView;
use App\Models\Visitor;
use App\Models\Website;
use App\Services\AnalyticsQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class AnalyticsQueryServiceTest extends TestCase
{
use RefreshDatabase;
private Website $website;
protected function setUp(): void
{
parent::setUp();
$this->website = Website::factory()->create();
}
private function makeService(?Carbon $from = null, ?Carbon $to = null): AnalyticsQueryService
{
return AnalyticsQueryService::for(
$this->website,
$from ?? Carbon::today()->subDays(6),
$to ?? Carbon::today(),
);
}
private function createVisitorWithPageViews(int $pvCount, ?Carbon $viewedAt = null): void
{
$visitor = Visitor::factory()->for($this->website)->create();
$session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'started_at' => $viewedAt ?? now(),
'ended_at' => $viewedAt ?? now(),
'duration' => 60,
'pages_count' => $pvCount,
'is_bounce' => $pvCount <= 1,
]);
for ($i = 0; $i < $pvCount; $i++) {
PageView::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'viewed_at' => $viewedAt ?? now(),
'path' => '/page-'.$i,
'time_on_page' => 30,
]);
}
}
// -- Summary metrics from raw data --
public function test_total_page_views_counts_correctly(): void
{
$this->createVisitorWithPageViews(3);
$this->createVisitorWithPageViews(2);
$service = $this->makeService();
$this->assertEquals(5, $service->totalPageViews());
}
public function test_unique_visitors_counts_distinct(): void
{
$this->createVisitorWithPageViews(2);
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$this->assertEquals(2, $service->uniqueVisitors());
}
public function test_total_sessions(): void
{
$this->createVisitorWithPageViews(2);
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$this->assertEquals(2, $service->totalSessions());
}
public function test_bounce_rate_calculation(): void
{
// Non-bounce session
$this->createVisitorWithPageViews(3);
// Bounce session
$visitor = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->bounce()->create([
'visitor_id' => $visitor->id,
'started_at' => now(),
]);
$service = $this->makeService();
$this->assertEquals(50.0, $service->bounceRate());
}
public function test_bounce_rate_zero_when_no_sessions(): void
{
$service = $this->makeService();
$this->assertEquals(0, $service->bounceRate());
}
public function test_avg_session_duration(): void
{
$visitor1 = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor1->id,
'duration' => 100,
'started_at' => now(),
]);
$visitor2 = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor2->id,
'duration' => 200,
'started_at' => now(),
]);
$service = $this->makeService();
$this->assertEquals(150, $service->avgSessionDuration());
}
public function test_summary_returns_all_keys(): void
{
$service = $this->makeService();
$summary = $service->summary();
$this->assertArrayHasKey('page_views', $summary);
$this->assertArrayHasKey('visitors', $summary);
$this->assertArrayHasKey('sessions', $summary);
$this->assertArrayHasKey('bounce_rate', $summary);
$this->assertArrayHasKey('avg_duration', $summary);
$this->assertArrayHasKey('avg_pages', $summary);
}
// -- Date range filtering --
public function test_only_counts_data_within_date_range(): void
{
$this->createVisitorWithPageViews(2, Carbon::today());
$this->createVisitorWithPageViews(1, Carbon::today()->subDays(30));
$service = $this->makeService(Carbon::today(), Carbon::today());
$this->assertEquals(2, $service->totalPageViews());
$this->assertEquals(1, $service->uniqueVisitors());
}
// -- Aggregate data --
public function test_reads_from_aggregate_tables_for_past_days(): void
{
DailyAggregate::create([
'website_id' => $this->website->id,
'date' => Carbon::yesterday()->toDateString(),
'visitors' => 50,
'sessions' => 80,
'page_views' => 200,
'bounce_count' => 20,
'total_duration' => 4000,
]);
$service = $this->makeService(
Carbon::yesterday(),
Carbon::yesterday(),
);
$this->assertEquals(200, $service->totalPageViews());
$this->assertEquals(50, $service->uniqueVisitors());
$this->assertEquals(80, $service->totalSessions());
}
// -- Top pages --
public function test_top_pages_returns_ordered_results(): void
{
$visitor = Visitor::factory()->for($this->website)->create();
$session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'started_at' => now(),
]);
PageView::factory()->for($this->website)->count(3)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'path' => '/popular',
'viewed_at' => now(),
]);
PageView::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'analytics_session_id' => $session->id,
'path' => '/less-popular',
'viewed_at' => now(),
]);
$service = $this->makeService();
$pages = $service->topPages(10);
$this->assertGreaterThanOrEqual(2, $pages->count());
$this->assertEquals('/popular', $pages->first()->path);
}
// -- Visitors over time --
public function test_visitors_over_time_returns_collection(): void
{
$this->createVisitorWithPageViews(1);
$service = $this->makeService();
$timeseries = $service->visitorsOverTime();
$this->assertNotEmpty($timeseries);
$first = $timeseries->first();
$this->assertArrayHasKey('period', $first);
$this->assertArrayHasKey('visitors', $first);
$this->assertArrayHasKey('page_views', $first);
$this->assertArrayHasKey('sessions', $first);
}
// -- Traffic sources --
public function test_traffic_sources_groups_by_referrer(): void
{
$visitor = Visitor::factory()->for($this->website)->create();
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'referrer_domain' => 'google.com',
'started_at' => now(),
]);
AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor->id,
'referrer_domain' => '',
'started_at' => now(),
]);
$service = $this->makeService();
$sources = $service->trafficSources();
$this->assertGreaterThanOrEqual(1, $sources->count());
}
// -- Segment filtering --
public function test_segment_filters_data_by_visitor_ids(): void
{
$visitor1 = Visitor::factory()->for($this->website)->create(['country' => 'DE']);
$visitor2 = Visitor::factory()->for($this->website)->create(['country' => 'US']);
$session1 = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor1->id,
'started_at' => now(),
]);
$session2 = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $visitor2->id,
'started_at' => now(),
]);
PageView::factory()->for($this->website)->count(3)->create([
'visitor_id' => $visitor1->id,
'analytics_session_id' => $session1->id,
'viewed_at' => now(),
]);
PageView::factory()->for($this->website)->count(2)->create([
'visitor_id' => $visitor2->id,
'analytics_session_id' => $session2->id,
'viewed_at' => now(),
]);
$service = $this->makeService();
$service->withSegment(collect([$visitor1->id]));
$this->assertEquals(3, $service->totalPageViews());
$this->assertEquals(1, $service->uniqueVisitors());
$this->assertEquals(1, $service->totalSessions());
}
}
+284
View File
@@ -0,0 +1,284 @@
<?php
namespace Tests\Unit;
use App\Models\AnalyticsSession;
use App\Models\Goal;
use App\Models\GoalConversion;
use App\Models\Visitor;
use App\Models\Website;
use App\Services\GoalEvaluationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\TestCase;
class GoalEvaluationServiceTest extends TestCase
{
use RefreshDatabase;
private GoalEvaluationService $service;
private Website $website;
private Visitor $visitor;
private AnalyticsSession $session;
protected function setUp(): void
{
parent::setUp();
$this->service = new GoalEvaluationService;
$this->website = Website::factory()->create();
$this->visitor = Visitor::factory()->for($this->website)->create();
$this->session = AnalyticsSession::factory()->for($this->website)->create([
'visitor_id' => $this->visitor->id,
'duration' => 120,
'pages_count' => 5,
]);
}
// -- URL Destination --
public function test_url_destination_exact_match(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', [
'goal_id' => $goal->id,
'visitor_id' => $this->visitor->id,
]);
}
public function test_url_destination_exact_mismatch(): void
{
Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/other-page',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
public function test_url_destination_starts_with(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/checkout', 'starts_with')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/checkout/step-2',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_url_destination_contains(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('success', 'contains')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/order/success/123',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_url_destination_regex(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/^\\/order\\/\\d+$/', 'regex')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/order/42',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
// -- Event Match --
public function test_event_match_by_name(): void
{
$goal = Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'purchase',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_event_match_wrong_name_does_not_convert(): void
{
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'signup',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
public function test_event_match_with_category(): void
{
$goal = Goal::factory()->for($this->website)->eventMatch('click', 'cta')->create();
// wrong category
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'click',
'event_category' => 'navigation',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
// right category
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'event',
'event_name' => 'click',
'event_category' => 'cta',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 1);
}
public function test_event_match_requires_event_type(): void
{
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'event_name' => 'purchase',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Duration --
public function test_duration_goal_converts_when_threshold_met(): void
{
$goal = Goal::factory()->for($this->website)->duration(60)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_duration_goal_does_not_convert_below_threshold(): void
{
Goal::factory()->for($this->website)->duration(300)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Pages per session --
public function test_pages_per_session_goal_converts(): void
{
$goal = Goal::factory()->for($this->website)->pagesPerSession(3)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
}
public function test_pages_per_session_goal_does_not_convert_below_threshold(): void
{
Goal::factory()->for($this->website)->pagesPerSession(10)->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- De-duplication --
public function test_does_not_duplicate_conversion_within_same_session(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 1);
}
// -- Inactive goals --
public function test_inactive_goals_are_skipped(): void
{
Goal::factory()->for($this->website)->urlDestination('/thank-you')->inactive()->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 0);
}
// -- Conversion value --
public function test_conversion_stores_goal_default_value(): void
{
$goal = Goal::factory()->for($this->website)->urlDestination('/buy')->create([
'default_value' => 49.99,
]);
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/buy',
], Carbon::now());
$conversion = GoalConversion::where('goal_id', $goal->id)->first();
$this->assertNotNull($conversion);
$this->assertEquals(49.99, (float) $conversion->value);
}
// -- Multiple goals --
public function test_evaluates_multiple_goals_independently(): void
{
$goal1 = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
$goal2 = Goal::factory()->for($this->website)->duration(60)->create();
Goal::factory()->for($this->website)->urlDestination('/other-page')->create();
$this->service->evaluate($this->website, $this->visitor, $this->session, [
'type' => 'pageview',
'path' => '/thank-you',
], Carbon::now());
$this->assertDatabaseCount('goal_conversions', 2);
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal1->id]);
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal2->id]);
}
}