Custom code not working 100%

Hi,
I have a optin page where traffic comes on through ads
On my thankyou page I have embedded a jotform where I want to pass the UTMs
So for this I need the form to redirect the user to a URL with the UTMs which the codes does properly.
The issue is that the preventdefault function sometimes prevents the form submission. So the user is redirected to the thank you page wit hthe proper UTMs but because there is no form submission it is not triggering the zap.

Here the code:


<script>
const runScript = () => {
	const form = document.getElementById("YOURFORMID");
	const urlParams = new URLSearchParams(window.location.search);
	const utmParams = [
		"utm_source",
		"utm_medium",
		"utm_campaign",
		"utm_term",
		"utm_content",
	];

	if (form) {
		utmParams.forEach((param) => {
			if (urlParams.has(param)) {
				let field = form.elements[param];
				if (field) {
					field.value = urlParams.get(param);
				}
			}
		});

		form.onsubmit = (e) => {
			e.preventDefault();

			let params = Array.from(urlParams)
				.filter(([key]) => utmParams.includes(key))
				.map(([key, value]) => `${key}=${value}`)
				.join("&");

			form.action =
				"https://yourthankyoupage.com" +
				(params ? "?" + params : "");
			form.submit();
		};
	}
};

if (document.readyState === "loading") {
	document.addEventListener("DOMContentLoaded", runScript);
} else {
	runScript();
}
</script>