-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RHOAIENG-9232 Create useTrackUser #3024
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import React from 'react'; | |
import { useAppContext } from '~/app/AppContext'; | ||
import { useAppSelector } from '~/redux/hooks'; | ||
import { fireIdentifyEvent, firePageEvent } from '~/concepts/analyticsTracking/segmentIOUtils'; | ||
import { useTrackUser } from '~/concepts/analyticsTracking/useTrackUser'; | ||
import { useWatchSegmentKey } from './useWatchSegmentKey'; | ||
import { initSegment } from './initSegment'; | ||
|
||
|
@@ -10,28 +11,27 @@ export const useSegmentTracking = (): void => { | |
const { dashboardConfig } = useAppContext(); | ||
const username = useAppSelector((state) => state.user); | ||
const clusterID = useAppSelector((state) => state.clusterID); | ||
const [userProps, uPropsLoaded] = useTrackUser(username); | ||
|
||
React.useEffect(() => { | ||
if (segmentKey && loaded && !loadError && username && clusterID) { | ||
const computeUserId = async () => { | ||
const anonymousIDBuffer = await crypto.subtle.digest( | ||
'SHA-1', | ||
new TextEncoder().encode(username), | ||
); | ||
const anonymousIDArray = Array.from(new Uint8Array(anonymousIDBuffer)); | ||
return anonymousIDArray.map((b) => b.toString(16).padStart(2, '0')).join(''); | ||
}; | ||
|
||
if (segmentKey && loaded && !loadError && username && clusterID && uPropsLoaded) { | ||
window.clusterID = clusterID; | ||
initSegment({ | ||
segmentKey, | ||
enabled: !dashboardConfig.spec.dashboardConfig.disableTracking, | ||
}).then(() => { | ||
computeUserId().then((userId) => { | ||
fireIdentifyEvent({ anonymousID: userId }); | ||
firePageEvent(); | ||
}); | ||
fireIdentifyEvent(userProps); | ||
pilhuhn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
firePageEvent(); | ||
}); | ||
} | ||
}, [clusterID, loadError, loaded, segmentKey, username, dashboardConfig]); | ||
}, [ | ||
clusterID, | ||
loadError, | ||
loaded, | ||
segmentKey, | ||
username, | ||
dashboardConfig, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
userProps, | ||
uPropsLoaded, | ||
]); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,50 @@ | ||||||||||||||||||||||||||||||||
import React from 'react'; | ||||||||||||||||||||||||||||||||
import { useUser } from '~/redux/selectors'; | ||||||||||||||||||||||||||||||||
import { useAccessReview } from '~/api'; | ||||||||||||||||||||||||||||||||
import { AccessReviewResourceAttributes } from '~/k8sTypes'; | ||||||||||||||||||||||||||||||||
import { IdentifyEventProperties } from '~/concepts/analyticsTracking/trackingProperties'; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
export const useTrackUser = (username?: string): [IdentifyEventProperties, boolean] => { | ||||||||||||||||||||||||||||||||
const { isAdmin } = useUser(); | ||||||||||||||||||||||||||||||||
const [anonymousId, setAnonymousId] = React.useState<string | undefined>(undefined); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const [loaded, setLoaded] = React.useState(false); | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary to track this |
||||||||||||||||||||||||||||||||
const createReviewResource: AccessReviewResourceAttributes = { | ||||||||||||||||||||||||||||||||
group: 'project.openshift.io', | ||||||||||||||||||||||||||||||||
resource: 'projectrequests', | ||||||||||||||||||||||||||||||||
verb: 'create', | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
const [allowCreate, acLoaded] = useAccessReview(createReviewResource); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
React.useEffect(() => { | ||||||||||||||||||||||||||||||||
const computeAnonymousUserId = async () => { | ||||||||||||||||||||||||||||||||
const anonymousIDBuffer = await crypto.subtle.digest( | ||||||||||||||||||||||||||||||||
'SHA-1', | ||||||||||||||||||||||||||||||||
new TextEncoder().encode(username), | ||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||
const anonymousIDArray = Array.from(new Uint8Array(anonymousIDBuffer)); | ||||||||||||||||||||||||||||||||
const aId = anonymousIDArray.map((b) => b.toString(16).padStart(2, '0')).join(''); | ||||||||||||||||||||||||||||||||
return aId; | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (!anonymousId) { | ||||||||||||||||||||||||||||||||
computeAnonymousUserId().then((val) => { | ||||||||||||||||||||||||||||||||
setAnonymousId(val); | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
if (acLoaded && anonymousId) { | ||||||||||||||||||||||||||||||||
setLoaded(true); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
}, [username, anonymousId, acLoaded]); | ||||||||||||||||||||||||||||||||
Comment on lines
+30
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If If you don't care to recompute based on
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The usename is set at login. So I don't think it will ever change during one session. |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const props: IdentifyEventProperties = React.useMemo( | ||||||||||||||||||||||||||||||||
() => ({ | ||||||||||||||||||||||||||||||||
isAdmin, | ||||||||||||||||||||||||||||||||
canCreateProjects: allowCreate, | ||||||||||||||||||||||||||||||||
anonymousID: anonymousId, | ||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||
[isAdmin, allowCreate, anonymousId], | ||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return [props, loaded]; | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userId
isn't used anywhere. Should this be removed? I also thought that we shouldn't include identifiable information in events.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could indeed be removed from this PR. UserId would be the web-user-id for the sandbox case.