【发布时间】:2019-03-14 10:24:48
【问题描述】:
在我的react 应用程序中,我有一些页面:
- 主要
- 服务
- 联系方式
- 个人资料(私人)
- 等等..
我需要使用 Google Analytics(分析)跟踪用户活动。我用谷歌搜索了react-ga,这很好。但是有了这个库,我必须在我使用的每条路线上初始化我的 GA。例如:
路线“/” - 主页:
class Main extends Component {
componentDidMount() {
initGA();
}
render() {
return (
<div>
<Component1 />
<Component2 />
</div>
)
}
}
我的initGA() 看起来像:
import ReactGA from 'react-ga';
export const initGA = () => {
ReactGA.initialize('UA-00000000-1');
ReactGA.pageview(window.location.pathname + window.location.search);
console.log(window.location.pathname + window.location.search);
}
我的App class 看起来像:
class App extends Component {
render() {
return (
<BrowserRouter>
<div className="App">
<Switch>
<Route exact path="/" component={Main} />
<Route exact path="/signup" component={SignupLayout} />
<Route component={PublicLayout} />
</Switch>
</div>
</BrowserRouter>
);
}
}
在我使用react-ga 的方式中,我在每个呈现路由响应的组件上添加initGA() 函数。我认为在每个组件中重复 initGA() 是不对的。请问各位大佬,react-ga 是怎么用的? react-ga 的正确使用方法是什么?
【问题讨论】:
标签: javascript reactjs google-analytics react-ga