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 () { var ficConfig = JSON.parse(localStorage.getItem("ficConfig")) ?? {}; var optanonConsentValue = getCookieValue("OptanonConsent"); handleCookieCleanup("persistent_id", ficConfig, optanonConsentValue); handleCookieCleanup("tncid", ficConfig, optanonConsentValue); }); function getCookieValue(name) { var regex = new RegExp(`(^| )${name}=([^;]+)`); var match = document.cookie.match(regex); if (match) { return match[2]; } } function setCookie(value, cookieName, isDevelop) { var cookieString = `${cookieName}=${value}; 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 clearCookie(cookieName) { document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.fattureincloud.it;`; } function generateId() { var chars = "abcdefghijklmnopqrstuvwxyz0123456789"; var result = ""; for (var i = 40; i > 0; --i) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } function saveId(endpoint, payload, token) { $.ajax({ url: `https://secure.fattureincloud.it/backend_apiV2/${endpoint}`, method: "POST", data: JSON.stringify(payload), headers: { "Accept-Language": "it", "Content-Type": "application/json; charset=utf-8", "X-Device-Id": "", }, beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", token); }, }); } function handleCookieCleanup(cookieName, ficConfig, optanonConsentValue) { var cookiePersistent = getCookieValue(cookieName); if ( cookiePersistent && (optanonConsentValue === undefined || !optanonConsentValue.includes("groups=C0001%3A1%2CC0004%3A1")) ) { clearCookie(cookieName); } } function handleTrackingId(config) { var ficConfig = JSON.parse(localStorage.getItem("ficConfig")) ?? {}; var isSupport = !!localStorage.getItem("support"); if (isSupport) { return; } var isDevelop = config.isDevelop; var cookieName = config.cookieName; var endpoint = config.endpoint; var includeTimestamp = config.includeTimestamp; var cookieValue = getCookieValue(cookieName); if (!cookieValue) { cookieValue = generateId(); setCookie(cookieValue, cookieName, isDevelop); if (ficConfig.accessToken) { var payload = { [cookieName]: cookieValue }; if (includeTimestamp) { payload.attributed_at = new Date().toISOString(); } saveId(endpoint, payload, ficConfig.accessToken); } } } window.addEventListener("fic_profiling_cookies", function (event) { if (!event.detail.accepted) { clearCookie("persistent_id"); clearCookie("tncid"); return; } var isDevelop = 0; handleTrackingId({ cookieName: "persistent_id", endpoint: "persistent_id", isDevelop: isDevelop, includeTimestamp: true }); handleTrackingId({ cookieName: "tncid", endpoint: "tncid", isDevelop: isDevelop, includeTimestamp: true }); });