refactor: move Analytics from ThemeSettings to AdsensePlacement
- Remove Analytics and AdSense tabs from theme-settings component - Add Analytics group to adsense-placement component - Add roi_enqueue_analytics_script() for GA4/UA support - Clean up ThemeSettings to only handle custom code (CSS/JS) - Update FormBuilders and FieldMappers accordingly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* AdSense Placement - Helper Functions
|
||||
* AdSense Placement & Google Analytics - Helper Functions
|
||||
*
|
||||
* Funciones para usar en templates:
|
||||
* - roi_render_ad_slot('post-top')
|
||||
* - roi_render_rail_ads() - Para los margenes laterales del viewport
|
||||
*
|
||||
* Funciones auto-registradas en wp_head:
|
||||
* - roi_enqueue_adsense_script() - Script principal de AdSense
|
||||
* - roi_enqueue_analytics_script() - Script de Google Analytics
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
@@ -231,3 +235,106 @@ function roi_inject_content_ads(string $content): string
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Carga el script de Google Analytics (GA4 o Universal Analytics)
|
||||
*/
|
||||
function roi_enqueue_analytics_script(): void
|
||||
{
|
||||
global $container;
|
||||
|
||||
if ($container === null || is_admin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$repository = $container->getComponentSettingsRepository();
|
||||
$settings = $repository->getComponentSettings('adsense-placement');
|
||||
|
||||
// Verificar si Analytics esta habilitado
|
||||
if (!($settings['analytics']['analytics_enabled'] ?? false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$trackingId = trim($settings['analytics']['ga_tracking_id'] ?? '');
|
||||
if (empty($trackingId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verificar si GA ya esta cargado por otro plugin
|
||||
if (roi_is_analytics_loaded()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$anonymizeIp = ($settings['analytics']['ga_anonymize_ip'] ?? true) === true;
|
||||
|
||||
// Detectar tipo de ID (GA4 vs Universal Analytics)
|
||||
if (strpos($trackingId, 'G-') === 0) {
|
||||
roi_render_ga4_script($trackingId, $anonymizeIp);
|
||||
} elseif (strpos($trackingId, 'UA-') === 0) {
|
||||
roi_render_universal_analytics_script($trackingId, $anonymizeIp);
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
if (defined('WP_DEBUG') && WP_DEBUG) {
|
||||
error_log('ROI Analytics: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action('wp_head', 'roi_enqueue_analytics_script', 1);
|
||||
|
||||
/**
|
||||
* Verifica si Google Analytics ya esta cargado por otro plugin
|
||||
*/
|
||||
function roi_is_analytics_loaded(): bool
|
||||
{
|
||||
// Verificar si MonsterInsights esta activo
|
||||
if (class_exists('MonsterInsights_Lite') || class_exists('MonsterInsights')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Verificar si Site Kit de Google esta activo
|
||||
if (class_exists('Google\Site_Kit\Plugin')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renderiza script de Google Analytics 4
|
||||
*/
|
||||
function roi_render_ga4_script(string $trackingId, bool $anonymizeIp): void
|
||||
{
|
||||
$config = $anonymizeIp ? "{ 'anonymize_ip': true }" : '{}';
|
||||
|
||||
echo '<!-- Google Analytics 4 (ROI Theme) -->' . "\n";
|
||||
echo '<script async src="https://www.googletagmanager.com/gtag/js?id=' . esc_attr($trackingId) . '"></script>' . "\n";
|
||||
echo '<script>' . "\n";
|
||||
echo 'window.dataLayer = window.dataLayer || [];' . "\n";
|
||||
echo 'function gtag(){dataLayer.push(arguments);}' . "\n";
|
||||
echo 'gtag("js", new Date());' . "\n";
|
||||
echo 'gtag("config", "' . esc_js($trackingId) . '", ' . $config . ');' . "\n";
|
||||
echo '</script>' . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renderiza script de Universal Analytics (legacy)
|
||||
*/
|
||||
function roi_render_universal_analytics_script(string $trackingId, bool $anonymizeIp): void
|
||||
{
|
||||
$anonymizeConfig = $anonymizeIp ? 'ga("set", "anonymizeIp", true);' : '';
|
||||
|
||||
echo '<!-- Universal Analytics (ROI Theme) -->' . "\n";
|
||||
echo '<script>' . "\n";
|
||||
echo '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){' . "\n";
|
||||
echo '(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),' . "\n";
|
||||
echo 'm=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)' . "\n";
|
||||
echo '})(window,document,"script","https://www.google-analytics.com/analytics.js","ga");' . "\n";
|
||||
echo 'ga("create", "' . esc_js($trackingId) . '", "auto");' . "\n";
|
||||
if (!empty($anonymizeConfig)) {
|
||||
echo $anonymizeConfig . "\n";
|
||||
}
|
||||
echo 'ga("send", "pageview");' . "\n";
|
||||
echo '</script>' . "\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user