【问题标题】:How to get Google Analytics to work with React如何让 Google Analytics 与 React 一起工作
【发布时间】:2020-08-08 10:16:40
【问题描述】:

我正在尝试跟踪每个页面,但每次单击刷新按钮时 Google 只会注册一个视图。当iv'e路由到新路径时不会。有人对如何进行这项工作有任何想法吗?

import React, {useEffect} from 'react';
import { BrowserRouter, Switch, Route } from "react-router-dom";
import OurWork from "./ourWork/ourWork"
import Home from "./home/Home"
import Packages from "./packages/Packages"
import Contacts from "./contact/Contact"
import ReactGA from 'react-ga'
import createHistory from 'history/createBrowserHistory'
const Routes = () => {
    const {
        ContactPage
    } = Contacts();
    const history = createHistory()
    history.listen(location => {
        ReactGA.set({ page: location.pathname });
        ReactGA.pageview(location.pathname);
    });
    useEffect(() => {
         ReactGA.pageview(window.location.pathname + window.location.search)
     }, [history])
    return(
        <BrowserRouter history={history}>
            <Switch>
                <Route path="/" exact component={Home} /> 
                <Route path="/ourwork" exact component={OurWork} /> 
                <Route path="/packages" exact component={Packages} /> 
                <Route path="/Contact" exact component={ContactPage} /> 
            </Switch>
        </BrowserRouter>
    )
}
export default Routes; 

【问题讨论】:

    标签: reactjs google-analytics react-router use-effect


    【解决方案1】:

    看起来与here 描述的问题相同。尝试将&lt;BrowserRouter history={history}&gt; 更改为&lt;Router history={history}&gt; 并从"react-router-dom" 导入Router,如下所示:

    import React, {useEffect} from 'react';
    import { Router, Switch, Route } from "react-router-dom";
    import OurWork from "./ourWork/ourWork"
    import Home from "./home/Home"
    import Packages from "./packages/Packages"
    import Contacts from "./contact/Contact"
    import ReactGA from 'react-ga'
    import createHistory from 'history/createBrowserHistory'
    const Routes = () => {
        const {
            ContactPage
        } = Contacts();
        const history = createHistory()
        history.listen(location => {
            ReactGA.set({ page: location.pathname });
            ReactGA.pageview(location.pathname);
        });
        useEffect(() => {
             ReactGA.pageview(window.location.pathname + window.location.search)
         }, [history])
        return(
            <Router history={history}>
                <Switch>
                    <Route path="/" exact component={Home} /> 
                    <Route path="/ourwork" exact component={OurWork} /> 
                    <Route path="/packages" exact component={Packages} /> 
                    <Route path="/Contact" exact component={ContactPage} /> 
                </Switch>
            </Router>
        )
    }
    export default Routes; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2014-05-31
      • 2019-04-27
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多