Created
January 13, 2024 01:19
-
-
Save iamFoxx/197c5ece0526c3a90fadd7ca61c3f7aa to your computer and use it in GitHub Desktop.
Collecting UTMs and automatic form filling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function cretaeField(name, value, form) { | |
if (value === null) return; | |
var input = document.createElement('input'); | |
input.type = 'hidden'; | |
input.name = name; | |
input.value = value; | |
form.appendChild(input); | |
} | |
const valueExists = (value) => { | |
return (value != null && value !== '' && value !== undefined) | |
} | |
const setValue = (name, value) => { | |
sessionStorage.setItem(name, decodeURIComponent(value)); | |
return value | |
} | |
const getValue = (name) => sessionStorage.getItem(name) ?? 'n/a'; | |
function param(name) { | |
let params = new URLSearchParams(document.location.search); | |
return params.has(name) && valueExists(params.get(name)) ? setValue(name, params.get(name)) : getValue(name ); | |
} | |
var forms = document.querySelectorAll('.lead-form'); | |
for (var i = 0; i < forms.length; i++) { | |
var form = forms[i]; | |
cretaeField('utm_source', param('utm_source'), form); | |
cretaeField('utm_campaign', param('utm_campaign'), form); | |
cretaeField('utm_medium', param('utm_medium'), form); | |
cretaeField('referral', document.referrer, form); | |
cretaeField('_origin', window.location.href, form); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment