///check if there was an instance of this script already loaded if (typeof queryParamsString === 'undefined') { // Define expiration time const EXPIRATION_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds // Function to get URL parameters from the current URL function getParamsFromURL() { const urlParams = new URLSearchParams(window.location.search); return { utm_medium: urlParams.get('utm_medium'), utm_source: urlParams.get('utm_source'), utm_campaign: urlParams.get('utm_campaign'), fbclid: urlParams.get('fbclid'), gclid: urlParams.get('gclid'), msclkid: urlParams.get('msclkid'), }; } // Function to check if tracking data has expired function hasTrackingDataExpired() { const timestamp = localStorage.getItem('tracking_data_timestamp'); if (!timestamp) { return true; // No timestamp means data doesn't exist or has expired } const currentTime = new Date().getTime(); return currentTime - parseInt(timestamp, 10) > EXPIRATION_TIME; } // Function to check if the campaign has changed function hasCampaignChanged(currentCampaign) { const storedCampaign = localStorage.getItem('utm_campaign'); // Ignore null or undefined currentCampaign and storedCampaign if (!currentCampaign || !storedCampaign) { return false; } // Check if the current campaign differs from the stored campaign return currentCampaign !== storedCampaign; } function getTrackingParameters() { // Get parameters from URL const params = getParamsFromURL(); // Get stored campaign const storedCampaign = localStorage.getItem('utm_campaign'); // Determine if the campaign has changed const campaignFromURL = params.utm_campaign; const campaignChanged = hasCampaignChanged(campaignFromURL); // Check if tracking data has expired const dataExpired = hasTrackingDataExpired(); // Initialize variables to hold the final values let utm_medium, utm_source, utm_campaign, fbclid, gclid, msclkid; if (dataExpired || campaignChanged) { // Use URL params or defaults utm_medium = params.utm_medium || ''; utm_source = params.utm_source || ''; utm_campaign = campaignFromURL; fbclid = params.fbclid || ''; gclid = params.gclid || ''; msclkid = params.msclkid || ''; // Save the final values to local storage along with a timestamp localStorage.setItem('utm_medium', utm_medium); localStorage.setItem('utm_source', utm_source); localStorage.setItem('utm_campaign', utm_campaign); localStorage.setItem('fbclid', fbclid); localStorage.setItem('gclid', gclid); localStorage.setItem('msclkid', msclkid); localStorage.setItem('tracking_data_timestamp', new Date().getTime().toString()); } else { // Data is present and hasn't expired, use stored values utm_medium = localStorage.getItem('utm_medium') || ''; utm_source = localStorage.getItem('utm_source') || ''; utm_campaign = storedCampaign; fbclid = localStorage.getItem('fbclid') || ''; gclid = localStorage.getItem('gclid') || ''; msclkid = localStorage.getItem('msclkid') || ''; } // Return the final parameters return { utm_medium, utm_source, utm_campaign, fbclid, gclid, msclkid, }; } // Function to get correct tracking parameters function getFinalParams() { const params = getTrackingParameters(); // Set defaults let medium = 'direct'; let source = 'direct'; // Known social media domains const socialDomains = ['facebook.com', 'twitter.com', 'instagram.com', 'linkedin.com', 'pinterest.com', 'tiktok.com']; // Known organic search engines const organicDomains = ['google.com', 'bing.com', 'yahoo.com', 'baidu.com', 'yandex.com', 'duckduckgo.com', 'ask.com']; // First priority to UTM parameters, with defaults if they are missing if (params.utm_medium || params.utm_source) { medium = params.utm_medium || medium; // Default to 'direct' if utm_medium is missing source = params.utm_source || source; // Default to 'direct' if utm_source is missing } // Next priority to PPC detection via gclid, fbclid, msclkid if (params.gclid) { medium = 'ppc'; source = 'google'; } else if (params.fbclid) { medium = 'ppc'; source = 'facebook'; } else if (params.msclkid) { medium = 'ppc'; source = 'bing'; } else { // If none of the above, then we check for referral or organic const referrer = document.referrer; if (referrer) { try { const referrerUrl = new URL(referrer); const referrerHostname = referrerUrl.hostname; // If the referrer is a social media site if (socialDomains.some(domain => referrerHostname.includes(domain))) { medium = 'social'; source = referrerHostname; } else if (organicDomains.some(domain => referrerHostname.includes(domain))) { // If the referrer is an organic search engine medium = 'organic'; source = referrerHostname; } else { // If the referrer is not the current website, set to referral const currentUrl = new URL(window.location.href); if (currentUrl.hostname !== referrerHostname) { medium = 'referral'; source = referrerHostname; } } } catch (e) { console.error('Invalid referrer URL:', e); } } else { // If no referrer and no UTM parameters, consider it direct medium = 'direct'; source = 'direct'; } } return { utm_source: source, utm_medium: medium, utm_campaign: params.utm_campaign || 'unknown', gclid: params.gclid, fbclid: params.fbclid, msclkid: params.msclkid }; } // Global variable to store query parameters string if (typeof queryParamsString === 'undefined') { var queryParamsString = ''; } // Function for creating iframe parameter function createIframeTrackingParam() { const { utm_source, utm_medium, utm_campaign, gclid, fbclid, msclkid } = getFinalParams(); const params = new URLSearchParams({ utm_source, utm_medium, utm_campaign }); const url = new URL(window.location.href); const utmUrl = `${url.origin}${url.pathname}`; params.set('utm_url', encodeURIComponent(utmUrl)); // Consolidate gclid, fbclid, msclkid into utm_clid const clid = gclid || fbclid || msclkid; if (clid) { params.set('utm_clid', clid); } // Construct the query parameters string queryParamsString = '?' + params.toString(); } // Call this function to start the collection of parameters createIframeTrackingParam(); }///end check if there was an instance of this script already loaded /////////////////////////////////////////////////////////////////////////// ////////////////////////custom session management////////////////////////// /////////////////////////////////////////////////////////////////////////// ////function for generating unique session function generateUniqueSessionID() { var timestamp = new Date().getTime(); // Current time in milliseconds var randomNum = Math.random().toString(36).substring(2, 15); // A random string return timestamp + '' + randomNum; } ////function for getting cookie function getCookie(name) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length == 2) return parts.pop().split(";").shift(); } ////function for setting cookie function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } var session = getCookie('chatbotsession'); if (!session) { // If session doesn't exist, create a new one session = generateUniqueSessionID(); setCookie('chatbotsession', session, 1); // Setting cookie for 1 day } /////////////////////////////////////////////////////////////////////////// ////////////////////////do the thing now/////////////////////////////////// /////////////////////////////////////////////////////////////////////////// ///create the script for iframe resizer var script = document.createElement('script'); script.onload = function () { iFrameResize({ log: false, heightCalculationMethod: 'bodyOffset', checkOrigin:false }, '#ssQuoteFormBotWidget12'); ////run resize once for height validation checksizeandhideiftoolarge(); ////then bind it to resize window.addEventListener('resize', checksizeandhideiftoolarge); }; script.src = 'https://demo.thequotebot.com/code/scripts/iframeresizer/iframeResizerBot.min.js'; document.head.appendChild(script); ////create the iframe var containerbox = document.getElementById('ssiframecontainerbot'); var appendatend = false; if (typeof(containerbox) == 'undefined' || containerbox == null){ ///create container if not already defined var containerbox = document.createElement('div'); containerbox.setAttribute('id', 'ssiframecontainerbot'); appendatend = true; } containerbox.innerHTML = ''; if(appendatend){ document.getElementsByTagName('body')[0].appendChild(containerbox); } ////function for height checks var iframeElementBox = document.getElementById("ssQuoteFormBotWidget12"); var ssQuoteFormBotWidgetheight = iframeElementBox.offsetWidth function checksizeandhideiftoolarge(){ if (window.innerHeight < ssQuoteFormBotWidgetheight) { iframeElementBox.style.display = "none"; } else { iframeElementBox.style.display = "block"; iframeElementBox.iFrameResizer.resize(); } } ///start listening for messages from iframe window.addEventListener('message', function (event) { if(event.data.redirect != null && event.data.redirect.length > 5){ event.data.redirect = event.data.redirect.replace("%%currenturl%%", window.location.href.split(/[?#]/)[0]); window.location.replace(event.data.redirect); } });