function getCookie(cname) { var name = cname + "="; if (document.cookie === undefined || document.cookie === "") { return ""; } var ca = document.cookie.split("; "); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == "") { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function pushFicPersistentIdToDataLayer() { var ficPersistentId = getCookie("fic_persistent_id"); if (ficPersistentId !== "") { dataLayer.push({ ficPersistentId, event: "setFicPersistentIdLanding" }); } } pushFicPersistentIdToDataLayer(); document.addEventListener("DOMContentLoaded", function(event) { var cookieName = 'fic_persistent_id'; var ficConfig = JSON.parse(localStorage.getItem("ficConfig")) ?? {}; var optanonConsentValue = getCookieValue("OptanonConsent"); var cookiePersistent = getCookieValue(cookieName); if(cookiePersistent && (optanonConsentValue === undefined || !optanonConsentValue.includes("groups=C0001%3A1%2CC0004%3A1"))) { clearPersistentIdCookie(cookieName, ficConfig.accessToken); } }); function getCookieValue(name) { var regex = new RegExp(`(^| )${name}=([^;]+)`) var match = document.cookie.match(regex) if (match) { return match[2]; } } function setPersistentIdCookie(persistentId, cookieName, isDevelop) { var cookieString = `${cookieName}=${persistentId}; path=/; domain=.fattureincloud.it; samesite=strict;`; var expire = new Date(); expire.setFullYear(expire.getFullYear() + 10); cookieString += ` expires=${expire.toUTCString()};` if (!isDevelop) { cookieString += " secure;"; } document.cookie = cookieString; } function clearPersistentIdCookie(cookieName, token) { document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.fattureincloud.it;`; } function savePersistentId(persistentId, token) { $.ajax({ url: "https://secure.fattureincloud.it/backend_apiV2/persistent_id", method: "POST", data: JSON.stringify({ persistent_id: persistentId, }), headers: { "Accept-Language": "it", "Content-Type": "application/json; charset=utf-8", "X-Device-Id": "", }, beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", token); }, }); } function generatePersistentId() { var chars = "abcdefghijklmnopqrstuvwxyz0123456789"; var result = ""; for (var i = 40; i > 0; --i) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } // Listener for custom cookie event triggered by GTM window.addEventListener("fic_profiling_cookies", function (event) { var ficConfig = JSON.parse(localStorage.getItem("ficConfig")) ?? {}; var isSupport = !!localStorage.getItem("support"); if (isSupport) { return; } var isDevelop = 0; var cookieName = 'fic_persistent_id'; if (event.detail.accepted) { var persistentIdFromURL = ""; if (!!ficConfig.accessToken) { // User logged $.ajax({ url: "https://secure.fattureincloud.it/backend_apiV2/persistent_id", method: "GET", headers: { "Accept-Language": "it", "Content-Type": "application/json; charset=utf-8", "X-Device-Id": "", }, beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", ficConfig.accessToken); }, success: function (response) { if (response.data.persistent_id) { setPersistentIdCookie(response.data.persistent_id, cookieName, isDevelop); } else if (persistentIdFromURL) { setPersistentIdCookie(persistentIdFromURL, cookieName, isDevelop); savePersistentId(persistentIdFromURL, ficConfig.accessToken); } else if (document.cookie.includes(cookieName)) { var cookie = document.cookie.split(";").find(cookie => cookie.trim().startsWith(cookieName + "=")); var cookieValue = cookie.split("=")[1]; savePersistentId(cookieValue, ficConfig.accessToken); } else { var persistentId = generatePersistentId(); setPersistentIdCookie(persistentId, cookieName, isDevelop); savePersistentId(persistentId, ficConfig.accessToken); } }, }); } else { // Not Logged if (persistentIdFromURL) { setPersistentIdCookie(persistentIdFromURL, cookieName, isDevelop); } else if (!document.cookie.includes(cookieName)) { var persistentId = generatePersistentId(); setPersistentIdCookie(persistentId, cookieName, isDevelop); } } } else { clearPersistentIdCookie(cookieName, ficConfig.accessToken); } });