将 Google Analytics 集成到 GWT 应用程序中
这应该是完全简单的,但无论我尝试什么,我都无法让它工作。我正在尝试将 Google Analytics 与 GWT 应用程序一起使用。据我了解,有两种方法可以做到这一点:
第一种是同步的,通过在 的末尾插入跟踪代码。部分 HTML 页面,然后调用此方法:
public static native void recordAnalyticsHit(String pageName) /*-{
pageTracker._trackPageview(pageName);
}-*/;
第二个是异步的,通过在 之后插入跟踪代码标记,然后调用此方法:
public static native void recordAnalyticsHit(String pageName) /*-{
_gaq.push(['_trackPageview(' + pageName + ')']);
}-*/;
但是,当运行每个方法时,我在托管模式下遇到此异常:
[ERROR] [myproject] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): pageTracker is not defined
[ERROR] [myproject] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): _gaq is not defined
在 Firebug 中观察站点时,我看到 ga.js 已加载,但仅此而已。
有人让 Analytics 与 GWT 一起使用吗?另外,_gaq 是否接受页面名称作为 trackPageview 参数,因为我见过的所有示例都使用此调用:(
_gaq.push(['_trackPageview()']);
当然,这对我也不起作用。)
This should be totally simple but I can't get it working no matter what I try. I'm trying to use Google Analytics with GWT application. From what I understood, there are two way to do it:
First is synchronous, by inserting tracking code at the end of <head> section HTML page and then calling this method:
public static native void recordAnalyticsHit(String pageName) /*-{
pageTracker._trackPageview(pageName);
}-*/;
Second is asynchronous, by inserting tracking code just after <body> tag and then calling this method:
public static native void recordAnalyticsHit(String pageName) /*-{
_gaq.push(['_trackPageview(' + pageName + ')']);
}-*/;
When running each of those methods, however, I get this exceptions in hosted mode:
[ERROR] [myproject] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): pageTracker is not defined
[ERROR] [myproject] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (ReferenceError): _gaq is not defined
When observing site in Firebug, I see that ga.js gets loaded, but that's about it.
Did anyone get Analytics working with GWT? Also, does _gaq accept page name as trackPageview parameter, since all the examples I've seen use this call:
_gaq.push(['_trackPageview()']);
(Of course, that also doesn't work for me.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是一种猜测,但您可能需要通过 JSNI 中的
$wnd
引用主机页(包含 Google Analytics JS 代码的主机页),如下所示:JSNI 代码(以及一般情况下,GWT 代码)在 iframe 中运行以保持命名空间干净,这就是为什么您需要对主窗口的
$wnd
引用。This is just a guess, but you probably need to reference the host page (the one where the Google Analytics JS code has been included) via
$wnd
in the JSNI, like this:JSNI code (and in general, GWT code) is run in a iframe to keep the namespace clean, that's why you need the
$wnd
reference to the main window.请参阅 http://www.google.com/ support/analytics/bin/answer.py?hl=en&answer=55485 了解详情。
See http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55485 for details.