javascript - 使用 JSNI 从 GWT 向 Google Analytics 发送事件

标签 javascript gwt google-analytics jsni

我的 HTML 文件中有 和 之间的所有 Google Analytics(分析)代码。这些统计数据得到正确处理。

在我的 onclick 事件之后,下面的函数被调用,我想向 Google Analytics 发送一个事件,但从未收到。我做错了什么?

public static native void googleAnalyticsEvent(String boxId) /*-{
_gaq.push(['_trackEvent', boxId , 'Click']);
}-*/;

仅供引用,如果我将此代码替换为以下代码,则会正确显示警报:

public static native void googleAnalyticsEvent(String boxId) /*-{
alert(boxId);
}-*/;

最佳答案

尝试:

public static native void googleAnalyticsEvent(String boxId) /*-{
    $wnd._gaq.push(['_trackEvent', boxId , 'Click']);
}-*/;

来自 https://developers.google.com/web-toolkit/doc/1.6/DevGuideCodingBasics#DevGuideJavaScriptNativeInterface

"Note that the code did not reference the JavaScript window object directly inside the method. When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page's window and document."

关于javascript - 使用 JSNI 从 GWT 向 Google Analytics 发送事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663834/

相关文章:

java - GWT 开发的当前状态是什么?

ajax - 带有 GWT 的 HtmlUnit 返回不完整的页面

java - 需要帮助将身份验证迁移到 Google Analytics 中的 OAuth2.0

c# - jQuery Mobile + MVC 3 + Request.Path

javascript - 将 cookie 值设置为递增的 int

javascript - jquery UI 添加带有动画的类不起作用

javascript - Proxy() block 作用域 - Javascript

GWT Servlet 错误 com.google.gwt.user.client.rpc .StatusCodeException : 404 on Tomcat server

google-analytics - 您可以推送具有值但没有标签的_trackEvent吗?

javascript - 处理 HTTP 状态 Ajax 响应