1const observers = new Map(); 2 3function trackImpressions( 4 { 5 selector, 6 endpoint, 7 idKey, 8 sentAttr = 'data-is-send-impressions', 9 observerOptions = { rootMargin: '0px', threshold: 0.5 }, 10 }, 11) { 12 const elements = document.querySelectorAll(selector); 13 if (!elements.length) return; 14 15 let io = observers.get(selector); 16 17 if (!io) { 18 io = new IntersectionObserver((entries, obs) => { 19 entries.forEach(({ isIntersecting, target }) => { 20 if (!isIntersecting) return; 21 22 const element = target; 23 if (element.hasAttribute(sentAttr)) { 24 obs.unobserve(element); 25 return; 26 } 27 28 const { id, source, token } = element.dataset || {}; 29 if (!id) { 30 obs.unobserve(element); 31 return; 32 } 33 34 iu.ajax(null, endpoint, { [idKey]: id, source, token }); 35 element.setAttribute(sentAttr, ''); 36 obs.unobserve(element); 37 }); 38 }, observerOptions); 39 40 observers.set(selector, io); 41 } 42 43 elements.forEach((el) => { 44 if (el.hasAttribute(sentAttr)) return; 45 io.observe(el); 46 }); 47} 48 49window.sendWorksheetShowing = function () { 50 trackImpressions({ 51 selector: '[data-track-impression]', 52 endpoint: '/worksheet/worksheet/addWorksheetShowing', 53 idKey: 'worksheetId', 54 }); 55}; 56 57window.sendLibraryDocumentShowing = function () { 58 trackImpressions({ 59 selector: '[data-track-document-impression]', 60 endpoint: '/sellable-items/document/showing-event', 61 idKey: 'entityId', 62 }); 63}; 64 65const trackInitialImpressions = () => { 66 window.sendWorksheetShowing(); 67 window.sendLibraryDocumentShowing(); 68}; 69 70if (document.readyState === 'loading') { 71 document.addEventListener('DOMContentLoaded', trackInitialImpressions, { once: true }); 72} else { 73 trackInitialImpressions(); 74} 75 76document.addEventListener('turbo:load', () => { 77 observers.forEach((io) => io.disconnect()); 78 observers.clear(); 79 80 window.sendWorksheetShowing(); 81 window.sendLibraryDocumentShowing(); 82});
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.