!function(){"use strict";const e="https://tagapi.delvify.ai",t=`${e}/api/merchant`,a=`${e}/api/engagement`,n=`${e}/api/order`,i=`${e}/api/orderSummary`,o=async e=>fetch(e).then((e=>e.json())),s=async(e,t)=>fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((e=>e.json()));const c=(e=0)=>`${Math.floor(18082021*Math.random())}.${new Date(Date.now()+e).getTime()}`,l=(e,t)=>localStorage.setItem(e,JSON.stringify(t)),r=e=>JSON.parse(localStorage.getItem(e)),d=e=>localStorage.removeItem(e);const u="IS_VISUAL_SEARCH",m="CLICK_DEFAULT_ADD_TO_CART",p="CLICK_VISUAL_SEARCH_ADD_TO_CART",y="PRODUCT_PAGE";var _=e=>{log(`${e.event||e.event_type}`),(({oid:e,sku:t,price:a,type:i,page_type:o,quantity:c,event_type:l=null,query:r=null})=>s(`${n}`,{oid:e,sku:t,price:a,type:i,page_type:o,quantity:c||1,event_type:l,query:r,sid:null,currency_code:"GBP"}))({oid:tagData.sId,sku:e.product_id||e.sku,price:e.price,type:e.event,page_type:e.location,quantity:e.quantity,event_type:e.event_type,query:e.query}).then((e=>{log("Recorded.")}))};const f=e=>e.toLowerCase().replace("&","&");var g=()=>{(()=>{let e;function t(){log("Session Expired"),sessionStorage.removeItem("sId")}function a(){clearTimeout(e),e=setTimeout(t,18e5)}window.onload=a,document.onmousemove=a,document.onkeydown=a})(),window.delvify_data=r("delvify")||{previous:null,current:null,buffer_event:null},delvify_data.previous=delvify_data.current,delvify_data.current=location.pathname+location.search,delvify_data.current.includes("/products/")||(delvify_data.buffer_event=null),(()=>{if(delvify_data.current.includes("/thank_you")&&delvify_data.current.includes("/checkouts")){const e=r("delvify_purchase");let t=c();try{t=document.getElementsByClassName("os-order-number")[0].innerText.replace("Confirmation # ","").trim()}catch{console.error("Error capturing order number")}e&&(log("PURCHASE"),(({sid:e,sub_total:t,discount_code:a,discount_amount:n,shipping_fee:o,tax_fee:c,total:l,products:r})=>s(`${i}/create`,{sid:e,sub_total:t,discount_code:a,discount_amount:n,shipping_fee:o,tax_fee:c,total:l,products:r}))({sid:t,shipping_fee:e.shipping_fee,tax_fee:0,sub_total:e.subTotal,total:e.total,products:e.products,discount_amount:e.discount_amount,discount_code:e.discount_code}).then((e=>{log("Recorded.")})).catch((e=>{error("Fail to record.")})).finally((e=>{d("delvify_purchase")})))}})(),l("delvify",delvify_data),window.addEventListener("click",(e=>{const t=e.composedPath();((e,t)=>{"INPUT"===e[0].nodeName&&"crop-file"===e[0].id||"delvify-camera"!==e[0].className||"IMG"!==e[0].nodeName||_({event_type:"VISUAL_SEARCH"})})(t),((e,t)=>{const a="DIV"===e[0].nodeName&&e[0].className.includes("hover")&&"A"===e[1].nodeName&&e[2].className.includes("product-image")&&"DIV"===e[2].nodeName,n="A"===e[0].nodeName&&"DIV"===e[1].nodeName&&e[1].className.includes("chesca-button-cont"),i="IMG"===e[0].nodeName&&"A"===e[1].nodeName&&"DIV"===e[2].nodeName&&e[2].className.includes("dv-product-image"),o="DIV"===e[0].nodeName&&"A"===e[1].nodeName&&!e[1].className.includes("cs-item")&&"DIV"===e[2].nodeName&&e[0].className.includes("dv-product-name");if(a||n||i||o){let t="CLICK",s="HOME_PAGE";delvify_data.current.includes("q=")?s="SEARCH_PAGE_RESULTS":delvify_data.current.includes("/products/")&&(s=y),delvify_data.buffer_event=null;let c,d=null;if(a)d=e[3].innerHTML.match(/((?<=filter-product-item-regular-price">\S)|(?<=filter-product-item-sale-price">\S))([\d.]+?)((?=<\/span><\/p><\/div>)|(?=<\/span>))/g)[0],c=e[3].innerHTML.match(/(?<=alt=")([\s\S]+?)(?=">)/g);else if(n)d=e[2].innerHTML.match(/((?<=filter-product-item-regular-price">\S)|(?<=filter-product-item-sale-price">\S))([\d.]+?)((?=<\/span><\/p><\/div>)|(?=<\/span>))/g)[0],c=e[2].innerHTML.match(/(?<=alt=")([\s\S]+?)(?=">)/g);else if(i){d=e[3].innerHTML.match(/([\d.]+)(?=<\/div>)/g),c=e[3].innerHTML.match(/(?<=\s">)([\s\S.]+)(?=<\/div>\s+<\/a>)/g),t=u;let a=r("visualDelvify")||"";a=a.concat(",",f(c[0])),l("visualDelvify",a)}else if(o){d=e[2].innerHTML.match(/([\d.]+)(?=<\/div>)/g),c=e[2].innerHTML.match(/(?<=\s">)([\s\S.]+)(?=<\/div>\s+<\/a>)/g),t=u;let a=r("visualDelvify")||"";a=a.concat(",",f(c[0])),l("visualDelvify",a)}c&&(c=f(c[0])),delvify_data.buffer_event=t,l("delvify",delvify_data),_({event:t,product_id:c,price:d,location:s})}})(t),(e=>{let t=null,a=0;const n="BUTTON"===e[0].nodeName&&!e[0].innerText.includes("Sold")&&e[0].className.includes("product-form__submit button button--full-width aaa button--secondary");if(n){let i=m,o=1;"IS_TEXT_SEARCH"===delvify_data.buffer_event&&(i="CLICK_TEXT_SEARCH_ADD_TO_CART"),delvify_data.buffer_event===u&&(i=p),a=e[6].innerHTML.match(/(?<=)([\S\s]+?)(?=<\/span>)/)[0].trim().match(/([\d.]+)/)[0],n&&null===t&&(t=e[6].innerHTML.match(/(?<=

)([\S\s]+?)(?=<\/h1>)/)[0].trim()),t=f(t),_({event:i,product_id:t,price:a,quantity:o,location:y})}})(t),((e,t)=>{if("btn"===e[0].className&&"INPUT"===e[0].nodeName&&e[1].className.includes("cart-row")&&"DIV"===e[1].nodeName){let t="GBP";const a=e[3].innerHTML;let n=0;const i=a.match(/(?<=
  • )([\s\S]+?)(?=<\/li>)/g),o=[];i&&i.forEach((e=>{const a=f(e.match(/(?<=a href)([\s\S]+?)(?=<\/a>)/)[0]).match(/(?<=">)([\s\S]+)/g)[0].trim(),n=e.match(/(?<=" value=")([\d]+)/g)[0];let i=e.match(/(?<=><\/div>)([\s\S]+?)(?=
    \S)([\s\S.]+?)(?=<\/span><\/em>)/);u&&(u=u[0]),l("delvify_purchase",{shipping_fee:n,subTotal:u,total:u,products:o,discount_code:s,discount_amount:c});let g=r("visualDelvify")||"";const v=[];o.forEach((e=>{e.event="CHECKOUT_CLICKED",e.location="CART_PAGE",v.push(_({event:g.includes(e.sku)?p:m,product_id:e.sku,price:e.price,quantity:e.quantity,location:y})),v.push(_(e))})),Promise.all(v).then((()=>{log("All Recorded."),d("visualDelvify")}))}})(t)}))};window.testKey="_T$"===localStorage.getItem("_t"),location.pathname,location.search;const v=window.testKey?"Testing v 1.0.1 Chesca":"Delvify v 1.0.1 Chesca";window.log=e=>{console.log(`[${v}] ${e}`)},window.error=e=>{console.error(`[${v}] ${e}`)},testKey?console.log("[Delvify v 1.0.1 Chesca] Script By-passed"):(async()=>{window.tagData={},window.apiKey="",window.skipSteps=sessionStorage.getItem("sId")||null,window.lockEvent=!1,await new Promise((e=>{log("Initializing session...");let t=sessionStorage.getItem("sId"),a=localStorage.getItem("uId");t||(t=c(),sessionStorage.setItem("sId",t),window.skipSteps=null,d("visualDelvify")),a||(a=c(),localStorage.setItem("uId",a)),tagData.sId=t,tagData.uId=a,e()})),skipSteps||(await new Promise((async(e,a)=>{log("Authorizing...");let n="delvify-tracking";testKey&&(n="delvify-tag-testing");const i=document.getElementById(n);i||a("ID delvify-tag is not declared");const s=i.getAttribute("api_key");s||a("API Key is not exist"),window.apiKey=s;const c=(await o(`${t}/getMerchantId?api_key=${s}`)).merchantId;c||a("No Merchant ID found"),tagData.merchantId=c,e()})),await(async()=>{log("Initialize engagement...");let e="";try{await o("https://reallyfreegeoip.org/json/").then((t=>{t&&(e=t)}))}catch(e){error("Cannot fetch location")}const t=e?e.city:null,n=e?e.country_code:null,i=(()=>{let e=navigator.userAgent.toLowerCase(),t=function(t){return void 0===t?t=e:e=t.toLowerCase(),/(ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP))))/.test(e)?"TABLET":/(mobi|ipod|phone|blackberry|opera mini|fennec|minimo|symbian|psp|nintendo ds|archos|skyfire|puffin|blazer|bolt|gobrowser|iris|maemo|semc|teashark|uzard)/.test(e)?"MOBILE":"DESKTOP"};return{device:t(),detect:t,isMobile:"desktop"!==t(),userAgent:e}})().device;await s(`${a}`,{oid:tagData.sId,customer_id:tagData.uId,country_code:n,city:t,merchant_id:tagData.merchantId,device:i,source:tagData.source})})())})().then((e=>{log("Initialization Success"),g()})).catch((e=>{error("Something went wrong!"),error(e)}))}();