简体   繁体   English

如何在React VR项目中实现Google Analytics(分析)事件跟踪?

[英]How to implement Google Analytics event tracking inside a React VR project?

This is loosely written to give a basic idea of what I'm trying achieve. 撰写本文的目的是为了大致了解我正在尝试实现的目标。

< VrButton onClick={props.userClick}>< /VrButton>

 userClick={() => this.triggerTracking} triggerTracking() { ga('send', 'event', 'myEventCategory', 'myEventAction', 'myEventLabel'); } 

I expect the code to trigger Google Analytics event tracking in the GA system when the user clicks on a button, but I get an error message - "ga is not a function". 我希望该代码在用户单击按钮时触发GA系统中的Google Analytics(分析)事件跟踪,但是我收到一条错误消息-“ ga不是函数”。

I have GA set up in my index.html file, with the proper ID, and pulling in the latest analytics.js API. 我在index.html文件中设置了Google Analytics(分析),具有正确的ID,并提取了最新的analytics.js API。

React VR is all within a web worker context so it is not possible to access anything on your window without the use of native modules. React VR都在Web Worker上下文中,因此如果不使用本机模块就无法访问窗口上的任何内容。

You can embed them directly in your client js and use the GA tracking functions as you normally would there. 您可以将它们直接嵌入到客户端js中,并像往常一样使用GA跟踪功能。 You will then call a function on your native module within your react VR app. 然后,您将在react VR应用程序中的本机模块上调用一个函数。

You can check out the documentation here: https://facebook.github.io/react-vr/docs/native-modules.html 您可以在此处查看文档: https : //facebook.github.io/react-vr/docs/native-modules.html

Try using the window scope as: 尝试将窗口范围用作:

window.ga('send', 'event', 'myEventCategory', 'myEventAction', 'myEventLabel');

I'm not familiar with React at all, but perhaps React causes some abstraction between the window and the react scope, making your ga() function unavailable. 我一点都不熟悉React,但也许React会在窗口和react范围之间造成某种抽象,从而使您的ga()函数不可用。

Do next stps: 执行下一个步骤:

  • open the network debug tool of your browser. 打开浏览器的网络调试工具。
  • reload your page 重新载入您的页面
  • review loaded url list and check that www.google-analytics.com/analytics.js is loaded 查看已加载的网址列表,并检查是否已加载www.google-analytics.com/analytics.js

If you does not see such url loaded - read google analitycks manual about how to setup google analytics on your page. 如果您没有看到这样的网址,请阅读google analitycks手册,了解如何在页面上设置google Analytics。

If you see such url replace url www.google-analytics.com/analytics.js with www.google-analytics.com/analytics_debug.js in your page, than reload and than go to console tab of your browser debugger and check the errors. 如果您在网页中看到此类网址,则将网址www.google-analytics.com/analytics.js替换为www.google-analytics.com/analytics_debug.js ,然后重新加载并转到浏览器调试器的控制台标签并检查错误。

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

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