Javascript 如何将Google Analytics与react router v5和StaticRouter结合使用?

Javascript 如何将Google Analytics与react router v5和StaticRouter结合使用?,javascript,reactjs,google-analytics,Javascript,Reactjs,Google Analytics,我正在尝试在我的react SSR应用程序中添加带有react router v5 StaticRouter的Google Analytics 如中所述。我知道createBrowserHistory不起作用,所以我想我会使用createMemoryHistory。似乎删除了v4中的createMemoryHistory?我会试着听路线的变化,但不知道如何用SSR来做?或者我可以写一个HOC并修复它。但这似乎是一种更干净更简单的方法 现在要实现这样的目标,我该怎么做 import ReactGA

我正在尝试在我的react SSR应用程序中添加带有react router v5 StaticRouter的Google Analytics 如中所述。我知道createBrowserHistory不起作用,所以我想我会使用createMemoryHistory。似乎删除了v4中的createMemoryHistory?我会试着听路线的变化,但不知道如何用SSR来做?或者我可以写一个HOC并修复它。但这似乎是一种更干净更简单的方法

现在要实现这样的目标,我该怎么做

import ReactGA from 'react-ga';
import { createMemoryHistory } from 'history';

ReactGA.initialize('UA-*********-1');


const history = createMemoryHistory();
history.listen(location => {
  console.log('location', location);
  ReactGA.set({ page: location.pathname });
  ReactGA.pageview(location.pathname);
});

function NotFound() {
  return <h1>Oops, nothing here!</h1>;
}

function App() {
  return (
      <Switch>
        {routes.map(({ path, component, isPublic, exact }) => (
          isPublic ?
            <Route
              key={path}
              exact={exact}
              path={path}
              component={component}
              history={history}
            />
            :
            <ProtectedRoute
              isLoggedIn={user}
              key={path}
              path={path}
              component={component}
              exact={exact}
              public={false}
              history={history}
            />
        ))}
        <Route component={NotFound} />
      </Switch>)
  );
}
从“react ga”导入ReactGA;
从“历史”导入{createMemoryHistory};
ReactGA.initialize('UA-*********-1');
const history=createMemoryHistory();
历史。听(位置=>{
console.log('location',location);
set({page:location.pathname});
ReactGA.pageview(location.pathname);
});
函数NotFound(){
返回哎呀,这里什么都没有!;
}
函数App(){
返回(
{routes.map({path,component,isPublic,exact})=>(
是公共的吗?
:
))}
)
);
}