<?php
// includes/page-header-utm.php

/**
 * Script para incluir no <head> das páginas
 * Captura UTM imediatamente quando a página carrega
 */

// Só executa se houver UTM na URL
if (!empty($_GET['utm_campaign'])) {
    ?>
    <script type="text/javascript">
    // Captura UTM ASAP
    (function() {
        const utmParams = new URLSearchParams(window.location.search);
        
        if (utmParams.has('utm_campaign')) {
            const data = {
                utm_campaign: utmParams.get('utm_campaign') || '',
                utm_source: utmParams.get('utm_source') || '',
                utm_medium: utmParams.get('utm_medium') || '',
                utm_term: utmParams.get('utm_term') || '',
                utm_content: utmParams.get('utm_content') || ''
            };
            
            // Salva imediatamente
            fetch('<?php echo defined('SITE_URL') ? SITE_URL : ''; ?>/save-utm.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            }).catch(e => console.warn('UTM capture failed:', e));
        }
    })();
    </script>
    <?php
}
?>