- Create a custom lead field called UTM source (logical name: utmsource).
- Make a new form for leads and add the UTM source field.
- Set that field to hidden in the form settings.
- Open the HTML editor and paste in the provided script.
- Update the script’s field ID to match your form’s field.
Reuse this example to enrich your leads with more UTM parameters, like utm_campaign, utm_medium, utm_term, and utm_content.
<script>
document.addEventListener("d365mkt-afterformload", updateUTMSourceField());
function updateUTMSourceField() {
const formField = document.getElementById("utmsource-1679335998029"); // The field ID is unique in each form, make sure you replace it with the correct value.
const urlParams = new URLSearchParams(window.location.search);
const utmSource = urlParams.get('utm_source');
formField.value = utmSource;
console.log(utmSource); // for debug only, can be removed
}
</script>