/**
 * Lean Analytics Beacon
 * Privacy-first tracking beacon using sendBeacon API
 */
(function() {
    'use strict';
    
    // Check if sendBeacon is supported
    if (!navigator.sendBeacon) {
        return;
    }
    
    // Get current page data
    var path = window.location.pathname;
    var url = window.location.href;
    var ref = document.referrer || '';
    
    // Parse UTM parameters
    var urlParams = new URLSearchParams(window.location.search);
    var utm_source = urlParams.get('utm_source') || '';
    var utm_medium = urlParams.get('utm_medium') || '';
    var utm_campaign = urlParams.get('utm_campaign') || '';
    
    // Prepare data
    var data = new FormData();
    data.append('action', 'lean_track');
    data.append('nonce', leanAnalytics.nonce);
    data.append('path', path);
    data.append('url', url);
    data.append('ref', ref);
    data.append('utm_source', utm_source);
    data.append('utm_medium', utm_medium);
    data.append('utm_campaign', utm_campaign);
    
    // Send beacon
    navigator.sendBeacon(leanAnalytics.ajaxUrl, data);
})();
