File size: 568 Bytes
97f195c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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]);
}