File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed
Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change 248248 window . dataLayer = window . dataLayer || [ ] ;
249249 function gtag ( ) { dataLayer . push ( arguments ) ; }
250250
251- // Defer GA configuration until after page loads to avoid blocking main thread
252- window . addEventListener ( 'load' , function ( ) {
253- gtag ( 'js' , new Date ( ) ) ;
254- gtag ( 'config' , 'G-Z7XL0RM921' ) ;
251+ // Defer GA configuration with bounce tracking
252+ let gaLoaded = false ;
253+ function loadGA ( ) {
254+ if ( ! gaLoaded ) {
255+ gtag ( 'js' , new Date ( ) ) ;
256+ gtag ( 'config' , 'G-Z7XL0RM921' ) ;
257+ gaLoaded = true ;
258+ }
259+ }
260+
261+ // Load GA on first user interaction
262+ [ 'mousedown' , 'touchstart' , 'keydown' , 'scroll' ] . forEach ( event => {
263+ document . addEventListener ( event , loadGA , { once : true , passive : true } ) ;
255264 } ) ;
265+
266+ // Track bounces: load GA when user is about to leave
267+ window . addEventListener ( 'beforeunload' , loadGA , { once : true } ) ;
268+ document . addEventListener ( 'visibilitychange' , loadGA , { once : true } ) ;
269+
270+ // Fallback: load after 1.5 seconds
271+ setTimeout ( loadGA , 1500 ) ;
256272</ script >
257273< body >
258274 < section class ="header ">
You can’t perform that action at this time.
0 commit comments