





		function isLocalStorageAvailable(){
    var ls = 'ls';
    try {
        localStorage.setItem(ls, ls);
        localStorage.removeItem(ls);
        return true;
    } catch(e) {
        return false;
    }
}


const leadsQueryString = window.location.search;
const urlParams = new URLSearchParams(leadsQueryString);

// clear the items in localStorage before setting them
if (isLocalStorageAvailable()) {
    window.localStorage.removeItem("isVitalityActive");
    window.localStorage.removeItem("savingsJourney");
    window.localStorage.removeItem("lead_url_params");
    window.localStorage.removeItem("campaign");
    window.localStorage.removeItem("utmTerm");
    window.localStorage.removeItem("utmMedium");
    window.localStorage.removeItem("utmSource");
    window.localStorage.removeItem("utmContent");
    window.localStorage.removeItem("utmId");
    window.localStorage.removeItem("urlHasLeadReference");
    window.localStorage.removeItem("leadReferenceNumber");
    window.localStorage.removeItem("urlHasFacebookReference");
    window.localStorage.removeItem("facebookReferenceNumber");
    
}

if (isLocalStorageAvailable() && urlParams) {
    const isVitalityActive = urlParams.get('isVitalityActive');
    const leadsUTMCampaign = urlParams.get('utm_campaign');
    const leadRef = urlParams.get('leadRef');
    const fbclid = urlParams.get('fbclid');

    const leadsUTMterm = urlParams.get('utm_term');
    const leadsUTMmedium = urlParams.get('utm_medium');
    const leadsUTMsource = urlParams.get('utm_source');
    const leadsUTMcontent = urlParams.get('utm_content');
    const leadsUTMid = urlParams.get('utm_id');    

    window.localStorage.setItem('lead_url_params', urlParams);

    if (leadsUTMCampaign) {
        window.localStorage.setItem('campaign', leadsUTMCampaign);
    }

   if (isVitalityActive) {
       window.localStorage.setItem('isVitalityActive', isVitalityActive);
   }

    if (leadsUTMterm) {
        window.localStorage.setItem('utmTerm', leadsUTMterm);
    }

    if (leadsUTMmedium) {
        window.localStorage.setItem('utmMedium', leadsUTMmedium);
    }

    if (leadsUTMsource) {
        window.localStorage.setItem('utmSource', leadsUTMsource);
    }

    if (leadsUTMcontent) {
        window.localStorage.setItem('utmContent', leadsUTMcontent);
    }

    if (leadsUTMid) {
        window.localStorage.setItem('utmId', leadsUTMid);
    }

    if (leadRef) {
        window.localStorage.setItem('urlHasLeadReference', 'true');
        window.localStorage.setItem('leadReferenceNumber', leadRef);
    }

    if (fbclid) {
        window.localStorage.setItem('urlHasFacebookReference', 'true');
        window.localStorage.setItem('facebookReferenceNumber', fbclid);
    }
}

