/**
 * Profit Warden - Tracking Context Script
 * Captures UTM parameters from URL and saves them in local cookies (30 days persistence)
 * so they can be saved on WooCommerce order creation.
 */
document.addEventListener('DOMContentLoaded', function() {
    function setCookie(name, value, days) {
        let expires = "";
        if (days) {
            let date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax";
    }

    // Parameters to track
    const paramsToTrack = [
        'utm_source',
        'utm_medium',
        'utm_campaign',
        'fbclid',
        'gclid'
    ];

    const urlParams = new URLSearchParams(window.location.search);
    
    paramsToTrack.forEach(function(param) {
        if (urlParams.has(param)) {
            // Save each param to a 30-day cookie
            setCookie('profwabr_track_' + param, encodeURIComponent(urlParams.get(param)), 30);
        }
    });
});
