phase 4
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\Event;
|
||||
use App\Models\PageView;
|
||||
use App\Models\Visitor;
|
||||
use App\Models\Website;
|
||||
use App\Services\GoalEvaluationService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -120,6 +121,10 @@ class CollectController extends Controller
|
||||
'is_bounce' => $session->pageViews()->count() <= 1,
|
||||
]);
|
||||
|
||||
$session->refresh();
|
||||
|
||||
app(GoalEvaluationService::class)->evaluate($website, $visitor, $session, $payload, $now);
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,12 @@ class DashboardController extends Controller
|
||||
$metrics = $service->summary();
|
||||
$chartData = $service->visitorsOverTime();
|
||||
$activeVisitors = AnalyticsQueryService::activeVisitors($website);
|
||||
$goalConversions = $service->conversionsReport();
|
||||
$goalChartData = $service->goalCompletionsOverTime();
|
||||
|
||||
return view('dashboard.show', compact(
|
||||
'website', 'metrics', 'chartData', 'activeVisitors', 'from', 'to',
|
||||
'website', 'metrics', 'chartData', 'activeVisitors',
|
||||
'goalConversions', 'goalChartData', 'from', 'to',
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreGoalRequest;
|
||||
use App\Http\Requests\UpdateGoalRequest;
|
||||
use App\Models\Goal;
|
||||
use App\Models\Website;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GoalController extends Controller
|
||||
{
|
||||
public function index(Website $website): View
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
|
||||
$goals = $website->goals()->latest()->get();
|
||||
|
||||
return view('goals.index', compact('website', 'goals'));
|
||||
}
|
||||
|
||||
public function create(Website $website): View
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
|
||||
return view('goals.create', compact('website'));
|
||||
}
|
||||
|
||||
public function store(StoreGoalRequest $request, Website $website): RedirectResponse
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
|
||||
$data = $request->validated();
|
||||
$data['is_active'] = $request->boolean('is_active', true);
|
||||
|
||||
$website->goals()->create($data);
|
||||
|
||||
return redirect()
|
||||
->route('goals.index', $website)
|
||||
->with('success', 'Goal created successfully.');
|
||||
}
|
||||
|
||||
public function edit(Website $website, Goal $goal): View
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
abort_unless($goal->website_id === $website->id, 404);
|
||||
|
||||
return view('goals.edit', compact('website', 'goal'));
|
||||
}
|
||||
|
||||
public function update(UpdateGoalRequest $request, Website $website, Goal $goal): RedirectResponse
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
abort_unless($goal->website_id === $website->id, 404);
|
||||
|
||||
$data = $request->validated();
|
||||
$data['is_active'] = $request->boolean('is_active', true);
|
||||
|
||||
$goal->update($data);
|
||||
|
||||
return redirect()
|
||||
->route('goals.index', $website)
|
||||
->with('success', 'Goal updated successfully.');
|
||||
}
|
||||
|
||||
public function destroy(Website $website, Goal $goal): RedirectResponse
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
abort_unless($goal->website_id === $website->id, 404);
|
||||
|
||||
$goal->delete();
|
||||
|
||||
return redirect()
|
||||
->route('goals.index', $website)
|
||||
->with('success', 'Goal deleted successfully.');
|
||||
}
|
||||
}
|
||||
@@ -193,4 +193,22 @@ class ReportController extends Controller
|
||||
'trending' => $service->eventTrending($eventName),
|
||||
]);
|
||||
}
|
||||
|
||||
// ---- Conversions ----
|
||||
|
||||
public function conversions(Request $request, Website $website): View
|
||||
{
|
||||
$this->authorize('view', $website);
|
||||
[$from, $to] = $this->parseDateRange($request);
|
||||
$service = AnalyticsQueryService::for($website, $from, $to);
|
||||
|
||||
return view('reports.conversions', [
|
||||
'website' => $website,
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'goals' => $service->conversionsReport(),
|
||||
'chartData' => $service->goalCompletionsOverTime(),
|
||||
'totalSessions' => $service->totalSessions(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreGoalRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'type' => ['required', Rule::in(['url_destination', 'event_match', 'duration', 'pages_per_session'])],
|
||||
'default_value' => ['nullable', 'numeric', 'min:0'],
|
||||
'is_active' => ['sometimes', 'boolean'],
|
||||
|
||||
'conditions.path' => ['required_if:type,url_destination', 'nullable', 'string', 'max:2048'],
|
||||
'conditions.match_type' => ['required_if:type,url_destination', 'nullable', Rule::in(['exact', 'starts_with', 'contains', 'regex'])],
|
||||
|
||||
'conditions.event_name' => ['required_if:type,event_match', 'nullable', 'string', 'max:255'],
|
||||
'conditions.event_category' => ['nullable', 'string', 'max:255'],
|
||||
|
||||
'conditions.seconds' => ['required_if:type,duration', 'nullable', 'integer', 'min:1'],
|
||||
|
||||
'conditions.page_count' => ['required_if:type,pages_per_session', 'nullable', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateGoalRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'type' => ['required', Rule::in(['url_destination', 'event_match', 'duration', 'pages_per_session'])],
|
||||
'default_value' => ['nullable', 'numeric', 'min:0'],
|
||||
'is_active' => ['sometimes', 'boolean'],
|
||||
|
||||
'conditions.path' => ['required_if:type,url_destination', 'nullable', 'string', 'max:2048'],
|
||||
'conditions.match_type' => ['required_if:type,url_destination', 'nullable', Rule::in(['exact', 'starts_with', 'contains', 'regex'])],
|
||||
|
||||
'conditions.event_name' => ['required_if:type,event_match', 'nullable', 'string', 'max:255'],
|
||||
'conditions.event_category' => ['nullable', 'string', 'max:255'],
|
||||
|
||||
'conditions.seconds' => ['required_if:type,duration', 'nullable', 'integer', 'min:1'],
|
||||
|
||||
'conditions.page_count' => ['required_if:type,pages_per_session', 'nullable', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user