1/** 2 * Algolia Request Interceptor (early-load) 3 * 4 * This script MUST load before the Algolia JS client initialises. 5 * It patches both fetch and XMLHttpRequest to capture Algolia search 6 * responses and buffer them for the GA event tracking registry to 7 * process once it loads. 8 * 9 * Buffered responses are stored in window.__gaAlgoliaBuffer and 10 * consumed by registerAlgoliaInterceptor() in ga-event-tracking-registry.js. 11 */ 12(function () { 13 window.__gaAlgoliaBuffer = []; 14 15 function isAlgoliaRequest(url) { 16 return typeof url === "string" && (url.indexOf("algolianet") !== -1 || url.indexOf("algolia.net") !== -1); 17 } 18 19 function getAlgoliaRequestType(url) { 20 if (!isAlgoliaRequest(url)) { 21 return null; 22 } 23 if (url.indexOf("/recommendations") !== -1) { 24 return "recommendation"; 25 } 26 return "search"; 27 } 28 29 // --- Patch fetch --- 30 var originalFetch = window.fetch; 31 window.fetch = function () { 32 var url = arguments[0]; 33 var opts = arguments[1]; 34 var urlStr = typeof url === "string" ? url : (url && url.url ? url.url : ""); 35 36 var requestType = getAlgoliaRequestType(urlStr); 37 if (!requestType) { 38 return originalFetch.apply(this, arguments); 39 } 40 41 var fetchBody = opts && opts.body ? opts.body : null; 42 43 return originalFetch.apply(this, arguments).then(function (response) { 44 var clone = response.clone(); 45 clone.json().then(function (data) { 46 window.__gaAlgoliaBuffer.push({ data: data, body: fetchBody, type: requestType }); 47 }).catch(function () {}); 48 return response; 49 }); 50 }; 51 52 // --- Patch XMLHttpRequest --- 53 var originalXHROpen = XMLHttpRequest.prototype.open; 54 var originalXHRSend = XMLHttpRequest.prototype.send; 55 56 XMLHttpRequest.prototype.open = function () { 57 this._gaUrl = arguments[1] || ""; 58 return originalXHROpen.apply(this, arguments); 59 }; 60 61 XMLHttpRequest.prototype.send = function (body) { 62 var xhr = this; 63 var xhrBody = body; 64 65 var requestType = getAlgoliaRequestType(xhr._gaUrl || ""); 66 if (requestType) { 67 xhr.addEventListener("load", function () { 68 try { 69 var data = JSON.parse(xhr.responseText); 70 window.__gaAlgoliaBuffer.push({ data: data, body: xhrBody, type: requestType }); 71 } catch (e) { 72 // Silently ignore parse errors 73 } 74 }); 75 } 76 77 return originalXHRSend.apply(this, arguments); 78 }; 79})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.