上下文:在该应用程序中,我正在使用react-slick来允许用户浏览轮播等组件。 (注意:当用户浏览轮播时,应用程序的URL不会更改/更新;始终 https://myApplicationURL.com)
我要完成的工作:轮播中的每个组件都使用react-ga来初始化和跟踪组件级别的综合浏览量分析。
我的期望:Google Analytics(分析)信息中心将返回用户当前正在查看的正确组件名称。
实际发生的情况:Google Analytics(分析)信息中心显示的组件名称不正确。 (例如:应用程序位于联系人组件上-应该显示“ / contact”,但GA仪表板显示了另一个组件名称)
**CAROUSEL COMPONENT**
import React, { Component } from "react";
import Slider from "react-slick";
import ReactGA from 'react-ga';
import About from '../../components/about';
import {default as Project1} from '../../components/projectTemplate';
import {default as Project2} from '../../components/projectTemplate';
import {default as Project3} from '../../components/projectTemplate';
import {default as Project4} from '../../components/projectTemplate';
import Contact from '../../components/contact';
export default class Carousel extends Component {
constructor(props) {
super(props);
this.state = {
nav1: null,
nav2: null,
pageNumber: 0
};
}
componentDidMount() {
this.setState({
nav1: this.slider1,
nav2: this.slider2
});
}
afterChangeHandler = currentSlide => {
this.setState({
pageNumber: currentSlide++
})
};
render() {
const carousel1 = {
asNavFor: this.state.nav2,
ref: slider => (this.slider1 = slider),
afterChange: this.afterChangeHandler
}
const carousel2 = {
asNavFor: this.state.nav1,
ref: slider => (this.slider2 = slider),
}
return (
<div id="carousel-container">
<Slider {...carousel1}>
<div>
<About props={this.props.props} />
</div>
<div>
<Project1 project={this.props.props.project[0]} />
</div>
<div>
<Project2 project={this.props.props.project[1]} />
</div>
<div>
<Project3 project={this.props.props.project[2]} />
</div>
<div>
<Project4 project={this.props.props.project[3]} />
</div>
<div>
<Contact {/*props*/} />
</div>
</Slider>
<Slider {...carousel2}>
{/*slider2 content*/}
</Slider>
</div>
);
}
}
**ABOUT COMPONENT**
import React from 'react';
import ReactGA from 'react-ga';
const About = props => {
//Google Analytics
ReactGA.initialize('[User ID removed]');
ReactGA.ga('set', 'page', '/about');
ReactGA.ga('send', 'pageview');
return(
<div id="aboutContainer">
{/*Component Content*/}
</div>
);
};
export default About;
**PROJECT COMPONENT**
import React from 'react';
import ReactGA from 'react-ga';
const ProjectTemp = props => {
const name = props.project.name
// Google Analytics
ReactGA.initialize('[User ID removed]');
ReactGA.ga('set', 'page', `/project/${name}`);
ReactGA.ga('send', 'pageview');
return(
<div id="projectTempContainer">
{/*Project Content*/}
</div>
);
};
export default ProjectTemp;
**CONTACT COMPONENT**
import React from 'react';
import ReactGA from 'react-ga';
const Contact = props => {
//Google Analytics
ReactGA.initialize('[User ID removed]');
ReactGA.ga('set', 'page', '/contact');
ReactGA.ga('send', 'pageview');
return(
<div id="contactContainer">
{/*Contact Content*/}
</div>
);
};
export default Contact;
答案 0 :(得分:0)
我建议使用细分分析库并遵循我们的React quickstart guide来跟踪页面调用。如果要在转盘中渲染单个组件,则可以使用componentDidMount
来调用page
调用。您可以通过参数手动设置页面名称,这将帮助您避免遇到/contact
时遇到的问题。下面的示例显示了执行此操作的一种方法:
export default class CarouselContact extends Component {
componentDidMount() {
window.analytics.page('Contact');
}
render() {
return (
<h1>
Contact page.
</h1>
);
}
}
我是https://github.com/segmentio/analytics-react的维护者。如果您有兴趣尝试使用多种分析工具(我们支持超过250个目的地),而无需编写任何其他代码,则可以使用细分功能通过切换开关来打开和关闭不同的目的地。