Skip to content

Commit bb47eee

Browse files
committed
feat: track user logged in
1 parent 8cf9ab2 commit bb47eee

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/containers/base/Core.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useHistory } from 'react-router-dom';
1212
import gtag from '../../lib/gtag';
1313
import { useThemeEffect } from '../../components/base/hooks/useThemeEffect';
1414
import BodyTransition from '../../components/base/BodyTransition';
15+
import useGoogleAnalyticsUserTracking from '../../lib/hooks/useGoogleAnalyticsUserTracking';
1516

1617
interface OwnProps {}
1718
interface StateProps {
@@ -23,6 +24,7 @@ type CoreProps = OwnProps & StateProps & DispatchProps;
2324
const Core: React.FC<CoreProps> = ({ layer }) => {
2425
useUserLoader();
2526
useThemeEffect();
27+
useGoogleAnalyticsUserTracking();
2628

2729
const history = useHistory();
2830

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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;

0 commit comments

Comments
 (0)