// List of UTM parameters you want to capture
const utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
// Function to get URL parameter by name
function getUrlParameter(name) {
name = name.replace(/[[]/, '\[').replace(/[]]/, '\]');
const regex = new RegExp('[\?&]' + name + '=([^]*)');
const results = regex.exec(window.location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/+/g, ' '));
}
// On DOM ready, set hidden fields if present
document.addEventListener('DOMContentLoaded', function() {
utmParams.forEach(function(param) {
const value = getUrlParameter(param);
if (value) {
// Look for any input field (hidden) with matching name
const input = document.querySelector('input[name="' + param + '"]');
if (input) {
input.value = value;
}
}
});
});