Skip to content

Commit

Permalink
RHOAIENG-9232 Create useTrackUser. (#3080)
Browse files Browse the repository at this point in the history
Addendum from review of PR3024
  • Loading branch information
pilhuhn authored Aug 13, 2024
1 parent 807e556 commit 68047f3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type ODHSegmentKey = {
export type IdentifyEventProperties = {
isAdmin: boolean;
anonymousID?: string;
userId?: string;
canCreateProjects: boolean;
};

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/concepts/analyticsTracking/useSegmentTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export const useSegmentTracking = (): void => {
const username = useAppSelector((state) => state.user);
const clusterID = useAppSelector((state) => state.clusterID);
const [userProps, uPropsLoaded] = useTrackUser(username);
const disableTrackingConfig = dashboardConfig.spec.dashboardConfig.disableTracking;

React.useEffect(() => {
if (segmentKey && loaded && !loadError && username && clusterID && uPropsLoaded) {
window.clusterID = clusterID;
initSegment({
segmentKey,
enabled: !dashboardConfig.spec.dashboardConfig.disableTracking,
enabled: !disableTrackingConfig,
}).then(() => {
fireIdentifyEvent(userProps);
firePageEvent();
Expand All @@ -30,7 +31,7 @@ export const useSegmentTracking = (): void => {
loaded,
segmentKey,
username,
dashboardConfig,
disableTrackingConfig,
userProps,
uPropsLoaded,
]);
Expand Down
18 changes: 7 additions & 11 deletions frontend/src/concepts/analyticsTracking/useTrackUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const useTrackUser = (username?: string): [IdentifyEventProperties, boole
const { isAdmin } = useUser();
const [anonymousId, setAnonymousId] = React.useState<string | undefined>(undefined);

const [loaded, setLoaded] = React.useState(false);
const createReviewResource: AccessReviewResourceAttributes = {
group: 'project.openshift.io',
resource: 'projectrequests',
Expand All @@ -27,15 +26,12 @@ export const useTrackUser = (username?: string): [IdentifyEventProperties, boole
return aId;
};

if (!anonymousId) {
computeAnonymousUserId().then((val) => {
setAnonymousId(val);
});
}
if (acLoaded && anonymousId) {
setLoaded(true);
}
}, [username, anonymousId, acLoaded]);
computeAnonymousUserId().then((val) => {
setAnonymousId(val);
});
// compute anonymousId only once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const props: IdentifyEventProperties = React.useMemo(
() => ({
Expand All @@ -46,5 +42,5 @@ export const useTrackUser = (username?: string): [IdentifyEventProperties, boole
[isAdmin, allowCreate, anonymousId],
);

return [props, loaded];
return [props, acLoaded && !!anonymousId];
};

0 comments on commit 68047f3

Please sign in to comment.