Skip to content

Commit 56a5f90

Browse files
committed
fix: restore early GA tracking
Load GA from the document head so initial visits are tracked before hydration and keep SPA pageviews from double-counting the first load. Remove the stale Rollup output flag that broke typechecking with the current locked Vite version.
1 parent 460010c commit 56a5f90

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/routes/__root.tsx

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ import { Navbar } from '~/components/Navbar'
3636
import { THEME_COLORS } from '~/utils/utils'
3737
import { 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+
3943
declare 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

vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export default defineConfig({
5959
return id.includes('postgres')
6060
},
6161
output: {
62-
onlyExplicitManualChunks: true,
6362
manualChunks: (id) => {
6463
// Vendor chunk splitting for better caching
6564
if (id.includes('node_modules')) {

0 commit comments

Comments
 (0)