const UTM_KEYS = [ "utm_medium", "utm_content", "utm_campaign", "utm_source", "utm_referrer", "gad_source", "gclid", ]; const validateUTMs = () => { // get query params const queryString = window.location.search.substring(1); const urlParams = new URLSearchParams(queryString); // If exists key value add to sessionStorage for(const key of UTM_KEYS) { const param = urlParams.get(key); if(!!param) sessionStorage.setItem(key, param); } } const addUTMsToFields = () => { // If exists key value add to field value for(const key of UTM_KEYS) { const value = sessionStorage.getItem(key); const input = document.querySelector(`input[name="${key}"]`); if(!!value && !!input) input.value = value; } } window.addEventListener("load", async function () { validateUTMs(); addUTMsToFields(); });