Add tracker to React Router App
1. Include the UXWizz tracking snippet in your index.html file
2. Track pageviews
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
export default function UXWizzPageview() {
const location = useLocation();
const isInitialLoad = useRef(true);
useEffect(() => {
if (isInitialLoad.current) {
isInitialLoad.current = false
return;
};
if (typeof window === 'undefined') return;
const ust = (window as typeof window & { UST: { trackNewPage: () => void } })['UST'];
if (!ust || !ust.trackNewPage) return;
ust.trackNewPage(); // Call on route change, but not on initial load
}, [location]);
return null;
};Last updated