/* SQUEEZELY BASE CODE sqzl-script-base */ (function(s,q,z,l,y){s._sqzl=s._sqzl||[];l=q.createElement('script'), y=q.getElementsByTagName('script')[0];l.async=1;l.type='text/javascript'; l.defer=true;l.src=z;y.parentNode.insertBefore(l,y)}) (window,document,'https://squeezely.tech/tracker/SQ\u002D25652084/sqzl.js'); window._sqzl = window._sqzl || []; window.sqzlLoadLightspeedTries = 0; window.sqzlPurchaseSent = window.sqzlPurchaseSent || false; window.sqzlSearchSent = window.sqzlSearchSent || false; window.sqzlViewCategorySent = window.sqzlViewCategorySent || false; window.sqzlViewContentSent = window.sqzlViewContentSent || false; let jsonUrl = document.location.href; jsonUrl += (jsonUrl.match( /\?/) ? '&' : '?') + 'format=json'; sqzlDeferJqCallback(sqzlInitLightspeed); function sqzlDeferJqCallback(callback) { if(typeof window.jQuery !== 'undefined') { callback(); } else if(window.sqzlLoadLightspeedTries * 50 < 5000) { window.sqzlLoadLightspeedTries++; setTimeout(function() { sqzlDeferJqCallback(callback); }, 50); } } function sqzlInitLightspeed() { let eventData = { event: 'PageView' }; window._sqzl.push(eventData); // Old implementation to check non cart related events sqzlCheckEvents(); $('a').on('click', () => window.setTimeout(sqzlCheckEvents, 1000)); // Handle static cart changes (tracks cart total in cookie and fires AddToCart/RemoveFromCart on changes) sqzlCheckCartChanges(); // Hooks into xhr lightspeed calls to trigger AddToCart/RemoveFromCart events // Re-bind since some external script were removing the event listener (bit ghetto, but oh well) setInterval(() => { $(document).off('ajaxSuccess', sqzlAjaxSuccessHandler); $(document).on('ajaxSuccess', sqzlAjaxSuccessHandler); }, 1000); if($('#gui-form-customer-email2').length > 0) { jQuery('#gui-form-customer-email2').blur(sqzlPrePurchase); } else { jQuery('#gui-form-customer-email').blur(sqzlPrePurchase); } } function sqzlAjaxSuccessHandler(event, xhr, settings) { if(settings.url.includes('cart/add/')) { sqzlHandleCartEvent('AddToCart'); } if(settings.url.includes('cart/delete/')) { sqzlHandleCartEvent('RemoveFromCart'); } } function sqzlCheckCartChanges() { $.getJSON(jsonUrl, function (data) { const cart = data?.page?.cart; if(cart !== undefined) { let cartTotal = 0; let products = []; if(cart !== false) { cartTotal = cart.total.price; products = sqzlFormatCartProducts(cart.products, data.shop.language); } const cartCookieValue = sqzlReadCookie('sqzl_cart_total') ?? 0; const previousCartTotal = parseFloat(cartCookieValue); if(cartTotal > previousCartTotal) { sqzlTriggerCartEvent('AddToCart', products, cartTotal); } else if(cartTotal < previousCartTotal) { sqzlTriggerCartEvent('RemoveFromCart', products, cartTotal); } } }); } function sqzlHandleCartEvent(event) { $.getJSON(jsonUrl, function (data) { const cart = data?.page?.cart; if(cart !== undefined) { let cartTotal = 0; let products = []; if(cart !== false) { cartTotal = cart.total.price; products = sqzlFormatCartProducts(cart.products, data.shop.language); } sqzlTriggerCartEvent(event, products, cartTotal); } }); } function sqzlTriggerCartEvent(event, products, cartTotal, setCart = true) { if(!['AddToCart', 'RemoveFromCart'].includes(event)) { return; } window._sqzl.push({ event: event, products: products, set_cart: setCart, }); sqzlSetCookie('sqzl_cart_total', cartTotal, 7); } function sqzlFormatCartProducts(products, language) { return products.map((product) => { const id = product.sku; return { id: id, name: product.title, price: product.price.price, quantity: product.quantity, language: language.toUpperCase() } }); } function sqzlCheckEvents() { $.getJSON(jsonUrl, function (data) { if (typeof data.order !== 'undefined' && data.template === 'pages/thankyou.rain' && window.sqzlPurchaseSent === false) { window.sqzlPurchaseSent = true; //prevent duplicates eventData = { 'event': 'Purchase', 'email': data.order.customer.email, 'firstname': data.order.customer.firstname, 'lastname': data.order.customer.fulllastname, 'gender': data.order.customer.gender, 'birthdate': data.order.customer.birthdate, 'phone': data.order.customer.phone, 'postcode': data.order.shipping.zipcode, 'city': data.order.shipping.city, 'country': data.order.shipping.country_code, 'totalvalue': data.order.information.price_incl, 'orderid': data.order.information.number, 'currency': data.order.information.currency }; let products = []; if (data.order.products) { for (let i = 0; i < data.order.products.length; i++) { let productId = data.order.products[i].sku; products.push({ "id": productId, "name": data.order.products[i].title, "price": data.order.products[i].price_incl, "quantity": data.order.products[i].quantity }); } } if (products) { eventData.products = products; } window._sqzl.push(eventData); } else if (typeof data.page !== 'undefined' && typeof data.page.search !== 'undefined' && window.sqzlSearchSent === false) { window.sqzlSearchSent = true; //prevent duplicates eventData = { event: 'Search', keyword: data.page.search } window._sqzl.push(eventData); } else if (typeof data.collection !== 'undefined' && typeof data.collection.category_id !== 'undefined' && window.sqzlViewCategorySent === false) { window.sqzlViewCategorySent = true; //prevent duplicates let products = []; let cnt = 0; for(let productId in data.collection.products) { if(data.collection.products.hasOwnProperty(productId) && cnt < 25) { let product = data.collection.products[productId]; let sku = product.sku; products.push({ "id": sku }) } cnt++; } eventData = { event: 'ViewCategory', category_id: data.collection.category_id, objectname: data.collection.title, products: products } window._sqzl.push(eventData); } else if (typeof data.product !== 'undefined' && window.sqzlViewContentSent === false) { window.sqzlViewContentSent = true; //prevent duplicates let lang = data.shop.language.toUpperCase(); let sku = data.product.sku; eventData = { event: 'ViewContent', custom_store_language: lang, products: [{ "id": sku, "name": data.product.title, "price": data.product.price.price, "language": lang }] } window._sqzl.push(eventData); } }); } function sqzlPrePurchase() { //prePurchase Event try { if(jQuery('#gui-form-customer-email').length && sqzlValidateEmail(jQuery('#gui-form-customer-email').val()) && ((jQuery('#gui-form-customer-email').val() == jQuery('#gui-form-customer-email2').val()) || $('#gui-form-customer-email2').length == 0)) { var price = jQuery('div.gui-price strong').html(); var currencySwitch = price.charAt(0); switch(currencySwitch) { case '$': var currency = 'USD'; break; case '£': var currency = 'GBP'; break; default: var currency = 'EUR'; } price = price.replace(/[^0-9,.]/, ''); price = parseFloat(price.replace(/,/, '.')); if(isNaN(price) || (currency !== 'EUR' && currency !== 'USD' && currency !== 'GBP')) { return; } var sqzlTrackData = { 'event' : 'PrePurchase', 'totalvalue' : price, 'currency' : currency, 'firstname' : jQuery('#gui-form-details-firstname').val(), 'lastname' : jQuery('#gui-form-details-lastname').val(), 'postcode' : jQuery('#gui-form-billing_address-zipcode').val(), 'city' : jQuery('#gui-form-billing_address-city').val(), 'country' : jQuery('#gui-form-billing_address-country').val(), 'email' : jQuery('#gui-form-customer-email').val(), 'phone' : jQuery('#gui-form-details-phone').val() }; window._sqzl.push(sqzlTrackData); } } catch(err) { //something went wrong } } function sqzlValidateEmail(email) { var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } function sqzlReadCookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) { c_start = c_start + c_name.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) { c_end = document.cookie.length; } var foundCookie = unescape(document.cookie.substring(c_start, c_end)); if(foundCookie) { //try to save in localStorage if (typeof(Storage) !== "undefined") { localStorage.setItem(c_name, foundCookie); } return foundCookie; } } } if (typeof(Storage) !== "undefined") { localStorageFound = localStorage.getItem(c_name); if(localStorageFound) { //found in localstorage, try to set cookie sqzlSetCookie(c_name, localStorageFound, 10957) return localStorageFound; } else { return sessionStorage.getItem(c_name); } } return ""; } var sqzlSetCookie = function(name, value, days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = name + "=" + value + expires + "; path=/"; if (typeof(Storage) !== "undefined") { //todo: add timestamp for expiration localStorage.setItem(name, value); } }