|
| 1 | +import { useEffect, useMemo, useRef } from 'react'; |
| 2 | +import { useLocation } from 'react-router-dom'; |
| 3 | +import { trackCompetitionEvent } from '@/lib/analytics'; |
| 4 | +import { useAuth } from '@/providers/AuthProvider'; |
| 5 | +import { usePageActivityTracking } from '../usePageActivityTracking'; |
| 6 | + |
| 7 | +const competitionPageName = (pathname: string, competitionId: string) => { |
| 8 | + const competitionRoot = `/competitions/${competitionId}`; |
| 9 | + const relativePath = pathname.replace(competitionRoot, '') || '/'; |
| 10 | + |
| 11 | + if (relativePath === '/') { |
| 12 | + return 'groups'; |
| 13 | + } |
| 14 | + |
| 15 | + if (relativePath.startsWith('/activities') || relativePath.startsWith('/rooms')) { |
| 16 | + return 'schedule'; |
| 17 | + } |
| 18 | + |
| 19 | + if (relativePath === '/live' || relativePath === '/admin/remote') { |
| 20 | + return 'live_activities'; |
| 21 | + } |
| 22 | + |
| 23 | + if (relativePath === '/admin/scramblers') { |
| 24 | + return 'assignments'; |
| 25 | + } |
| 26 | + |
| 27 | + return relativePath.replace(/^\//, '').replace(/\//g, '_') || 'competition'; |
| 28 | +}; |
| 29 | + |
| 30 | +const pageViewEventName = (page: string) => { |
| 31 | + if (page === 'groups') { |
| 32 | + return 'groups_viewed'; |
| 33 | + } |
| 34 | + |
| 35 | + if (page === 'schedule') { |
| 36 | + return 'schedule_viewed'; |
| 37 | + } |
| 38 | + |
| 39 | + if (page === 'assignments') { |
| 40 | + return 'assignments_viewed'; |
| 41 | + } |
| 42 | + |
| 43 | + if (page === 'live_activities') { |
| 44 | + return 'live_activities_opened'; |
| 45 | + } |
| 46 | + |
| 47 | + return undefined; |
| 48 | +}; |
| 49 | + |
| 50 | +export const useCompetitionAnalytics = (competitionId?: string) => { |
| 51 | + const location = useLocation(); |
| 52 | + const { user } = useAuth(); |
| 53 | + const lastCompetitionId = useRef<string>(); |
| 54 | + const lastPageEventKey = useRef<string>(); |
| 55 | + |
| 56 | + const page = useMemo( |
| 57 | + () => (competitionId ? competitionPageName(location.pathname, competitionId) : 'competition'), |
| 58 | + [competitionId, location.pathname], |
| 59 | + ); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + if (!competitionId) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (lastCompetitionId.current !== competitionId) { |
| 67 | + trackCompetitionEvent('competition_viewed', { |
| 68 | + competition_id: competitionId, |
| 69 | + page, |
| 70 | + user_id: user?.id, |
| 71 | + }); |
| 72 | + lastCompetitionId.current = competitionId; |
| 73 | + } |
| 74 | + }, [competitionId, page, user?.id]); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + if (!competitionId) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const eventName = pageViewEventName(page); |
| 82 | + if (!eventName) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const eventKey = `${competitionId}:${location.pathname}:${eventName}`; |
| 87 | + if (lastPageEventKey.current === eventKey) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + trackCompetitionEvent(eventName, { |
| 92 | + competition_id: competitionId, |
| 93 | + page, |
| 94 | + feature: page === 'live_activities' ? 'live_activities' : undefined, |
| 95 | + user_id: user?.id, |
| 96 | + }); |
| 97 | + lastPageEventKey.current = eventKey; |
| 98 | + }, [competitionId, location.pathname, page, user?.id]); |
| 99 | + |
| 100 | + usePageActivityTracking({ |
| 101 | + competitionId, |
| 102 | + page, |
| 103 | + userId: user?.id, |
| 104 | + }); |
| 105 | +}; |
0 commit comments