function getUTMParameters() { var params = {}; window.location.search.substring(1).split('&').forEach(function(param) { var pair = param.split('='); if (pair[0].startsWith('utm_')) { params[pair[0]] = decodeURIComponent(pair[1]); } }); return params; } function setCookie(name, value, minutes) { var expires = ""; if (minutes) { var date = new Date(); date.setTime(date.getTime() + (minutes * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } var utmParams = getUTMParameters(); for (var param in utmParams) { setCookie(param, utmParams[param], 30); // Storing each UTM parameter in a cookie for 30 minutes } function getCookie(name) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length == 2) return parts.pop().split(";").shift(); } function appendUTMParametersToForm() { var utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']; var form = document.querySelector('.hs-form-iframe').contentDocument; // Adjust this selector to target your form specifically if (form) { utmParams.forEach(function(param) { var value = getCookie(param); console.log(value); if (value) { var formField = form.querySelector(`input[name=${param}]`); jQuery(formField).val(value).change(); } }); } } // Run the function after the form has fully loaded to ensure the form is present window.addEventListener('message', event => { if(event.data.eventName === 'onFormReady') { console.log('form loaded'); appendUTMParametersToForm(); } });