MCPcopy Create free account
hub / github.com/dailydotdev/apps / useLogPageView

Function useLogPageView

packages/shared/src/hooks/log/useLogPageView.ts:6–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4import { useLogContext } from '../../contexts/LogContext';
5
6export default function useLogPageView(): MutableRefObject<() => void> {
7 const router = useRouter();
8 const { logEventStart, logEventEnd } = useLogContext();
9 const routeChangedCallbackRef = useRef<() => void>();
10 const lifecycleCallbackRef = useRef<(event: CustomEvent) => void>();
11
12 useEffect(() => {
13 routeChangedCallbackRef.current = () => {
14 logEventEnd('page view');
15 logEventStart('page view', { event_name: 'page view' });
16 };
17
18 lifecycleCallbackRef.current = (event) => {
19 if (event.detail.newState === 'active') {
20 logEventStart('page view', { event_name: 'page view' });
21 }
22 };
23 }, [logEventStart, logEventEnd]);
24
25 useEffect(() => {
26 const handleRouteChange = () => routeChangedCallbackRef.current();
27 router.events.on('routeChangeComplete', handleRouteChange);
28
29 const handleLifecycle = (event: CustomEvent) =>
30 lifecycleCallbackRef.current(event);
31 window.addEventListener('statechange', handleLifecycle);
32
33 return () => {
34 router.events.off('routeChangeComplete', handleRouteChange);
35 window.removeEventListener('statechange', handleLifecycle);
36 };
37 // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM
38 // eslint-disable-next-line react-hooks/exhaustive-deps
39 }, []);
40
41 return routeChangedCallbackRef;
42}

Callers 3

InternalAppFunction · 0.85
CompanionFunction · 0.85
InternalAppFunction · 0.85

Calls 4

useLogContextFunction · 0.90
logEventEndFunction · 0.85
logEventStartFunction · 0.85
useRouterFunction · 0.50

Tested by

no test coverage detected