Allow heading tags (h1-h6) in ContentSanitizer so news editor headings render
This commit is contained in:
@@ -6,11 +6,13 @@ namespace App\Http\Controllers\Settings;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AcademyContentMetricDaily;
|
||||
use App\Models\AcademyEvent;
|
||||
use App\Models\AcademySearchLog;
|
||||
use App\Services\Academy\AcademyAnalyticsContentResolver;
|
||||
use App\Services\Academy\AcademyContentIntelligenceService;
|
||||
use App\Services\Academy\AcademyPopularityService;
|
||||
use App\Support\AcademyAnalytics\AcademyAnalyticsContentType;
|
||||
use App\Support\AcademyAnalytics\AcademyAnalyticsEventType;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -29,6 +31,9 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
public function overview(Request $request): Response
|
||||
{
|
||||
[$from, $to, $range] = $this->resolveDateRange($request);
|
||||
$promptLibraryCurrent = $this->contentSummary(AcademyAnalyticsContentType::PROMPT_LIBRARY, $from, $to);
|
||||
[$previousFrom, $previousTo] = $this->previousRange($from, $to);
|
||||
$promptLibraryPrevious = $this->contentSummary(AcademyAnalyticsContentType::PROMPT_LIBRARY, $previousFrom, $previousTo);
|
||||
|
||||
$summary = $this->metricsQuery($from, $to)
|
||||
->selectRaw('sum(views) as views, sum(unique_visitors) as unique_visitors, sum(user_views) as user_views, sum(guest_views) as guest_views, sum(subscriber_views) as subscriber_views, sum(prompt_copies) as prompt_copies, sum(likes) as likes, sum(saves) as saves, sum(completions) as completions, sum(starts) as starts, sum(upgrade_clicks) as upgrade_clicks')
|
||||
@@ -50,6 +55,21 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
'courseStarts' => (int) ($summary?->starts ?? 0),
|
||||
'upgradeClicks' => (int) ($summary?->upgrade_clicks ?? 0),
|
||||
],
|
||||
'promptLibraryTrend' => [
|
||||
'current' => $promptLibraryCurrent,
|
||||
'previous' => $promptLibraryPrevious,
|
||||
'deltas' => [
|
||||
'views' => $this->percentDelta((int) $promptLibraryCurrent['views'], (int) $promptLibraryPrevious['views']),
|
||||
'uniqueVisitors' => $this->percentDelta((int) $promptLibraryCurrent['uniqueVisitors'], (int) $promptLibraryPrevious['uniqueVisitors']),
|
||||
'engagedViews' => $this->percentDelta((int) $promptLibraryCurrent['engagedViews'], (int) $promptLibraryPrevious['engagedViews']),
|
||||
'engagementRate' => $this->percentDelta((float) $promptLibraryCurrent['engagementRate'], (float) $promptLibraryPrevious['engagementRate']),
|
||||
],
|
||||
'range' => [
|
||||
'current' => ['from' => $from->toDateString(), 'to' => $to->toDateString()],
|
||||
'previous' => ['from' => $previousFrom->toDateString(), 'to' => $previousTo->toDateString()],
|
||||
],
|
||||
],
|
||||
'popularPromptPeriodUsage' => $this->popularPromptPeriodUsage($from, $to),
|
||||
'topContent' => $this->serializeContentRows($this->popularity->topContent($from, $to, 8)),
|
||||
'topWeek' => $this->serializeContentRows($this->popularity->topContent(now()->subDays(6)->startOfDay(), now()->endOfDay(), 8)),
|
||||
]);
|
||||
@@ -65,6 +85,11 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
return $this->renderContentPage($request, AcademyAnalyticsContentType::PROMPT, 'Prompt analytics', 'Copy-heavy prompt performance, save rates, and upgrade interest.');
|
||||
}
|
||||
|
||||
public function promptLibrary(Request $request): Response
|
||||
{
|
||||
return $this->renderContentPage($request, AcademyAnalyticsContentType::PROMPT_LIBRARY, 'Prompt library analytics', 'Discovery and engagement on the public /academy/prompts library page.');
|
||||
}
|
||||
|
||||
public function lessons(Request $request): Response
|
||||
{
|
||||
return $this->renderContentPage($request, AcademyAnalyticsContentType::LESSON, 'Lesson analytics', 'Lesson engagement, starts, completions, and drop-off signals.');
|
||||
@@ -333,9 +358,14 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
'access' => $access,
|
||||
'content_type' => $contentType,
|
||||
],
|
||||
'summary' => $contentType === AcademyAnalyticsContentType::PROMPT_LIBRARY
|
||||
? $this->contentSummary(AcademyAnalyticsContentType::PROMPT_LIBRARY, $from, $to)
|
||||
: null,
|
||||
'rows' => $serializedRows,
|
||||
'contentTypeOptions' => [
|
||||
['value' => '', 'label' => 'All content'],
|
||||
['value' => AcademyAnalyticsContentType::PROMPT_LIBRARY, 'label' => 'Prompt library'],
|
||||
['value' => AcademyAnalyticsContentType::PROMPT_PACK_LIBRARY, 'label' => 'Prompt pack library'],
|
||||
['value' => AcademyAnalyticsContentType::PROMPT, 'label' => 'Prompts'],
|
||||
['value' => AcademyAnalyticsContentType::LESSON, 'label' => 'Lessons'],
|
||||
['value' => AcademyAnalyticsContentType::COURSE, 'label' => 'Courses'],
|
||||
@@ -359,7 +389,134 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
private function metricsQuery(Carbon $from, Carbon $to)
|
||||
{
|
||||
return AcademyContentMetricDaily::query()
|
||||
->whereBetween('date', [$from->toDateString(), $to->toDateString()]);
|
||||
->whereBetween('date', [$from->copy()->startOfDay(), $to->copy()->endOfDay()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int|float>
|
||||
*/
|
||||
private function contentSummary(string $contentType, Carbon $from, Carbon $to): array
|
||||
{
|
||||
$query = $this->metricsQuery($from, $to)
|
||||
->where('content_type', $contentType);
|
||||
|
||||
if (! AcademyAnalyticsContentType::requiresContentId($contentType)) {
|
||||
$query->whereNull('content_id');
|
||||
}
|
||||
|
||||
$summary = $query
|
||||
->selectRaw('sum(views) as views, sum(unique_visitors) as unique_visitors, sum(engaged_views) as engaged_views, sum(scroll_50) as scroll_50, sum(scroll_75) as scroll_75, sum(scroll_100) as scroll_100, avg(avg_engaged_seconds) as avg_engaged_seconds, sum(popularity_score) as popularity_score')
|
||||
->first();
|
||||
|
||||
$uniqueVisitors = max(0, (int) ($summary?->unique_visitors ?? 0));
|
||||
$engagedViews = max(0, (int) ($summary?->engaged_views ?? 0));
|
||||
$scroll100 = max(0, (int) ($summary?->scroll_100 ?? 0));
|
||||
|
||||
return [
|
||||
'views' => max(0, (int) ($summary?->views ?? 0)),
|
||||
'uniqueVisitors' => $uniqueVisitors,
|
||||
'engagedViews' => $engagedViews,
|
||||
'scroll50' => max(0, (int) ($summary?->scroll_50 ?? 0)),
|
||||
'scroll75' => max(0, (int) ($summary?->scroll_75 ?? 0)),
|
||||
'scroll100' => $scroll100,
|
||||
'avgEngagedSeconds' => round((float) ($summary?->avg_engaged_seconds ?? 0), 1),
|
||||
'popularityScore' => round((float) ($summary?->popularity_score ?? 0), 2),
|
||||
'engagementRate' => $uniqueVisitors > 0 ? round(($engagedViews / $uniqueVisitors) * 100, 1) : 0.0,
|
||||
'deepScrollRate' => $uniqueVisitors > 0 ? round(($scroll100 / $uniqueVisitors) * 100, 1) : 0.0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: Carbon, 1: Carbon}
|
||||
*/
|
||||
private function previousRange(Carbon $from, Carbon $to): array
|
||||
{
|
||||
$days = $from->copy()->startOfDay()->diffInDays($to->copy()->startOfDay()) + 1;
|
||||
|
||||
return [
|
||||
$from->copy()->subDays($days)->startOfDay(),
|
||||
$from->copy()->subDay()->endOfDay(),
|
||||
];
|
||||
}
|
||||
|
||||
private function percentDelta(int|float $current, int|float $previous): ?float
|
||||
{
|
||||
if ((float) $previous === 0.0) {
|
||||
return (float) $current === 0.0 ? 0.0 : null;
|
||||
}
|
||||
|
||||
return round((((float) $current - (float) $previous) / (float) $previous) * 100, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{totalViews:int,totalVisitors:int,periods:list<array<string,int|float|string>>}
|
||||
*/
|
||||
private function popularPromptPeriodUsage(Carbon $from, Carbon $to): array
|
||||
{
|
||||
$events = AcademyEvent::query()
|
||||
->whereBetween('occurred_at', [$from, $to])
|
||||
->where('event_type', AcademyAnalyticsEventType::PAGE_VIEW)
|
||||
->where('content_type', AcademyAnalyticsContentType::PROMPT_POPULAR)
|
||||
->get(['visitor_id', 'metadata']);
|
||||
|
||||
$summary = [];
|
||||
$totalViews = 0;
|
||||
$visitorBuckets = [];
|
||||
|
||||
foreach ($events as $event) {
|
||||
$metadata = is_array($event->metadata) ? $event->metadata : [];
|
||||
$period = trim((string) ($metadata['period'] ?? ''));
|
||||
|
||||
if ($period === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$days = max(0, (int) ($metadata['period_days'] ?? 0));
|
||||
|
||||
if (! isset($summary[$period])) {
|
||||
$summary[$period] = [
|
||||
'period' => $period,
|
||||
'label' => sprintf('%s days', $days > 0 ? $days : (int) preg_replace('/\D+/', '', $period)),
|
||||
'views' => 0,
|
||||
'uniqueVisitors' => 0,
|
||||
'share' => 0.0,
|
||||
'days' => $days,
|
||||
];
|
||||
$visitorBuckets[$period] = [];
|
||||
}
|
||||
|
||||
$summary[$period]['views']++;
|
||||
$totalViews++;
|
||||
|
||||
$visitorId = trim((string) ($event->visitor_id ?? ''));
|
||||
if ($visitorId !== '') {
|
||||
$visitorBuckets[$period][$visitorId] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$totalVisitors = 0;
|
||||
|
||||
foreach ($summary as $period => &$row) {
|
||||
$uniqueVisitors = count($visitorBuckets[$period] ?? []);
|
||||
$row['uniqueVisitors'] = $uniqueVisitors;
|
||||
$row['share'] = $totalViews > 0 ? round((((int) $row['views']) / $totalViews) * 100, 1) : 0.0;
|
||||
$totalVisitors += $uniqueVisitors;
|
||||
}
|
||||
unset($row);
|
||||
|
||||
usort($summary, static function (array $left, array $right): int {
|
||||
if ((int) $right['views'] === (int) $left['views']) {
|
||||
return ((int) $left['days']) <=> ((int) $right['days']);
|
||||
}
|
||||
|
||||
return ((int) $right['views']) <=> ((int) $left['views']);
|
||||
});
|
||||
|
||||
return [
|
||||
'totalViews' => $totalViews,
|
||||
'totalVisitors' => $totalVisitors,
|
||||
'periods' => array_values($summary),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -440,6 +597,7 @@ final class AcademyAdminAnalyticsController extends Controller
|
||||
['label' => 'Overview', 'href' => route('admin.academy.analytics.overview')],
|
||||
['label' => 'Intelligence', 'href' => route('admin.academy.analytics.intelligence')],
|
||||
['label' => 'Content', 'href' => route('admin.academy.analytics.content')],
|
||||
['label' => 'Prompt Library', 'href' => route('admin.academy.analytics.prompt-library')],
|
||||
['label' => 'Prompts', 'href' => route('admin.academy.analytics.prompts')],
|
||||
['label' => 'Lessons', 'href' => route('admin.academy.analytics.lessons')],
|
||||
['label' => 'Courses', 'href' => route('admin.academy.analytics.courses')],
|
||||
|
||||
Reference in New Issue
Block a user