const landingTime = new Date(); var xmlhttp = new XMLHttpRequest(); function run_pixel() { const script_version = 'v4' const url2 = 'https://marketing-api3.stage.shutterfly.com/mktg/beacon-pixel/posts' const cookies_logic = [{ 'condition': () => {return SFLY_DTL?.page?.pageInfo?.pageType === "product detail pages"}, 'key': 'is_pip_visited', 'value': 'true', 'exp': 5, }]; // Prepare request xmlhttp.open("POST", url2, true); xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xmlhttp.setRequestHeader("Web-Pixel-Version", 'v3'); xmlhttp.setRequestHeader("Script-Pixel-Version", script_version); xmlhttp.setRequestHeader("Sfly-Web-Domain", document.domain); xmlhttp.setRequestHeader("Sfly-Web-Uri", document.documentURI); xmlhttp.setRequestHeader("Sfly-Web-Title", document.title); let pixel_browsing_uid = "undefined"; if (typeof(Storage) !== "undefined") { pixel_browsing_uid = window.localStorage.getItem("Sfly-Browsing-Uid"); } if (pixel_browsing_uid !== "undefined"){ xmlhttp.setRequestHeader("Sfly-Browsing-Uid", pixel_browsing_uid); } xmlhttp.withCredentials = true; xmlhttp.timeout = 30000; // 30 seconds // Copy Master Tms Udo data if (typeof MasterTmsUdo !== 'undefined' && MasterTmsUdo != null) { pixel_data = JSON.parse(JSON.stringify(MasterTmsUdo)); } else { pixel_data = {}; } // Create postProspectRequest method postProspectRequest = function postProspectReq(data, target, callback) { let prefix_url = ''; if (window.location.href.includes(".dev.")) { prefix_url = 'https://www.dev.shutterfly.com/mktg/'; } else if (window.location.href.includes(".stage.")) { prefix_url = 'https://www.stage.shutterfly.com/mktg/'; } else if (window.location.href.includes(".beta.")) { prefix_url = 'https://www.beta.shutterfly.com/mktg/'; } else if (window.location.href.includes(".kappa.")) { prefix_url = 'https://www.kappa.shutterfly.com/mktg/'; } else /* if prod */ { prefix_url = 'https://sfly-mktg.com/'; // TODO: Shutterfly domain } let httpReq = new XMLHttpRequest(); let url3 = prefix_url + 'posts-prospect'; if (target) { data['target'] = target } if (callback) { httpReq.onreadystatechange = callback; } httpReq.open("POST", url3, true); httpReq.send(JSON.stringify(data)); } // Add SFLY_DTL object if exists try { pixel_data['SFLY_DTL'] = SFLY_DTL.page; } catch (ex_var) { console.warn('beacon-pixel: SFLY_DTL.page not exists.'); } // move skucode object and other objects required for the backend, if they exist try { pixel_data.categoryCode = pixel_data.categoryCode || pixel_data.SFLY_DTL?.productDetails?.categoryId pixel_data.pageType = pixel_data.SFLY_DTL?.pageInfo?.pageType; pixel_data.productId = pixel_data.SFLY_DTL?.productDetails?.productCode; pixel_data.productName = pixel_data.SFLY_DTL?.productDetails?.productName; pixel_data.skuCode = pixel_data.SFLY_DTL?.productDetails?.skuCode; pixel_data.escapedPagename = pixel_data.SFLY_DTL?.pageInfo?.pageName || "unknown page name"; customer_id = SFLY_DTL.session?.visitorDetails?.customerId; if (customer_id) { pixel_data.uid = SFLY_DTL.session?.visitorDetails?.customerId; pixel_data.userId = pixel_data.uid; pixel_data.visitorId = pixel_data.uid; } } catch (ex_var) { console.warn('error copying fields from SFLY_DTL'); } function setCookie(cname, cvalue, exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); let expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";SameSite=None; secure; path=/"; if (typeof(Storage) !== "undefined") { window.localStorage.setItem(cname, cvalue); } } xmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { try { var cookie_name = 'Sfly-Browsing-Uid' let ret_cookie = ''; if (xmlhttp.getAllResponseHeaders().indexOf(cookie_name) >= 0) { ret_cookie = xmlhttp.getResponseHeader(cookie_name); } if (ret_cookie) { setCookie(cookie_name, ret_cookie, 730); for (let i=0;i < cookies_logic.length;i++) { if (cookies_logic[i].condition()) { setCookie(cookies_logic[i].key, cookies_logic[i].value, cookies_logic[i].exp); } } } else { console.warn('beacon-pixel: ' + cookie_name + ' cant be found ' + ret_cookie); } } catch (ex_var) { console.warn('beacon-pixel: Failed to set ' + cookie_name + '.'); } } }; return pixel_data } let iterationCount = 0; let pixel_data = {}; let time_pixel_data = {}; let intervalId = setInterval(function() { pixel_data = run_pixel(); // Check if data has been sent successfully if ((pixel_data['SFLY_DTL'] && pixel_data['SFLY_DTL'].pageInfo && pixel_data['SFLY_DTL'].pageInfo.pageType !== 'other store pages') || iterationCount === 10) { time_pixel_data = { ...pixel_data }; xmlhttp.send(JSON.stringify(pixel_data)); clearInterval(intervalId); } iterationCount++; }, 1000); setTimeout(function () { const now = new Date(); const timeSpentSeconds = Math.round((now - landingTime) / 1000); try { time_pixel_data.time_spent_seconds = timeSpentSeconds; time_pixel_data.start_time = landingTime.toISOString(); time_pixel_data.end_time = now.toISOString(); time_pixel_data.time_event = "True"; } catch (ex_var) { console.warn('error copying fields from SFLY_DTL in time event'); } const timeRequest = new XMLHttpRequest(); timeRequest.open("POST", "https://marketing-api3.stage.shutterfly.com/mktg/beacon-pixel/posts", true); timeRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); timeRequest.setRequestHeader("Web-Pixel-Version", 'v3'); timeRequest.setRequestHeader("Script-Pixel-Version", 'v4'); timeRequest.setRequestHeader("Sfly-Web-Domain", document.domain); timeRequest.setRequestHeader("Sfly-Web-Uri", document.documentURI); timeRequest.setRequestHeader("Sfly-Web-Title", document.title); let pixel_browsing_uid = "undefined"; if (typeof(Storage) !== "undefined") { pixel_browsing_uid = window.localStorage.getItem("Sfly-Browsing-Uid"); } if (pixel_browsing_uid !== "undefined"){ timeRequest.setRequestHeader("Sfly-Browsing-Uid", pixel_browsing_uid); } timeRequest.withCredentials = true; if (time_pixel_data["pageType"] && ["home", "category pages"].includes(time_pixel_data["pageType"]) ) { timeRequest.send(JSON.stringify(time_pixel_data)); } }, 10000); // 10 seconds (function() { let pageType = 'Empty' if (!(typeof pixel_data === 'undefined') && !(typeof pixel_data.pageType === 'undefined')) { pageType = pixel_data.pageType; } if (!(typeof SFLY_DTL === 'undefined') && !(typeof SFLY_DTL.page === 'undefined')) { pageType = SFLY_DTL?.page?.pageInfo?.pageType; } if (pageType === 'Empty') { return } if (!["home", "category pages"].includes(pageType)) return; let eventSent = false; function getScrollPercent() { const scrollTop = window.scrollY; const docHeight = document.body.scrollHeight; return (scrollTop / docHeight) * 100; } function checkScrollDepth() { if (eventSent) return; // Already sent event const scrollPercent = getScrollPercent(); if (scrollPercent >= 50) { eventSent = true; sendScrollEvent(); window.removeEventListener('scroll', checkScrollDepth); } } function sendScrollEvent() { const scroll_pixel_data = { ...pixel_data, scroll_depth_percent: 50, scroll_event: "True" }; const scrollReq = new XMLHttpRequest(); scrollReq.open("POST", "https://marketing-api3.stage.shutterfly.com/mktg/beacon-pixel/posts", true); scrollReq.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); scrollReq.setRequestHeader("Web-Pixel-Version", 'v3'); scrollReq.setRequestHeader("Script-Pixel-Version", 'v4'); scrollReq.setRequestHeader("Sfly-Web-Domain", document.domain); scrollReq.setRequestHeader("Sfly-Web-Uri", document.documentURI); scrollReq.setRequestHeader("Sfly-Web-Title", document.title); let pixel_browsing_uid = "undefined"; if (typeof(Storage) !== "undefined") { pixel_browsing_uid = window.localStorage.getItem("Sfly-Browsing-Uid"); } if (pixel_browsing_uid !== "undefined"){ scrollReq.setRequestHeader("Sfly-Browsing-Uid", pixel_browsing_uid); } scrollReq.withCredentials = true; scrollReq.send(JSON.stringify(scroll_pixel_data)); } window.addEventListener('scroll', checkScrollDepth); })();