简体   繁体   English

将GA事件添加到可重用的React组件

[英]Adding GA Events to reusable React components

Build a react web page with reusable components. 用可重用的组件构建一个反应网页。 Now the problem arrives in adding GA events because GA events are mostly page specific events and react components are reusable. 现在的问题是添加GA事件,因为GA事件主要是特定于页面的事件,而react组件是可重用的。 What will be the best way to apply GA event on this? 对此应用GA事件的最佳方法是什么?

Eg: Suppose Card component is quite reusable and I have used it in my Home Page . 例如:假设Card组件可重复使用,并且我已经在Home Page使用了它。 GA event will be like ('Home Page','Card Click','Card No') . GA事件将类似于('Home Page','Card Click','Card No') But if I reuse Card component somewhere else lets say User Page , GA event of Home Page will be call instead which is wrong. 但是,如果我在其他地方重用Card组件,比如说User Page ,则会调用Home page的GA事件,这是错误的。 Instead User Page event should be called instead. 而是应调用User Page事件。 How can I solve this? 我该如何解决?

One way I can think of is to add click event listeners separately in page specific JS and call the events. 我能想到的一种方法是在特定于页面的JS中分别添加单击事件侦听器并调用事件。 But that would lead to multiple click handlers (Assuming card also has click event). 但这将导致多个单击处理程序(假设卡也具有单击事件)。 Any better suggestion on this? 有什么更好的建议吗?

Why not pass the context in via props and then your click handler could look something like this: 为什么不通过props传递上下文,然后单击处理程序可能如下所示:

handleClick() {
  ga('send', 'event', this.props.pageContext, 'Card Click', 'Card No');
}

That's typically how context is handled in React. 这通常是在React中处理上下文的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM