Skip to content

Commit

Permalink
RHOAIENG-9232 Create useTrackUser
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn committed Jul 25, 2024
1 parent d47fa7a commit 0bf2cb5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions frontend/src/concepts/analyticsTracking/segmentIOUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export const fireIdentifyEvent = (properties: IdentifyEventProperties): void =>
const clusterID = window.clusterID ?? '';
if (DEV_MODE) {
/* eslint-disable-next-line no-console */
console.log(`Identify event triggered`);
console.log(`Identify event triggered: ${JSON.stringify(properties)}`);
} else if (window.analytics) {
window.analytics.identify(properties.anonymousID, { clusterID });
window.analytics.identify(properties, { clusterID });
}
};
3 changes: 3 additions & 0 deletions frontend/src/concepts/analyticsTracking/trackingProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ export type ODHSegmentKey = {
};

export type IdentifyEventProperties = {
isAdmin: boolean;
anonymousID?: string;
userId?: string;
canCreateProjects: boolean;
};

export const enum TrackingOutcome {
Expand Down
19 changes: 5 additions & 14 deletions frontend/src/concepts/analyticsTracking/useSegmentTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -10,28 +11,18 @@ export const useSegmentTracking = (): void => {
const { dashboardConfig } = useAppContext();
const username = useAppSelector((state) => state.user);
const clusterID = useAppSelector((state) => state.clusterID);
const userProps = 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('');
};

window.clusterID = clusterID;
initSegment({
segmentKey,
enabled: !dashboardConfig.spec.dashboardConfig.disableTracking,
}).then(() => {
computeUserId().then((userId) => {
fireIdentifyEvent({ anonymousID: userId });
firePageEvent();
});
fireIdentifyEvent(userProps);
firePageEvent();
});
}
}, [clusterID, loadError, loaded, segmentKey, username, dashboardConfig]);
}, [clusterID, loadError, loaded, segmentKey, username, dashboardConfig, userProps]);
};
44 changes: 44 additions & 0 deletions frontend/src/concepts/analyticsTracking/useTrackUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 => {
const { isAdmin } = useUser();
const [anonymousId, setAnonymousId] = React.useState<string | undefined>(undefined);

const createReviewResource: AccessReviewResourceAttributes = {
group: 'project.openshift.io',
resource: 'projectrequests',
verb: 'create',
};
const [allowCreate] = 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));
}
}, [username, anonymousId]);

const props: IdentifyEventProperties = React.useMemo(
() => ({
isAdmin,
canCreateProjects: allowCreate,
anonymousID: anonymousId,
}),
[isAdmin, allowCreate, anonymousId],
);

return props;
};

0 comments on commit 0bf2cb5

Please sign in to comment.