window.addEventListener('load', function() {
// Broader selector to catch Jotform-generated iframes
var formIframes = document.querySelectorAll('iframe[id*="JotForm"], iframe[src*="jotform"]');
if (!formIframes.length) {
// Iframe not ready yet, retry after short delay
setTimeout(function() {
window.dispatchEvent(new Event('load'));
}, 1500);
return;
}
var utmSource = sessionStorage.getItem('utm_source') || 'direct';
var utmMedium = sessionStorage.getItem('utm_medium') || 'none';
var utmCampaign = sessionStorage.getItem('utm_campaign') || 'none';
var ref = new URLSearchParams(window.location.search).get('ref') || '';
formIframes.forEach(function(formIframe) {
var iframeSrc = formIframe.src;
iframeSrc += (iframeSrc.includes('?') ? '&' : '?');
iframeSrc += 'utm_source=' + encodeURIComponent(utmSource);
iframeSrc += '&utm_medium=' + encodeURIComponent(utmMedium);
iframeSrc += '&utm_campaign=' + encodeURIComponent(utmCampaign);
if (ref) iframeSrc += '&page_source=' + encodeURIComponent(ref);
formIframe.src = iframeSrc;
});
});