【问题标题】:Google Analytics on React app doesn't workReact 应用程序上的 Google Analytics(分析)不起作用
【发布时间】:2018-08-30 02:26:58
【问题描述】:

我在 github 页面上托管了我的作品集(仍在开发中)。它只是一个具有静态内容的网络应用程序。我需要将 Google Analytics 添加到我的投资组合中并获取访问次数(主要是为了熟悉流程)。我发现 react-ga 模块可用于在 React Apps(使用 create-react-app 创建)上配置 Google Analytics。

并按照this 教程进行配置和托管。我用测试流量检查了该网站,但 Google Analytics(分析)仪表板没有更新。可能是什么情况?它应该按照教程工作。由于我是 Google Analytics 的新手,我很难找出问题所在。

这是我的 App.js

import React, { Component } from "react";
import HeaderList from "./components/Header";
import "./App.css";
import { Layout } from "antd";
import { Router, Route, Switch } from "react-router-dom";
import createHistory from "history/createBrowserHistory";
import ReactGA from 'react-ga';
import Keys from './config/keys';

import AboutMe from "./components/AboutMe";
import Skills from "./components/Skills";
import Contact from "./components/Contact";
import Projects from "./components/Projects";

const { Content } = Layout;
ReactGA.initialize(Keys.GOOGLE_TRACKING_ID); //Unique Google Analytics tracking number

function fireTracking() {
  ReactGA.pageview(window.location.hash);
}

class App extends Component {
  render() {
    return (
      <Router onUpdate={fireTracking} history={createHistory({ basename: process.env.PUBLIC_URL })}>
        <div>
          <Layout>
            <HeaderList />
            <Content className="Content">
              <div className="RouteContent">
                <Switch>
                  <Route exact path="/" component={AboutMe} />
                  <Route path="/skills" component={Skills} />
                  <Route path="/projects" component={Projects} />
                  <Route path="/Contact" component={Contact} />
                </Switch>
              </div>
            </Content>
          </Layout>
        </div>
      </Router>
    );
  }
}

export default App;

【问题讨论】:

  • 你用的是什么版本的 react-router?
  • 第 4 版。 "react-router-dom": "^4.2.2"
  • @ztadic91 你为什么删除你发布的答案?关于这个问题的任何帮助?

标签: reactjs google-analytics react-router create-react-app react-router-dom


【解决方案1】:

React v16.8 或更高版本

可以在useEffect 函数中生成浏览量; withRouter 用于在路由变化时注入 props:

import React, { useEffect }          from 'react';
import { Switch, Route, withRouter } from 'react-router-dom';
import ReactGA                       from 'react-ga';
import Keys                          from './config/keys';

//Unique Google Analytics tracking number
ReactGA.initialize(Keys.GOOGLE_TRACKING_ID); 

const App = () => {

    useEffect( () => {

        // This line will trigger on a route change
        ReactGA.pageview(window.location.pathname + window.location.search); 

    });

    return (/* Code here */);

}

export default withRouter(App);

React v16.8 之前

需要在componentDidMount和componentDidUpdate中生成pageview:

componentDidMount  = () => ReactGA.pageview(window.location.pathname + window.location.search);
componentDidUpdate = () => ReactGA.pageview(window.location.pathname + window.location.search);

所以,最终代码:

import React, { Component } from "react";
import HeaderList from "./components/Header";
import "./App.css";
import { Layout } from "antd";
import { Router, Route, Switch } from "react-router-dom";
import createHistory from "history/createBrowserHistory";
import ReactGA from 'react-ga';
import Keys from './config/keys';

import AboutMe from "./components/AboutMe";
import Skills from "./components/Skills";
import Contact from "./components/Contact";
import Projects from "./components/Projects";

const { Content } = Layout;
ReactGA.initialize(Keys.GOOGLE_TRACKING_ID); //Unique Google Analytics tracking number

class App extends Component {

  componentDidMount  = () => ReactGA.pageview(window.location.pathname + window.location.search);
  componentDidUpdate = () => ReactGA.pageview(window.location.pathname + window.location.search);

  render() {
    return (
      <Router>
        <div>
          <Layout>
            <HeaderList />
            <Content className="Content">
              <div className="RouteContent">
                <Switch>
                  <Route exact path="/" component={AboutMe} />
                  <Route path="/skills" component={Skills} />
                  <Route path="/projects" component={Projects} />
                  <Route path="/Contact" component={Contact} />
                </Switch>
              </div>
            </Content>
          </Layout>
        </div>
      </Router>
    );
  }
}

export default App;

【讨论】:

  • 我们可以将此 GA 代码放在多个组件或特定页面上吗?
  • 如果你把代码放在多个组件上,它会起作用。但是,最好在父组件上编写代码,您不需要在其他组件上复制代码。
【解决方案2】:

如果您有兴趣跟踪页面访问(以及未来的用户行为),我建议使用细分分析库并关注我们的 React quickstart guide 以使用 react-router 库跟踪页面调用。您可以允许&lt;Route /&gt; 组件在页面呈现时进行处理,并使用componentDidMount 调用page 调用。下面的示例显示了您可以执行此操作的一种方法:

const App = () => (
  <div>
    <Switch>
       <Route exact path="/" component={Home} />
       <Route path="/about" component={About} />
    </Switch>
  </div>
);

export default App;
export default class Home extends Component {
  componentDidMount() {
    window.analytics.page('Home');
  }

  render() {
    return (
      <h1>
        Home page.
      </h1>
    );
  }
}

我是https://github.com/segmentio/analytics-react 的维护者。如果您有兴趣尝试多种分析工具(我们支持超过 250 多个目的地),而无需编写任何额外代码,您可以通过 Segment 来打开和关闭不同的目的地。 ?

【讨论】:

    【解决方案3】:

    不幸的是onUpdate was removed in React Router v4,这意味着您的fireTracking 函数永远不会触发。

    在 react-ga 文档 there's an example from Julia Giu 中,该文档已包含在该场景中,该场景涉及将您的应用程序包装在 HoC 中。

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多