1import React from "react";
2import ReactDOM from "react-dom";
3import {
4 BrowserRouter as Router,
5 Switch,
6 useLocation
7} from "react-router-dom";
8
9function usePageViews() {
10 let location = useLocation();
11 React.useEffect(() => {
12 ga.send(["pageview", location.pathname]);
13 }, [location]);
14}
15
16function App() {
17 usePageViews();
18 return <Switch>...</Switch>;
19}
20
21ReactDOM.render(
22 <Router>
23 <App />
24 </Router>,
25 node
26);
27
1import { useLocation } from 'react-router-dom'
2
3// Location is, for example: http://localhost:3000/users/new
4
5// Care! MyComponent must be inside Router to work
6const MyComponent = () => {
7 const location = useLocation()
8
9 // location.pathname is '/users/new'
10 return <span>Path is: {location.pathname}</span>
11}
12
13export default MyComponent