@@ -26,6 +26,13 @@ import UserMenu from './UserMenu/UserMenu';
2626import { useSession } from 'next-auth/react' ;
2727// import { useTheme } from '../context/ThemeContext';
2828import { useState } from 'react' ;
29+ import {
30+ PROD_DEPLOYMENT_ENVIRONMENT ,
31+ PROD_METRICS_WEBSITE_ID ,
32+ QA_DEPLOYMENT_ENVIRONMENT ,
33+ QA_METRICS_WEBSITE_ID ,
34+ UMAMI_METRICS_SCRIPT_SOURCE
35+ } from '../types/const' ;
2936
3037interface IAppLayout {
3138 children : React . ReactNode ;
@@ -46,6 +53,31 @@ const AppLayout: React.FunctionComponent<IAppLayout> = ({ children }) => {
4653 const router = useRouter ( ) ;
4754 const pathname = usePathname ( ) ;
4855
56+ React . useEffect ( ( ) => {
57+ if ( typeof window === 'undefined' ) return ;
58+
59+ const hostname = window . location . hostname ;
60+ const isProd = hostname === PROD_DEPLOYMENT_ENVIRONMENT ;
61+ const isQA = hostname === QA_DEPLOYMENT_ENVIRONMENT ;
62+
63+ const scriptSource = isQA || isProd ? UMAMI_METRICS_SCRIPT_SOURCE : '' ;
64+ const websiteId = isProd ? PROD_METRICS_WEBSITE_ID : isQA ? QA_METRICS_WEBSITE_ID : null ;
65+
66+ if ( scriptSource && websiteId ) {
67+ const script = document . createElement ( 'script' ) ;
68+ script . async = true ;
69+ script . defer = true ;
70+ script . dataset . websiteId = websiteId ;
71+ script . src = scriptSource ;
72+
73+ document . head . appendChild ( script ) ;
74+
75+ return ( ) => {
76+ document . head . removeChild ( script ) ;
77+ } ;
78+ }
79+ } , [ ] ) ;
80+
4981 React . useEffect ( ( ) => {
5082 // Fetch the experimental feature flag
5183 const fetchExperimentalFeature = async ( ) => {
0 commit comments