@@ -23,7 +23,7 @@ type UaEventOptions = {
2323
2424// Our user's opt-out status can be one of these three values.
2525// Analytics will never be initialized if the user has opted out or if the status is unknown.
26- type IOptOutStatus = "unknown" | " true" | " false"
26+ type IOptOutStatus = "unknown" | true | false
2727
2828// This is the main analytics hook that we use throughout the app.
2929// It handles initializing analytics, sending events, and tracking page views.
@@ -45,7 +45,7 @@ export const useAnalytics = () => {
4545
4646 // Get the user's opt-out status from the config store
4747 const initializeAnalytics = ( ) => {
48- const status = configStore . get ( "analyticsOptOut" )
48+ const status = configStore . get ( "analyticsOptOut" ) as IOptOutStatus
4949
5050 if ( status === "unknown" ) {
5151 console . log ( `[analytics] user has not opted in or out` )
@@ -56,9 +56,9 @@ export const useAnalytics = () => {
5656 } )
5757 } else {
5858 // If the user has opted out, we'll disable analytics
59- setOptedOut ( status as IOptOutStatus )
59+ setOptedOut ( status )
6060 setInitialized ( false )
61- console . log ( `[analytics] user has opted ${ status === "true" ? "out" : "in" } ` )
61+ console . log ( `[analytics] user has opted ${ status ? "out" : "in" } ` )
6262 }
6363 }
6464
@@ -69,8 +69,8 @@ export const useAnalytics = () => {
6969 useEffect ( ( ) => {
7070 const initialize = ( ) => {
7171 const testMode = process . env . NODE_ENV === "test" // we don't want to send analytics events during tests
72- ReactGA . initialize ( GA4_KEY , { testMode : testMode || optedOut === " true" } )
73- optedOut === "false" &&
72+ ReactGA . initialize ( GA4_KEY , { testMode : testMode || optedOut === true } )
73+ ! optedOut &&
7474 ReactGA . set ( {
7575 app_version : packageJson . version ,
7676 app_platform : process . platform ,
@@ -91,25 +91,21 @@ export const useAnalytics = () => {
9191 // https://github.com/codler/react-ga4
9292 const sendAnalyticsEvent = useCallback (
9393 ( event : UaEventOptions ) => {
94- if ( optedOut === "true" ) {
95- // console.log("[analytics] Disabled. Not sending event")
96- return
94+ if ( ! optedOut ) {
95+ console . log ( "[analytics] Sending event" , event )
96+ ReactGA . event ( event )
9797 }
98- console . log ( "[analytics] Sending event" , event )
99- ReactGA . event ( event )
10098 } ,
10199 [ optedOut ]
102100 )
103101
104102 // Send a page view event
105103 const sendPageViewAnalyticsEvent = useCallback (
106104 ( page : string ) => {
107- if ( optedOut === "true" ) {
108- // console.log("[analytics] Disabled. Not sending page view event")
109- return
105+ if ( ! optedOut ) {
106+ console . log ( "[analytics] Sending page view event" , page )
107+ ReactGA . send ( { hitType : "pageview" , page } )
110108 }
111- console . log ( "[analytics] Sending page view event" , page )
112- ReactGA . send ( { hitType : "pageview" , page } )
113109 } ,
114110 [ optedOut ]
115111 )
@@ -232,7 +228,7 @@ const CustomAlert = ({ onClose }) => {
232228 >
233229 < button
234230 onClick = { ( ) => {
235- configStore . set ( "analyticsOptOut" , " true" )
231+ configStore . set ( "analyticsOptOut" , true )
236232 onClose ( )
237233 } }
238234 style = { {
@@ -245,7 +241,7 @@ const CustomAlert = ({ onClose }) => {
245241 </ button >
246242 < button
247243 onClick = { ( ) => {
248- configStore . set ( "analyticsOptOut" , " false" )
244+ configStore . set ( "analyticsOptOut" , false )
249245 onClose ( )
250246 } }
251247 style = { {
0 commit comments