(function() { var isDevEnvLoaded = document.getElementsByTagName('html')[0].innerHTML.indexOf('localhost:4055') > 0 if(window.location.href.indexOf('debugReplayJS') > 0 && !isDevEnvLoaded){ var devScript = document.createElement('script'); devScript.setAttribute('src','http://localhost:4055/replay.js'); document.head.appendChild(devScript); throw('loading dev script') } var cookieEnabled=(navigator.cookieEnabled)? true : false if (typeof navigator.cookieEnabled=='undefined' && !cookieEnabled){ document.cookie='testcookie' cookieEnabled=(document.cookie.indexOf('testcookie')!=-1)? true : false } if (!cookieEnabled) { console.info('Cookies not enabled, exiting better replay') ; return } function isShopifyAdmin(){ if(document.getElementById('admin-bar-iframe') != null) return true; // inside the theme editor if(Shopify && Shopify.designMode) return true; return false } function getUrlVars(url) { var vars = {}; var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; }); return vars; } var __replayScript = document.querySelector('script[src*="/replay.js"]'); var __replayApiKey = getUrlVars(__replayScript.src)['replayApiKey'] var __replaySettings = { proof_ignore_admin_visits: getUrlVars(__replayScript.src)['proof_ignore_admin_visits'] } if(__replayApiKey == ''){ console.info('replay API Key is Blank') ; return } if(__replaySettings.proof_ignore_admin_visits == 'true' && isShopifyAdmin()){ console.info('replay Ignoring Admin Session') ; return } (function(w,d,n,u,o,t,m){w['SrecObject']=o;w[o]=w[o]||function(){ (w[o].q=w[o].q||[]).push(arguments)},w[o].l=1*new Date();t=d.createElement(n), m=d.getElementsByTagName(n)[0];t.async=1;t.src=u;m.parentNode.insertBefore(t,m) })(window,document,'script','https://replayapp.io/collect/initialize.js','replay'); replay('init', __replayApiKey); function isOnRecentThankYouPage() { /* COPIED FROM CART SYNC JS */ /* it starts as /checkouts/.../thank_you when the order is placed */ /* any subsequent calls will auto redirect to /orders/ */ var orders_check = window.location.href.indexOf('/orders/') > 0 // if(orders_check) return false; var checkouts_check = window.location.href.indexOf('/checkouts/') > 0 var thank_you_check = window.location.href.indexOf('/thank_you') > 0 if(!checkouts_check && !thank_you_check && !orders_check) return false; /* this should be available on the thank you page */ if(!window.Shopify.checkout || !window.Shopify.checkout.updated_at) return false; /* if everything checks out, check the updated_at and if its less than 1 min old, we have a match */ var currTime = Number(new Date) var checkoutTime = Number(Date.parse(window.Shopify.checkout.updated_at)) if(((currTime - checkoutTime) / 60000.0) < 1.0) return true; return false } if(isOnRecentThankYouPage()) replay('tag', 'order placed'); function __brIsCookieSet(cname) { var decodedCookie = decodeURIComponent(document.cookie); return (decodedCookie.indexOf(cname) >= 0) } function __brGetCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i { let send = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.send = function() { /// catch it before the response even comes in, this way /// if the page changes there is a higher chance we hit the server with the new tag if(this._url && this._url.includes(urlmatch)){ callback(this); } this.addEventListener('readystatechange', function() { if (this.responseURL.includes(urlmatch) && this.readyState === 4) { callback(this); } }, false); send.apply(this, arguments); }; }; let output = response => { __brPushTag('add to cart') }; intercept('/cart/add.js', output); intercept('/cart/update.js', output); intercept('/cart/change.js', output); intercept('/cart/clear.js', output); //=== 2: on fetch (function(ns, fetch) { if (typeof fetch !== 'function') return; ns.fetch = function() { const response = fetch.apply(this, arguments); response.then(res => { var observers = ['/cart/add', '/cart/update', '/cart/change', '/cart/clear'] var found = false for (var i = 0; i < observers.length; i++) { if(res.url.indexOf(observers[i]) > 0) { found = true } } if (found){ res.clone().json().then(data => __brPushTag('add to cart')); } }); return response; } }(window, window.fetch)) /* send unique sessions to server */ /* var unique_session_cookie = __brGetCookie('replay_unique_visitor'); if (unique_session_cookie != 'set') { __brSetCookie('replay_unique_visitor', 'set', (1/24 / 2)) // new session every 30 min var xhr = new XMLHttpRequest(); xhr.open("POST", "https://api.better-replay.com/static/site_session", true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.send(JSON.stringify({ apiKey: __replayApiKey })); } */ })();