File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import { useHistory } from 'react-router-dom';
1212import gtag from '../../lib/gtag' ;
1313import { useThemeEffect } from '../../components/base/hooks/useThemeEffect' ;
1414import BodyTransition from '../../components/base/BodyTransition' ;
15+ import useGoogleAnalyticsUserTracking from '../../lib/hooks/useGoogleAnalyticsUserTracking' ;
1516
1617interface OwnProps { }
1718interface StateProps {
@@ -23,6 +24,7 @@ type CoreProps = OwnProps & StateProps & DispatchProps;
2324const Core : React . FC < CoreProps > = ( { layer } ) => {
2425 useUserLoader ( ) ;
2526 useThemeEffect ( ) ;
27+ useGoogleAnalyticsUserTracking ( ) ;
2628
2729 const history = useHistory ( ) ;
2830
Original file line number Diff line number Diff line change 1+ import { useEffect } from 'react' ;
2+ import useUser from './useUser' ;
3+ import gtag from '../gtag' ;
4+
5+ const GA_MEASUREMENT_ID = 'G-8D0MD2S4PK' ;
6+
7+ /**
8+ * Custom hook to track logged-in users with Google Analytics
9+ * Sets user_id and user_properties when a user is logged in
10+ */
11+ const useGoogleAnalyticsUserTracking = ( ) => {
12+ const currentUser = useUser ( ) ;
13+
14+ useEffect ( ( ) => {
15+ // Only run on client side
16+ if ( typeof window === 'undefined' ) return ;
17+
18+ // Check if user is logged in and gtag is available
19+ if ( currentUser && currentUser . id ) {
20+ gtag ( 'config' , GA_MEASUREMENT_ID , {
21+ user_id : currentUser . id ,
22+ user_properties : {
23+ username : currentUser . username ,
24+ } ,
25+ } ) ;
26+ }
27+ } , [ currentUser ] ) ;
28+ } ;
29+
30+ export default useGoogleAnalyticsUserTracking ;
You can’t perform that action at this time.
0 commit comments