/* ============================================================
UTM Cookie Setup — captures traffic source on first visit
============================================================ */
(function () {
function getParam(name) {
const match = window.location.search.match(new RegExp('[?&]' + name + '=([^&]*)'));
return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null;
}
function isSearchEngine(hostname) {
const engines = {
'google.': 'google',
'bing.com': 'bing',
'yahoo.': 'yahoo',
'duckduckgo.com': 'duckduckgo'
};
for (const key in engines) {
if (hostname.includes(key)) return engines[key];
}
return null;
}
function isExternalReferrer(referrer) {
if (!referrer) return false;
try {
const refHost = new URL(referrer).hostname.replace('www.', '');
const curHost = window.location.hostname.replace('www.', '');
return refHost !== curHost;
} catch (e) { return false; }
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
return parts.length > 1 ? parts.pop().split(';').shift() : null;
}
const existing = getCookie('utm_source_info');
const utm_source = getParam('utm_source');
const utm_medium = getParam('utm_medium');
const utm_campaign = getParam('utm_campaign');
const utm_term = getParam('utm_term') || getParam('utm_keyword');
const gclid = getParam('gclid');
const gad_source = getParam('gad_source');
const fbclid = getParam('fbclid');
let source = '';
if (!existing || utm_source || gclid || fbclid || isExternalReferrer(document.referrer)) {
if (utm_source || utm_medium || utm_campaign || utm_term || gclid) {
if (gclid || gad_source) {
source = `utmcsr=google|utmcmd=cpc|utmccn=${encodeURIComponent(utm_campaign || '(not set)')}|utmctr=${encodeURIComponent(utm_term || '(not provided)')}${gclid ? '|utmgclid=' + encodeURIComponent(gclid) : ''}`;
} else {
source = `utmcsr=${encodeURIComponent(utm_source || '(direct)')}|utmcmd=${encodeURIComponent(utm_medium || '(none)')}|utmccn=${encodeURIComponent(utm_campaign || '(not set)')}|utmctr=${encodeURIComponent(utm_term || '(not provided)')}`;
}
} else if (isExternalReferrer(document.referrer)) {
const refHost = new URL(document.referrer).hostname.replace('www.', '');
const engine = isSearchEngine(refHost);
source = engine
? `utmcsr=${encodeURIComponent(engine)}|utmcmd=organic|utmccn=(not set)|utmctr=(not provided)`
: `utmcsr=${encodeURIComponent(refHost)}|utmcmd=referral|utmccn=(not set)|utmctr=(not provided)`;
} else if (!existing) {
source = `utmcsr=(direct)|utmcmd=(none)|utmccn=(not set)|utmctr=(not provided)`;
}
// --- Facebook Ad landing-page override ---
const AD_LANDING_PAGES = ['/boston-movers', '/boston-movers/'];
const isAdLP = AD_LANDING_PAGES.includes(window.location.pathname);
if (isAdLP) {
const ref = (document.referrer || '').toLowerCase();
const fromFacebook =
fbclid ||
(utm_source && utm_source.toLowerCase().includes('facebook')) ||
/facebook\.com|fb\.com|fb\.me|fbcdn|instagram\.com/i.test(ref);
if (fromFacebook) {
source = `utmcsr=facebook_ad|utmcmd=cpc|utmccn=${encodeURIComponent(utm_campaign || '(not set)')}|utmctr=${encodeURIComponent(utm_term || '(not provided)')}${fbclid ? '|utmfbclid=' + encodeURIComponent(fbclid) : ''}`;
}
}
if (source) {
document.cookie = `utm_source_info=${source}; path=/; max-age=${60 * 60 * 24 * 90}; Secure; SameSite=Lax`;
}
}
})();
/* ============================================================
Populate UTM hidden inputs in both moving forms
============================================================ */
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
return parts.length > 1 ? decodeURIComponent(parts.pop().split(';').shift()) : '';
}
function getFromCookie(str, key) {
const match = str.match(new RegExp(key + '=([^|]+)'));
return match ? decodeURIComponent(match[1]) : '';
}
function normalizeSource(source, paidType) {
if (!source) return 'Direct';
const s = source.toLowerCase();
if (s.includes('google')) return paidType === 'Paid' ? 'Google Ads' : 'Google Organic';
if (s.includes('bing')) return 'Bing';
if (s.includes('yahoo')) return 'Yahoo';
if (s.includes('youtube') || s.includes('youtu.be')) return 'YouTube';
if (s === 'ig' || s.includes('instagram')) return 'Instagram';
if (s.includes('facebook_ad')) return 'Facebook Ad';
if (s.includes('facebook')) return 'Facebook';
if (s.includes('linkedin')) return 'LinkedIn';
//if (s.includes('twitter') || s.includes('t.co')) return 'Twitter';
if (s.includes('twitter') || s.includes('x.com') ||
s === 't.co' || s.endsWith('.t.co') || s.includes('//t.co')) {
return 'Twitter';
}
if (s.includes('yelp')) return 'Yelp';
if (s.includes('trustpilot')) return 'Trustpilot';
if (s.includes('bbb')) return 'BBB';
if (s.includes('angi')) return 'Angi';
if (s === '(direct)' || s === 'direct') return 'Direct';
return source.charAt(0).toUpperCase() + source.slice(1);
}
function normalizeCampaign(c) {
if (!c || c === '(not set)') return '(Not Set)';
return c.replace(/[_-]+/g, ' ').replace(/\b\w/g, ch => ch.toUpperCase());
}
function getPaidType(utmCmd, hasGclid) {
if (hasGclid) return 'Paid';
return ['cpc', 'ppc', 'paid'].includes((utmCmd || '').toLowerCase()) ? 'Paid' : 'Organic';
}
function populateUTMFields() {
const utmInfo = getCookie('utm_source_info');
if (!utmInfo) return;
const rawSource = getFromCookie(utmInfo, 'utmcsr') || '(direct)';
const rawMedium = getFromCookie(utmInfo, 'utmcmd') || '(none)';
const rawCampaign = getFromCookie(utmInfo, 'utmccn') || '(not set)';
const gclidValue = getFromCookie(utmInfo, 'utmgclid');
const paidType = getPaidType(rawMedium, !!gclidValue);
const utmSource = normalizeSource(rawSource, paidType);
const utmCampaign = normalizeCampaign(rawCampaign);
/* Form 1 = moving-form-2026.php, Form 2 = moving-form-nh-2026.php */
const fieldMap = { utm_source: 23, utm_medium: 24, utm_campaign: 25 };
const values = { utm_source: utmSource, utm_medium: rawMedium, utm_campaign: utmCampaign };
Object.entries(fieldMap).forEach(function ([key, fieldId]) {
const el = document.getElementById(`input_${fieldId}`);
if (el) el.value = values[key] || '';
});
}
/* Run on DOM ready + GF AJAX transitions */
document.addEventListener('DOMContentLoaded', populateUTMFields);
document.addEventListener('gform_page_loaded', populateUTMFields);
document.addEventListener('gform_post_render', populateUTMFields);
























































































