@devgateway/dvz-ui-react
Version:
A modular, embeddable React component library for data visualization and UI, built with TypeScript. Provides reusable components for charts, maps, dashboards, and more, with built-in support for internationalization and Redux integration.
2 lines (1 loc) • 2.62 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("react/jsx-runtime"),o=require("react"),s=require("react-ga4"),R=require("react-router"),b=require("../conf/index.js"),P=require("@devgateway/wp-react-lib"),L=require("./internalTrafficUtils.js"),y=new Set;let l=!1;const k=d=>{const w=C=>{var _;const u=o.useContext(P.SettingsContext)??{},e=((_=u==null?void 0:u.data)==null?void 0:_.google_analytics_code)??b.Config.GA_CODE,c=R.useLocation(),g=o.useRef(null),f=o.useRef(!1),m=o.useRef(!1),i=o.useRef(!1);if(typeof window<"u"&&!i.current&&(i.current=L.isInternalTrafficEnabled(),i.current&&console.log("[GA] Internal traffic detected — GA will NOT be initialized and no events will be sent.")),typeof window<"u"&&!i.current&&!l&&e&&e!=="#REACT_APP_GA_CODE#"){if(!y.has(e))try{s.initialize(e),y.add(e),console.log("GA initialized with code:",e)}catch(t){console.error("Failed to initialize GA:",t)}l=!0}return o.useEffect(()=>{if(typeof window>"u"||i.current||!e||e==="#REACT_APP_GA_CODE#"||!l)return;const t=c.pathname;if(g.current!==t){g.current=t,f.current=!1;try{s.event("page_view",{page_path:t}),console.log("GA pageview sent:",{page:t})}catch(a){console.error("Failed to send pageview:",a)}}},[c.pathname,e]),o.useEffect(()=>{if(typeof window>"u"||i.current||!e||e==="#REACT_APP_GA_CODE#"||!l||m.current)return;const t=()=>{if(f.current)return;const n=Math.round(window.scrollY/(document.documentElement.scrollHeight-window.innerHeight)*100);if(n>10)try{s.event("scroll",{engagement_type:"scroll",scroll_depth:n}),console.log("GA scroll event sent:",{scrollPercentage:n}),f.current=!0}catch(r){console.error("Failed to send scroll event:",r)}},a=n=>{var E;const r=n.target,v=((E=r.textContent)==null?void 0:E.substring(0,50))||"",p=r.id||"",A=r.className||"";if(!(!v&&!p&&!A))try{s.event("element_click",{element_id:p,element_class:A,element_text:v.substring(0,50)})}catch{}},G=Date.now(),h=()=>{const n=Math.round((Date.now()-G)/1e3);if(n>3)try{s.event("time_on_page",{page_path:c.pathname,duration_seconds:n}),console.log("GA time_on_page event sent:",{timeOnPage:n,path:c.pathname})}catch(r){console.error("Failed to send time_on_page event:",r)}};return window.addEventListener("scroll",t,{passive:!0}),document.addEventListener("click",a),window.addEventListener("beforeunload",h),m.current=!0,()=>{window.removeEventListener("scroll",t),document.removeEventListener("click",a),window.removeEventListener("beforeunload",h)}},[c.pathname,e]),T.jsx(d,{...C})};return w.displayName=`withTracker(${d.displayName||d.name||"Component"})`,w};exports.withTracker=k;