@@ -36,6 +36,10 @@ import { Navbar } from '~/components/Navbar'
3636import { THEME_COLORS } from '~/utils/utils'
3737import { useHubSpotChat } from '~/hooks/useHubSpotChat'
3838
39+ const GOOGLE_ANALYTICS_ID = 'G-JMT1Z50SPS'
40+ const GOOGLE_ANALYTICS_SCRIPT_SRC = `https://www.googletagmanager.com/gtag/js?id=${ GOOGLE_ANALYTICS_ID } `
41+ const GOOGLE_ANALYTICS_BOOTSTRAP = `window.dataLayer = window.dataLayer || [];window.gtag = window.gtag || function(){window.dataLayer.push(arguments);};window.gtag('js', new Date());window.gtag('config', '${ GOOGLE_ANALYTICS_ID } ');`
42+
3943declare global {
4044 interface Window {
4145 dataLayer : unknown [ ] | undefined
@@ -101,6 +105,13 @@ export const Route = createRootRouteWithContext<{
101105 {
102106 children : `(function(){try{var t=localStorage.getItem('theme')||'auto';var v=['light','dark','auto'].includes(t)?t:'auto';if(v==='auto'){var a=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';document.documentElement.classList.add(a,'auto')}else{document.documentElement.classList.add(v)}}catch(e){var a=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';document.documentElement.classList.add(a,'auto')}})()` ,
103107 } ,
108+ {
109+ async : true ,
110+ src : GOOGLE_ANALYTICS_SCRIPT_SRC ,
111+ } ,
112+ {
113+ children : GOOGLE_ANALYTICS_BOOTSTRAP ,
114+ } ,
104115 ] ,
105116 } ) ,
106117 beforeLoad : async ( ctx ) => {
@@ -189,7 +200,7 @@ function ShellComponent({ children }: { children: React.ReactNode }) {
189200 < body className = "overflow-x-hidden" >
190201 < LoginModalProvider >
191202 < ToastProvider >
192- < IdleGtmLoader />
203+ < GoogleAnalyticsTracker />
193204 { hideNavbar ? children : < Navbar > { children } </ Navbar > }
194205 { showDevtools ? (
195206 < LazyRouterDevtools position = "bottom-right" />
@@ -260,7 +271,7 @@ function SearchHotkeyController() {
260271 )
261272}
262273
263- function IdleGtmLoader ( ) {
274+ function GoogleAnalyticsTracker ( ) {
264275 const pagePath = useRouterState ( {
265276 select : ( s ) => {
266277 const pathname = s . resolvedLocation ?. pathname || '/'
@@ -269,38 +280,15 @@ function IdleGtmLoader() {
269280 return `${ pathname } ${ search } `
270281 } ,
271282 } )
283+ const hasTrackedInitialPage = React . useRef ( false )
272284
273285 React . useEffect ( ( ) => {
274- const gaId = 'G-JMT1Z50SPS'
275- const existingScript = document . querySelector < HTMLScriptElement > (
276- `script[src*="googletagmanager.com/gtag/js?id=${ gaId } "]` ,
277- )
278-
279- if ( ! window . dataLayer ) {
280- window . dataLayer = [ ]
281- }
282-
283286 if ( ! window . gtag ) {
284- window . gtag = ( ...args : unknown [ ] ) => {
285- window . dataLayer ?. push ( args )
286- }
287- }
288-
289- window . gtag ( 'js' , new Date ( ) )
290- window . gtag ( 'config' , gaId , {
291- send_page_view : false ,
292- } )
293-
294- if ( ! existingScript ) {
295- const script = document . createElement ( 'script' )
296- script . async = true
297- script . src = `https://www.googletagmanager.com/gtag/js?id=${ gaId } `
298- document . head . appendChild ( script )
287+ return
299288 }
300- } , [ ] )
301289
302- React . useEffect ( ( ) => {
303- if ( ! window . gtag ) {
290+ if ( ! hasTrackedInitialPage . current ) {
291+ hasTrackedInitialPage . current = true
304292 return
305293 }
306294
0 commit comments