window.tqbUtmTracking = window.tqbUtmTracking || {};
if (!window.tqbUtmTracking.hasInitialized) {
// Define expiration time
window.tqbUtmTracking.EXPIRATION_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
window.tqbUtmTracking.getParamsFromURL = function () {
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"),
};
};
window.tqbUtmTracking.hasTrackingDataExpired = function () {
const timestamp = localStorage.getItem("tracking_data_timestamp");
if (!timestamp) {
return true;
}
const currentTime = new Date().getTime();
return (
currentTime - parseInt(timestamp, 10) >
window.tqbUtmTracking.EXPIRATION_TIME
);
};
window.tqbUtmTracking.hasCampaignChanged = function (currentCampaign) {
const storedCampaign = localStorage.getItem("utm_campaign");
if (!currentCampaign || !storedCampaign) {
return false;
}
return currentCampaign !== storedCampaign;
};
window.tqbUtmTracking.getTrackingParameters = function () {
const params = window.tqbUtmTracking.getParamsFromURL();
const storedCampaign = localStorage.getItem("utm_campaign");
const campaignFromURL = params.utm_campaign;
const campaignChanged =
window.tqbUtmTracking.hasCampaignChanged(campaignFromURL);
const dataExpired = window.tqbUtmTracking.hasTrackingDataExpired();
let utm_medium, utm_source, utm_campaign, fbclid, gclid, msclkid;
if (dataExpired || campaignChanged) {
utm_medium = params.utm_medium || "";
utm_source = params.utm_source || "";
utm_campaign = campaignFromURL;
fbclid = params.fbclid || "";
gclid = params.gclid || "";
msclkid = params.msclkid || "";
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 {
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 { utm_medium, utm_source, utm_campaign, fbclid, gclid, msclkid };
};
window.tqbUtmTracking.getFinalParams = function () {
const params = window.tqbUtmTracking.getTrackingParameters();
let medium = "direct";
let source = "direct";
const socialDomains = [
"facebook.com",
"twitter.com",
"instagram.com",
"linkedin.com",
"pinterest.com",
"tiktok.com",
];
const organicDomains = [
"google.com",
"bing.com",
"yahoo.com",
"baidu.com",
"yandex.com",
"duckduckgo.com",
"ask.com",
];
if (params.utm_medium || params.utm_source) {
medium = params.utm_medium || medium;
source = params.utm_source || source;
}
if (params.gclid) {
medium = "ppc";
source = "google";
} else if (params.fbclid) {
medium = "ppc";
source = "facebook";
} else if (params.msclkid) {
medium = "ppc";
source = "bing";
} else {
const referrer = document.referrer;
if (referrer) {
try {
const referrerUrl = new URL(referrer);
const referrerHostname = referrerUrl.hostname;
if (
socialDomains.some((domain) => referrerHostname.includes(domain))
) {
medium = "social";
source = referrerHostname;
} else if (
organicDomains.some((domain) => referrerHostname.includes(domain))
) {
medium = "organic";
source = referrerHostname;
} else {
const currentUrl = new URL(window.location.href);
if (currentUrl.hostname !== referrerHostname) {
medium = "referral";
source = referrerHostname;
}
}
} catch (e) {
console.error("Invalid referrer URL:", e);
}
} else {
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,
};
};
window.tqbUtmTracking.createIframeTrackingParam = async function () {
const { utm_source, utm_medium, utm_campaign, gclid, fbclid, msclkid } =
window.tqbUtmTracking.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));
const clid = gclid || fbclid || msclkid;
if (clid) {
params.set("utm_clid", clid);
}
return "?" + params.toString();
};
window.tqbUtmTracking.hasInitialized = true;
}
var queryParamsString;
(async function() {
try {
// Await the asynchronous function to create tracking parameters.
queryParamsString = await window.tqbUtmTracking.createIframeTrackingParam();
///////////////////////////////////////////////////////////////////////////
////////////////////////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);
}
});
} catch (error) {
console.error('Error setting up the iframe:', error);
}
})();