fix(analytics): Corregir queries y unificar UI de recomendaciones

Fixes:
- CTR 0%: Cambiar query de IS NULL a HAVING clicks=0 para CTR global real
- Decay content: Aumentar umbral de 3 a 5 clicks para evitar ruido
- Click distribution: Expandir de posiciones 1-5 a 1-10 individual
- Remover URL redundante de tabla Top Posts

UI:
- Convertir 3 secciones de recomendaciones a formato tabla consistente:
  * ACCIÓN URGENTE: Contenido a Crear
  * REVISAR: Títulos con CTR 0%
  * QUICK WINS: Oportunidades Fáciles

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-12-03 21:55:10 -06:00
parent 41fb658ca7
commit 3367f06ee8
2 changed files with 109 additions and 64 deletions

View File

@@ -309,89 +309,124 @@ final class ROI_APU_Analytics_Dashboard
<!-- 🔴 ACCIÓN URGENTE: Contenido a Crear -->
<?php if (!empty($zero_results)) : ?>
<div class="recommendation-card mb-4" role="alert"
style="border-left: 4px solid #ef4444; background-color: #fef2f2; border-radius: 0.5rem; padding: 1rem;">
<div class="d-flex justify-content-between align-items-center mb-2">
<h6 class="mb-0 d-flex align-items-center gap-2" style="color: #7f1d1d;">
<div class="card mb-4 shadow-sm" style="border-left: 4px solid #ef4444;">
<div class="card-header d-flex justify-content-between align-items-center" style="background-color: #ef4444; color: white;">
<h6 class="mb-0 d-flex align-items-center gap-2">
<i class="bi bi-exclamation-triangle-fill"></i>
<?php esc_html_e('ACCIÓN URGENTE: Contenido a Crear', 'roi-apu-search'); ?>
</h6>
<span class="badge" style="background-color: #ef4444;"><?php echo count($zero_results); ?></span>
<span class="badge bg-light text-dark"><?php echo count($zero_results); ?></span>
</div>
<p class="small mb-2" style="color: #991b1b;">
<div class="card-body p-0">
<p class="small text-muted px-3 pt-3 mb-2">
<?php esc_html_e('Los usuarios buscan esto pero NO encuentran nada. ¡Crea este contenido!', 'roi-apu-search'); ?>
</p>
<div class="small">
<?php foreach ($zero_results as $index => $term) : ?>
<div class="d-flex justify-content-between py-1 <?php echo $index < count($zero_results) - 1 ? 'border-bottom' : ''; ?>" style="border-color: #fecaca !important;">
<span style="color: #7f1d1d;">
<strong><?php echo esc_html($term['q_term']); ?></strong>
</span>
<span style="color: #991b1b;">
<?php echo esc_html(number_format((int)$term['frecuencia'])); ?> <?php esc_html_e('veces buscado', 'roi-apu-search'); ?>
</span>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width: 70%;"><?php esc_html_e('Término buscado', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('Frecuencia', 'roi-apu-search'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($zero_results as $term) : ?>
<tr>
<td><strong><?php echo esc_html(mb_strimwidth($term['q_term'], 0, 80, '...')); ?></strong></td>
<td class="text-center">
<span class="badge bg-danger"><?php echo esc_html(number_format((int)$term['frecuencia'])); ?></span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<!-- 🟡 REVISAR: Títulos con CTR 0% -->
<?php if (!empty($ctr_zero)) : ?>
<div class="recommendation-card mb-4" role="alert"
style="border-left: 4px solid #f59e0b; background-color: #fffbeb; border-radius: 0.5rem; padding: 1rem;">
<div class="d-flex justify-content-between align-items-center mb-2">
<h6 class="mb-0 d-flex align-items-center gap-2" style="color: #78350f;">
<div class="card mb-4 shadow-sm" style="border-left: 4px solid #f59e0b;">
<div class="card-header d-flex justify-content-between align-items-center" style="background-color: #f59e0b; color: white;">
<h6 class="mb-0 d-flex align-items-center gap-2">
<i class="bi bi-pencil-square"></i>
<?php esc_html_e('REVISAR: Títulos con CTR 0%', 'roi-apu-search'); ?>
</h6>
<span class="badge" style="background-color: #f59e0b;"><?php echo count($ctr_zero); ?></span>
<span class="badge bg-light text-dark"><?php echo count($ctr_zero); ?></span>
</div>
<p class="small mb-2" style="color: #92400e;">
<div class="card-body p-0">
<p class="small text-muted px-3 pt-3 mb-2">
<?php esc_html_e('Estos términos tienen resultados pero nadie hace click. Mejora títulos y descripciones.', 'roi-apu-search'); ?>
</p>
<div class="small">
<?php foreach ($ctr_zero as $index => $term) : ?>
<div class="d-flex justify-content-between py-1 <?php echo $index < count($ctr_zero) - 1 ? 'border-bottom' : ''; ?>" style="border-color: #fde68a !important;">
<span style="color: #78350f;">
<strong><?php echo esc_html($term['q_term']); ?></strong>
<small>(<?php echo esc_html(number_format((int)$term['resultados'])); ?> <?php esc_html_e('resultados', 'roi-apu-search'); ?>)</small>
</span>
<span style="color: #92400e;">
<?php echo esc_html(number_format((int)$term['busquedas'])); ?> <?php esc_html_e('búsquedas', 'roi-apu-search'); ?> → 0 clicks
</span>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width: 50%;"><?php esc_html_e('Término buscado', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('Resultados', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('Búsquedas', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('Clicks', 'roi-apu-search'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($ctr_zero as $term) : ?>
<tr>
<td><strong><?php echo esc_html(mb_strimwidth($term['q_term'], 0, 60, '...')); ?></strong></td>
<td class="text-center"><?php echo esc_html(number_format((int)$term['resultados'])); ?></td>
<td class="text-center">
<span class="badge bg-warning text-dark"><?php echo esc_html(number_format((int)$term['busquedas'])); ?></span>
</td>
<td class="text-center">
<span class="badge bg-danger">0</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<!-- 🎯 QUICK WINS: Oportunidades Fáciles -->
<?php if (!empty($quick_wins)) : ?>
<div class="recommendation-card mb-4" role="alert"
style="border-left: 4px solid #0284c7; background-color: #e7f3ff; border-radius: 0.5rem; padding: 1rem;">
<div class="d-flex justify-content-between align-items-center mb-2">
<h6 class="mb-0 d-flex align-items-center gap-2" style="color: #0c4a6e;">
<div class="card mb-4 shadow-sm" style="border-left: 4px solid #0284c7;">
<div class="card-header d-flex justify-content-between align-items-center" style="background-color: #0284c7; color: white;">
<h6 class="mb-0 d-flex align-items-center gap-2">
<i class="bi bi-bullseye"></i>
<?php esc_html_e('QUICK WINS: Oportunidades Fáciles', 'roi-apu-search'); ?>
</h6>
<span class="badge" style="background-color: #0284c7;"><?php echo count($quick_wins); ?></span>
<span class="badge bg-light text-dark"><?php echo count($quick_wins); ?></span>
</div>
<p class="small mb-2" style="color: #075985;">
<div class="card-body p-0">
<p class="small text-muted px-3 pt-3 mb-2">
<?php esc_html_e('Términos con clicks que no están en top 1. Una pequeña mejora = más visibilidad.', 'roi-apu-search'); ?>
</p>
<div class="small">
<?php foreach ($quick_wins as $index => $term) : ?>
<div class="d-flex justify-content-between py-1 <?php echo $index < count($quick_wins) - 1 ? 'border-bottom' : ''; ?>" style="border-color: #bae6fd !important;">
<span style="color: #0c4a6e;">
<strong><?php echo esc_html($term['q_term']); ?></strong>
</span>
<span style="color: #075985;">
<?php esc_html_e('Pos.', 'roi-apu-search'); ?> <?php echo esc_html($term['pos_prom']); ?>,
CTR <?php echo esc_html($term['ctr']); ?>%
</span>
</div>
<div class="table-responsive">
<table class="table table-striped table-hover mb-0">
<thead class="table-light">
<tr>
<th style="width: 50%;"><?php esc_html_e('Término buscado', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('Pos. Prom.', 'roi-apu-search'); ?></th>
<th class="text-center"><?php esc_html_e('CTR', 'roi-apu-search'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($quick_wins as $term) : ?>
<tr>
<td><strong><?php echo esc_html(mb_strimwidth($term['q_term'], 0, 60, '...')); ?></strong></td>
<td class="text-center">
<span class="badge bg-secondary"><?php echo esc_html($term['pos_prom']); ?></span>
</td>
<td class="text-center">
<span class="badge bg-info text-dark"><?php echo esc_html($term['ctr']); ?>%</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
@@ -683,7 +718,6 @@ final class ROI_APU_Analytics_Dashboard
<tr>
<td>
<strong><?php echo esc_html(mb_strimwidth($post['post_title'], 0, 80, '...')); ?></strong>
<br><small class="text-muted">/<?php echo esc_html($post['post_name']); ?>/</small>
</td>
<td class="text-center"><?php echo esc_html(number_format((int)$post['clicks'])); ?></td>
<td class="text-center">

View File

@@ -153,7 +153,8 @@ final class ROI_APU_Metrics_Repository
}
/**
* Get terms with CTR 0% (have results but no clicks)
* Get terms with truly 0% CTR (have results but ZERO clicks globally)
* Only shows terms where no search instance ever got a click
*
* @param int $days Number of days to query
* @param int $limit Max results
@@ -162,14 +163,18 @@ final class ROI_APU_Metrics_Repository
public function getCTRZero(int $days = 30, int $limit = 20): array
{
try {
$sql = "SELECT s.q_term, COUNT(DISTINCT s.id) as busquedas, MAX(s.total_results) as resultados
// Get terms with results but 0 total clicks (true 0% CTR)
$sql = "SELECT
s.q_term,
COUNT(DISTINCT s.id) as busquedas,
MAX(s.total_results) as resultados,
COUNT(DISTINCT c.id) as clicks
FROM {$this->prefix}rcp_paginas_querys s
LEFT JOIN {$this->prefix}rcp_paginas_querys_log c ON s.id = c.search_id
WHERE s.total_results > 0
AND c.id IS NULL
AND s.ts >= DATE_SUB(NOW(), INTERVAL :days DAY)
GROUP BY s.q_term
HAVING busquedas >= 3
HAVING clicks = 0 AND busquedas >= 3
ORDER BY busquedas DESC
LIMIT :limit";
@@ -415,7 +420,7 @@ final class ROI_APU_Metrics_Repository
}
/**
* Get click distribution by position
* Get click distribution by position (positions 1-10 individually, 11+ grouped)
*
* @param int $days Number of days to query
* @return array<array{posicion: string, clicks: int, porcentaje: float}>
@@ -430,7 +435,12 @@ final class ROI_APU_Metrics_Repository
WHEN position = 3 THEN 'Pos 3'
WHEN position = 4 THEN 'Pos 4'
WHEN position = 5 THEN 'Pos 5'
ELSE 'Pos 6+'
WHEN position = 6 THEN 'Pos 6'
WHEN position = 7 THEN 'Pos 7'
WHEN position = 8 THEN 'Pos 8'
WHEN position = 9 THEN 'Pos 9'
WHEN position = 10 THEN 'Pos 10'
ELSE 'Pos 11+'
END as posicion,
COUNT(*) as clicks,
ROUND(COUNT(*) * 100.0 / NULLIF((
@@ -456,6 +466,7 @@ final class ROI_APU_Metrics_Repository
/**
* Get decaying content: posts that lost clicks vs previous period
* Only shows posts with significant traffic (≥5 clicks before) to avoid statistical noise
*
* @param int $days Number of days to query
* @param int $limit Max results
@@ -485,7 +496,7 @@ final class ROI_APU_Metrics_Repository
GROUP BY post_id
) curr ON pa.post_id = curr.post_id
JOIN {$this->prefix}posts p ON pa.post_id = p.ID
WHERE pa.clicks_anterior >= 3
WHERE pa.clicks_anterior >= 5
AND (COALESCE(curr.clicks_actual, 0) - pa.clicks_anterior) * 100.0 / pa.clicks_anterior <= -20
ORDER BY cambio_pct ASC
LIMIT :limit";