<script>
// CONFIG: map UTM parameter names -> input selectors
const fieldMap = {
utm_source: 'input[name="1097612_221968pi_1097612_221968"]',
utm_medium: 'input[name="1097612_221965pi_1097612_221965"]',
utm_campaign: 'input[name="1097612_221959pi_1097612_221959"]',
utm_content: 'input[name="1097612_221962pi_1097612_221962"]'
};
// CONFIG: whether to save values read from the URL into sessionStorage
const saveUrlToSession = true;
function safeSessionGet(key) {
try {
return sessionStorage.getItem(key);
} catch (e) {
return null;
}
}
function safeSessionSet(key, value) {
if (!saveUrlToSession) return;
try {
sessionStorage.setItem(key, value);
} catch (e) {
// ignore storage write failures
}
}
function getUrlParam(paramName) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(paramName); // returns null if not present
}
function setFieldValue(el, value) {
if (!el) return;
el.value = value;
// trigger input/change events for libraries that listen for them
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
}
function populateKey(paramName, selector) {
const el = document.querySelector(selector);
// 1) Try sessionStorage first
const sessVal = safeSessionGet(paramName);
if (sessVal !== null && sessVal !== '') {
setFieldValue(el, sessVal);
return;
}
// 2) Fallback to URL param
const urlVal = getUrlParam(paramName);
if (urlVal !== null && urlVal !== '') {
setFieldValue(el, urlVal);
safeSessionSet(paramName, urlVal); // store for future pages if enabled
}
}
function populateHiddenFields() {
Object.keys(fieldMap).forEach(paramName => {
populateKey(paramName, fieldMap[paramName]);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', populateHiddenFields);
} else {
populateHiddenFields();
}
// Optional: allow manual calls from other scripts
window.populateHiddenFields = populateHiddenFields;
(function () {
// CONFIG
const fieldMap = {
utm_source: 'input[name="1097612_221968pi_1097612_221968"]',
utm_medium: 'input[name="1097612_221965pi_1097612_221965"]',
utm_campaign: 'input[name="1097612_221959pi_1097612_221959"]',
utm_content: 'input[name="1097612_221962pi_1097612_221962"]'
};
const saveUrlToSession = true;
// Safe session helpers
function safeSessionGet(key) {
try { return sessionStorage.getItem(key); } catch (e) { return null; }
}
function safeSessionSet(key, value) {
if (!saveUrlToSession) return;
try { sessionStorage.setItem(key, value); } catch (e) {}
}
// URL helper
function getUrlParam(paramName) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(paramName);
}
// DOM helper
function setFieldValue(el, value) {
if (!el) return;
el.value = value;
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
}
// Core: check session first, then URL
function populateKey(paramName, selector) {
const el = document.querySelector(selector);
const sessVal = safeSessionGet(paramName);
if (sessVal !== null && sessVal !== '') {
setFieldValue(el, sessVal);
return;
}
const urlVal = getUrlParam(paramName);
if (urlVal !== null && urlVal !== '') {
setFieldValue(el, urlVal);
safeSessionSet(paramName, urlVal);
}
}
// Populate all configured fields
function populateHiddenFields() {
Object.keys(fieldMap).forEach(paramName => {
populateKey(paramName, fieldMap[paramName]);
});
}
// Init
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', populateHiddenFields);
} else {
populateHiddenFields();
}
window.populateHiddenFields = populateHiddenFields;
})();
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Function to get URL parameters
function getUrlParam(paramName) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(paramName);
}
// Get the utm_source value
const utmSource = getUrlParam('utm_source');
const utmMedium = getUrlParam('utm_medium'); // Also get others
const utmCampaign = getUrlParam('utm_campaign');
const utmContent = getUrlParam('utm_content');
// --- Method 1: Direct Assignment (if form fields exist) ---
// Find your form fields by name/ID and set their value
const sourceField = document.querySelector('input[name="1097612_221968pi_1097612_221968"]'); // Adjust selector
if (sourceField && utmSource) {
sourceField.value = utmSource;
}
const mediumField = document.querySelector('input[name="1097612_221965pi_1097612_221965"]'); // Adjust selector
if (mediumField && utmMedium) {
mediumField.value = utmMedium;
}
const campaignField = document.querySelector('input[name="1097612_221959pi_1097612_221959"]'); // Adjust selector
if (campaignField && utmCampaign) {
campaignField.value = utmCampaign;
}
const contentField = document.querySelector('input[name="1097612_221962pi_1097612_221962"]'); // Adjust selector
if (contentField && utmContent) {
contentField.value = utmContent;
}
// Repeat for medium, campaign, etc.
// --- Method 2: Store in Session Storage (Recommended for multi-page) ---
// This keeps data across page views before form submission
if (utmSource) {
sessionStorage.setItem('utm_source', utmSource);
}
if (utmMedium) {
sessionStorage.setItem('utm_medium', utmMedium);
}
if (utmCampaign) {
sessionStorage.setItem('utm_campaign', utmCampaign);
}
if (utmContent) {
sessionStorage.setItem('utm_content', utmContent);
}
});
// Function to populate form on any page load (using session storage)
function populateHiddenFields() {
const utm_source = sessionStorage.getItem('utm_source');
const utm_medium = sessionStorage.getItem('utm_medium');
const utm_campaign = sessionStorage.getItem('utm_campaign');
const utm_content = sessionStorage.getItem('utm_content');
const sourceInput = document.querySelector('input[name="1097612_221968pi_1097612_221968"]');
if (sourceInput && utm_source) {
sourceInput.value = utm_source;
}
const mediumInput = document.querySelector('input[name="1097612_221965pi_1097612_221965"]');
if (mediumInput && utm_medium) {
mediumInput.value = utm_medium;
}
const campaignInput = document.querySelector('input[name="1097612_221959pi_1097612_221959"]');
if (campaignInput && utm_campaign) {
campaignInput.value = utm_campaign;
}
const contentInput = document.querySelector('input[name="1097612_221962pi_1097612_221962"]');
if (contentInput && utm_content) {
contentInput.value = utm_content;
}
}
// Run this when the form loads on the *final* page (e.g., the form page itself)
// Call populateHiddenFields(); when your form element is ready or on DOMContentLoaded again.
</script>