File size: 711 Bytes
97f195c
 
550c9d4
 
 
 
 
 
 
 
 
97f195c
 
 
 
 
 
 
550c9d4
97f195c
 
 
550c9d4
 
97f195c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
import debounce from "lodash.debounce";

const debouncedEvent = debounce((eventName, args) => {
  if (window.gtag) {
    console.log('Sending event', eventName, args);
    window.gtag("event", eventName, args);
  }
}, 500)


export function usePageTracking() {
  const location = useLocation();

  useEffect(() => {
    // Send pageview to Google Analytics
    if (window.gtag) {
      const args = {
        page_path: location.pathname + location.search + location.hash,
        page_location: window.location.href,
        page_title: document.title
      }
      debouncedEvent("page_view", args);
    }
  }, [location]);
}