Skip to content

Commit b04e815

Browse files
committed
feat(analytics): add Google Tag Manager and Google Analytics for hosted environments
1 parent 21e5b5c commit b04e815

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

apps/sim/app/layout.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { PostHogProvider } from '@/app/_shell/providers/posthog-provider'
66
import { generateBrandedMetadata, generateThemeCSS } from '@/ee/whitelabeling'
77
import '@/app/_styles/globals.css'
88
import { OneDollarStats } from '@/components/analytics/onedollarstats'
9-
import { isReactGrabEnabled, isReactScanEnabled } from '@/lib/core/config/feature-flags'
9+
import { isHosted, isReactGrabEnabled, isReactScanEnabled } from '@/lib/core/config/feature-flags'
1010
import { HydrationErrorHandler } from '@/app/_shell/hydration-error-handler'
1111
import { QueryProvider } from '@/app/_shell/providers/query-provider'
1212
import { SessionProvider } from '@/app/_shell/providers/session-provider'
@@ -25,6 +25,9 @@ export const viewport: Viewport = {
2525

2626
export const metadata: Metadata = generateBrandedMetadata()
2727

28+
const GTM_ID = 'GTM-T7PHSRX5' as const
29+
const GA_ID = 'G-DR7YBE70VS' as const
30+
2831
export default function RootLayout({ children }: { children: React.ReactNode }) {
2932
const themeCSS = generateThemeCSS()
3033

@@ -208,9 +211,54 @@ export default function RootLayout({ children }: { children: React.ReactNode })
208211
<link rel='dns-prefetch' href='https://assets.onedollarstats.com' />
209212
<script defer src='https://assets.onedollarstats.com/stonks.js' />
210213

214+
{/* Google Tag Manager — hosted only */}
215+
{isHosted && (
216+
<Script
217+
id='gtm'
218+
strategy='afterInteractive'
219+
dangerouslySetInnerHTML={{
220+
__html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
221+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
222+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
223+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
224+
})(window,document,'script','dataLayer','${GTM_ID}');`,
225+
}}
226+
/>
227+
)}
228+
229+
{/* Google Analytics (gtag.js) — hosted only */}
230+
{isHosted && (
231+
<>
232+
<Script
233+
id='gtag-src'
234+
src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}
235+
strategy='afterInteractive'
236+
/>
237+
<Script
238+
id='gtag-init'
239+
strategy='afterInteractive'
240+
dangerouslySetInnerHTML={{
241+
__html: `window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','${GA_ID}');`,
242+
}}
243+
/>
244+
</>
245+
)}
246+
211247
<PublicEnvScript />
212248
</head>
213249
<body className={`${season.variable} font-season`} suppressHydrationWarning>
250+
{/* Google Tag Manager (noscript) — hosted only */}
251+
{isHosted && (
252+
<noscript>
253+
<iframe
254+
src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`}
255+
title='Google Tag Manager'
256+
height='0'
257+
width='0'
258+
className='invisible hidden'
259+
/>
260+
</noscript>
261+
)}
214262
<HydrationErrorHandler />
215263
<OneDollarStats />
216264
<PostHogProvider>

0 commit comments

Comments
 (0)