(function() {
function getUrlParam(name) {
const params = new URLSearchParams(window.location.search);
return params.get(name);
}
function setCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + encodeURIComponent(value || "") + expires + "; path=/";
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function fillFormFieldInClass(className, value) {
const container = document.querySelector('.' + className);
if (container) {
const input = container.querySelector('input');
if (input) {
input.value = value;
}
}
}
window.addEventListener('DOMContentLoaded', function() {
const gclidFromUrl = getUrlParam('gclid');
if (gclidFromUrl) {
setCookie('gclid', gclidFromUrl, 30); // store for 30 days
fillFormFieldInClass('gclid', gclidFromUrl);
} else {
const gclidFromCookie = getCookie('gclid');
if (gclidFromCookie) {
fillFormFieldInClass('gclid', gclidFromCookie);
}
}
});
})();