@@ -261,46 +261,55 @@ function SearchHotkeyController() {
261261}
262262
263263function IdleGtmLoader ( ) {
264+ const pagePath = useRouterState ( {
265+ select : ( s ) => {
266+ const pathname = s . resolvedLocation ?. pathname || '/'
267+ const search = s . resolvedLocation ?. searchStr || ''
268+
269+ return `${ pathname } ${ search } `
270+ } ,
271+ } )
272+
264273 React . useEffect ( ( ) => {
265274 const gaId = 'G-JMT1Z50SPS'
266275 const existingScript = document . querySelector < HTMLScriptElement > (
267276 `script[src*="googletagmanager.com/gtag/js?id=${ gaId } "]` ,
268277 )
269278
270- if ( existingScript ) return
271-
272- const inject = ( ) => {
273- if ( ! window . dataLayer ) {
274- window . dataLayer = [ ]
275- }
279+ if ( ! window . dataLayer ) {
280+ window . dataLayer = [ ]
281+ }
276282
277- if ( ! window . gtag ) {
278- window . gtag = ( ...args : unknown [ ] ) => {
279- window . dataLayer ?. push ( args )
280- }
283+ if ( ! window . gtag ) {
284+ window . gtag = ( ...args : unknown [ ] ) => {
285+ window . dataLayer ?. push ( args )
281286 }
287+ }
282288
283- window . gtag ( 'js' , new Date ( ) )
284- window . gtag ( 'config' , gaId )
289+ window . gtag ( 'js' , new Date ( ) )
290+ window . gtag ( 'config' , gaId , {
291+ send_page_view : false ,
292+ } )
285293
294+ if ( ! existingScript ) {
286295 const script = document . createElement ( 'script' )
287296 script . async = true
288297 script . src = `https://www.googletagmanager.com/gtag/js?id=${ gaId } `
289298 document . head . appendChild ( script )
290299 }
300+ } , [ ] )
291301
292- if ( 'requestIdleCallback' in window ) {
293- const idleHandle = window . requestIdleCallback ( inject , { timeout : 2500 } )
294- return ( ) => {
295- window . cancelIdleCallback ( idleHandle )
296- }
302+ React . useEffect ( ( ) => {
303+ if ( ! window . gtag ) {
304+ return
297305 }
298306
299- const timeoutHandle = globalThis . setTimeout ( inject , 2500 )
300- return ( ) => {
301- globalThis . clearTimeout ( timeoutHandle )
302- }
303- } , [ ] )
307+ window . gtag ( 'event' , 'page_view' , {
308+ page_title : document . title ,
309+ page_path : pagePath ,
310+ page_location : window . location . href ,
311+ } )
312+ } , [ pagePath ] )
304313
305314 return null
306315}
0 commit comments