0

我正在使用重定向转到我网站的最后一页。在这个重定向中,我向它提供了一堆 url 参数,这些参数告诉我用户在那之前做了什么。

问题是,当我添加 Google Analytics 时,它没有接收到我希望能够收集数据的任何参数。这是 index.js 中的代码:

import Analytics from 'react-router-ga';
import ReactGA from 'react-ga';

ReactGA.initialize('UA-11111111111-1');
ReactGA.pageview(window.location.pathname + window.location.search);

const allGiftsPage = () => { return ( <Results quizType="allgifts" title="All Gifts" /> )}
const popularGiftsPage = () => { return ( <Results quizType="populargifts" title="Popular Gifts" /> )}
const quizResultsPage = () => { return ( <Results title="Results" /> )}

ReactDOM.render(
  <Fragment>
    <title>Find Their Gifts</title>
    <BrowserRouter>
      <Analytics id='UA-111111111-1'>
        <Route exact path="/" component={App}></Route>
        <Route path="/results" component={quizResultsPage}></Route>
        <Route path="/allgifts" component={allGiftsPage}></Route>
        <Route path="/populargifts" component={popularGiftsPage}></Route>
      </Analytics>
    </BrowserRouter>
  </Fragment>
  , document.getElementById('root')
);

这是调用重定向的代码:

<Redirect to={`/results?`+
  `quizType=romanticquiz` +
  `&age=${this.state.age}`+
  `&gender=${this.state.gender}`+
  `&color=${this.state.color}`+
  `&vacation=${this.state.vacation}`+
  `&word=${this.state.word}`+
  `&prison=${this.state.prison}`
}

在 Google Analytics 中,使用所有这些参数访问此页面的用户只会显示“/results”,但如果您在结果页面上刷新,您将获得包含所有参数的完整 URL。

有没有一种好方法可以让我在不修改大部分代码的情况下让这个功能正常工作?

谢谢!

4

1 回答 1

0

首先,不确定您为什么react-ga使用react-router-ga.

本质上 react-ga 包非常灵活,react-router-ga 包实际上只是一个简单的组件。

让我们看看为什么要传递无参数:https ://github.com/fknussel/react-router-ga/blob/master/index.js#L53

第 53 行:window.ga('set', 'page', location.pathname);

如果您只是想让它起作用,那么将其更改为包含 URL 的其他部分将起作用。请参阅位置对象上的参考。

另一种解决方案是使用 react-ga 解决方案将其与路由器一起使用:

https://github.com/react-ga/react-ga/blob/master/demo/app/withTracker.jsx

于 2018-12-03T15:14:58.783 回答