将 Google Analytics 集成到 GWT 应用程序中

发布于 2024-08-25 00:21:23 字数 1027 浏览 12 评论 0原文

这应该是完全简单的,但无论我尝试什么,我都无法让它工作。我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-09-01 00:21:23

这只是一种猜测,但您可能需要通过 JSNI 中的 $wnd 引用主机页(包含 Google Analytics JS 代码的主机页),如下所示:

public static native void recordAnalyticsHit(String pageName) /*-{
    $wnd.pageTracker._trackPageview(pageName);
}-*/;

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:

public static native void recordAnalyticsHit(String pageName) /*-{
    $wnd.pageTracker._trackPageview(pageName);
}-*/;

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.

無處可尋 2024-09-01 00:21:23
<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
   var pageTracker = _gat._getTracker("UA-xxxxxx-x");
   pageTracker._trackPageview("/subdirectory/pagename");
</script>

请参阅 http://www.google.com/ support/analytics/bin/answer.py?hl=en&answer=55485 了解详情。

<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
   var pageTracker = _gat._getTracker("UA-xxxxxx-x");
   pageTracker._trackPageview("/subdirectory/pagename");
</script>

See http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55485 for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文