Skip to content

Commit ac1d9cd

Browse files
committed
clarity implemented
1 parent 8f260a1 commit ac1d9cd

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/frontend/src/app/AppAuthenticated.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { useHomePageContext } from './HomePageContext';
3434
import { useCurrentOrganization } from '../hooks/organizations.hooks';
3535
import Statistics from '../pages/StatisticsPage/Statistics';
3636
import RetrospectiveGanttChartPage from '../pages/RetrospectivePage/Retrospective';
37+
import { useClarity } from './ClarityProvider';
3738

3839
interface AppAuthenticatedProps {
3940
userId: string;

src/frontend/src/app/AppContextUser.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { v4 as uuidv4 } from 'uuid';
2+
13
/*
24
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
35
* See the LICENSE file in the repository root folder for details.
@@ -7,13 +9,33 @@ import { createContext } from 'react';
79
import { AuthenticatedUser } from 'shared';
810
import LoadingIndicator from '../components/LoadingIndicator';
911
import { useAuth } from '../hooks/auth.hooks';
12+
import { useClarity } from './ClarityProvider';
13+
import { fullNamePipe } from '../utils/pipes';
14+
import { useGetUsersTeams } from '../hooks/teams.hooks';
15+
import ErrorPage from '../pages/ErrorPage';
1016

1117
export const UserContext = createContext<AuthenticatedUser | undefined>(undefined);
1218

1319
const AppContextUser: React.FC = (props) => {
1420
const auth = useAuth();
21+
const clarity = useClarity();
22+
const { data: teams, isLoading: teamsIsLoading, isError: teamsIsError, error: teamsError } = useGetUsersTeams();
23+
24+
if (!auth.user || teamsIsLoading || !teams) return <LoadingIndicator />;
25+
26+
if (teamsIsError) return <ErrorPage message={teamsError.message} />;
1527

16-
if (!auth.user) return <LoadingIndicator />;
28+
if (import.meta.env.VITE_REACT_APP_CLARITY_PROJECT_ID) {
29+
clarity('consent');
30+
clarity('identify', auth.user.email, uuidv4(), undefined, fullNamePipe(auth.user));
31+
clarity('set', 'role', auth.user.role);
32+
clarity('set', 'finishlineUserId', auth.user.userId);
33+
clarity(
34+
'set',
35+
'team',
36+
teams.map((team) => team.teamName)
37+
);
38+
}
1739

1840
return <UserContext.Provider value={auth.user}>{props.children}</UserContext.Provider>;
1941
};

src/frontend/src/app/ClarityProvider.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ declare global {
2222
}
2323

2424
// Your Microsoft Clarity project ID is read from VITE_REACT_APP_CLARITY_PROJECT_ID
25-
const CLARITY_PROJECT_ID = import.meta.env.VITE_REACT_APP_CLARITY_PROJECT_ID as string;
25+
const CLARITY_PROJECT_ID = import.meta.env.VITE_REACT_APP_CLARITY_PROJECT_ID as string | undefined;
2626

2727
// Type for the Clarity function
2828
type ClarityFn = (...args: any[]) => void;
@@ -36,7 +36,13 @@ export const ClarityContext = createContext<ClarityFn | undefined>(undefined);
3636
* Returns the Clarity function from context. Use this to call Clarity API methods.
3737
* Example: const clarity = useClarity();
3838
*/
39-
export const useClarity = () => useContext(ClarityContext);
39+
export const useClarity = () => {
40+
const context = useContext(ClarityContext);
41+
if (context === undefined) {
42+
throw new Error('useClarity must be used within a ClarityProvider');
43+
}
44+
return context;
45+
};
4046

4147
/**
4248
* ClarityProvider component
@@ -47,7 +53,7 @@ export const useClarity = () => useContext(ClarityContext);
4753
const ClarityProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
4854
useEffect(() => {
4955
// Inject the Clarity script only once, if not already present
50-
if (typeof window !== 'undefined' && !window.clarity) {
56+
if (typeof window !== 'undefined' && !window.clarity && CLARITY_PROJECT_ID) {
5157
(function (c: any, l: Document, a: string, r: string, i: string) {
5258
c[a] =
5359
c[a] ||

0 commit comments

Comments
 (0)