Daniel Kantor
add page view events using google analytics
97f195c
raw
history blame
568 Bytes
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
export function usePageTracking() {
const location = useLocation();
useEffect(() => {
// Send pageview to Google Analytics
if (window.gtag) {
console.debug('sending page view event', location.pathname, location.search, location.hash);
window.gtag("event", "page_view", {
page_path: location.pathname + location.search + location.hash,
page_location: window.location.href,
page_title: document.title
});
}
}, [location]);
}