false ) ); wp_enqueue_script( 'nsc-tppm-interceptor' ); $helper = new Helper(); $auth_hash = $helper->get_auth_hash(); $proxy_endpoint = esc_url( get_site_url() . '/wp-json/nsc-tppm/v1/intercept' ); // Get the sample percentage (e.g., 10 means intercept on 10% of page views). $sample_percentage = intval( get_option( 'nsc3ppm_sample_percentage', 100 ) ); $js = '!function(){"use strict";var e=' . $sample_percentage . ',t=100*Math.random();if(t>e)return void console.log("🏄‍♀️3rd Party Request Manager Plugin: Not sampling this page view ("+t.toFixed(2)+"% > "+e+"%).");const n="' . $proxy_endpoint . '";let o=[];function r(e){try{return new URL(e,window.location.href).origin!==window.location.origin}catch(e){return!1}}function i(e){return 0===e.indexOf(n)}function c(e){var t;(t=e.originalUrl).startsWith("chrome-extension://")||t.startsWith("moz-extension://")||t.startsWith("safari-extension://")||e.originalUrl&&r(e.originalUrl)&&!i(e.originalUrl)&&(e.pageUrl=window.location.href,o.push(e))}function a(){if(o.length>0){const e=o.slice();o=[],window.__originalFetch(n,{method:"POST",headers:{"Content-Type":"application/json","X-NSCPLE-API-KEY":"' . $auth_hash . '"},body:JSON.stringify(e),keepalive:!0}).catch((function(e){console.error("Error sending batch to proxy:",e)}))}}window.__originalFetch=window.fetch.bind(window),setInterval(a,1e4),window.addEventListener("beforeunload",a),window.addEventListener("pagehide",a);const s=window.fetch;window.fetch=function(e,t){let n;if("object"==typeof e&&e.url?n=e.url:"string"==typeof e&&(n=e),n&&r(n)&&!i(n)){c({originalUrl:n,method:t&&t.method?t.method:"GET",type:"fetch"})}return s(e,t)};const d=window.XMLHttpRequest;if(window.XMLHttpRequest=function(){const e=new d;let t="",n="";const o=e.open;e.open=function(r,i,c,a,s){return t=i,n=r,o.apply(e,arguments)};const a=e.send;return e.send=function(o){return t&&r(t)&&!i(t)&&c({originalUrl:t,method:n,type:"xhr",body:o}),a.apply(e,arguments)},e},navigator.sendBeacon){const e=navigator.sendBeacon.bind(navigator);navigator.sendBeacon=function(t,n){return t&&r(t)&&!i(t)&&c({originalUrl:t,method:"BEACON",type:"beacon",body:n}),e(t,n)}}function l(e){var t=e.getAttribute("src")||e.getAttribute("href");t&&r(t)&&!i(t)&&"a"!==e.tagName.toLowerCase()&&c({originalUrl:t,type:e.tagName.toLowerCase()})}document.addEventListener("DOMContentLoaded",(function(){document.querySelectorAll("script[src], img[src], link[href], video[src], audio[src], iframe[src], embed[src], source[src], track[src]").forEach(l)}));new MutationObserver((function(e){e.forEach((function(e){e.addedNodes.forEach((function(e){1===e.nodeType&&((e.getAttribute("src")||e.getAttribute("href"))&&l(e),e.querySelectorAll&&e.querySelectorAll("[src], link[href]").forEach(l))}))}))})).observe(document.documentElement,{childList:!0,subtree:!0})}();'; wp_add_inline_script( 'nsc-tppm-interceptor', $js ); } }