栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在React中使用Google Analytics(分析)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在React中使用Google Analytics(分析)?

要使其工作,需要使用

Router
功能。因此在App组件中
import { Router } from 'react-router-dom'
。它必须是路由器而不是BrowserRouter。

然后

import createHistory from 'history/createBrowserHistory'

const history = createHistory()ReactGA.initialize('UA-000000-1');history.listen((location, action) => {    ReactGA.pageview(location.pathname + location.search);    console.log(location.pathname)});

此代码将在每次更改路线时触发!

比给路由器组件一个历史属性。

完整的代码:

import React, { Component } from 'react';import { Router, Route, Switch } from 'react-router-dom';import createHistory from 'history/createBrowserHistory'import Main from './components/pages/Main';import ReactGA from 'react-ga';const history = createHistory()ReactGA.initialize('UA-00000000-1');history.listen((location, action) => {    ReactGA.pageview(location.pathname + location.search);    console.log(location.pathname)});class App extends Component {    render() {        return ( <Router history={history}>     <div className="App">         <Switch>  <Route exact path="/" component={Main} />  <Route exact path="/signup" component={SignupLayout} />  <Route component={PublicLayout} />         </Switch>     </div> </Router>        );    }}export default App;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号