如何使用异步脚本将 Google Analytics 集成到 GWT [英] How to integrate Google Analytics into GWT using the asynchronous script

查看:26
本文介绍了如何使用异步脚本将 Google Analytics 集成到 GWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 GWT 应用程序中使用 Google Analytics 跟踪页面.我已经检查了以下线程:将 Google Analytics 集成到 GWT 应用程序

I'm trying to track pages using Google Analytics within a GWT application. I already check the following thread: Integrating Google Analytics into GWT application

我认为解决方案:

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

仅适用于同步 GA 脚本.

only works using the synchronous GA script.

我正在尝试以下内容:

public native void trackHit (String pageName) /*-{
  try {
    $wnd._gaq.push (['_setAccount', 'UA-XXXXXX-XX']);
    $wnd._gaq.push (['_setDomainName', '.mydomain.com']);
    $wnd._gaq.push (['_trackPageview', pageName]);
  } catch (err) {
    alert('failure on gaq' + err);
  }
}-*/;

而且不适合我.

推荐答案

这是我的页面和事件跟踪功能:

Here are my page- and event-tracking functions:

public static native void trackEvent(String category, String action, String label) /*-{
    $wnd._gaq.push(['_trackEvent', category, action, label]);
}-*/;

public static native void trackEvent(String category, String action, String label, int intArg) /*-{
    $wnd._gaq.push(['_trackEvent', category, action, label, intArg]);
}-*/;

public static native void trackPageview(String url) /*-{
    $wnd._gaq.push(['_trackPageview', url]);
}-*/;

我在主机页面中像往常一样执行 _setAccount 东西(需要在 trackPageview() 等工作之前执行:

I do the _setAccount stuff like normal in the host page (needs to execute before trackPageview() etc will work:

<!-- Analytics -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-FAKE1234-2']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

您不需要在每次发布事件时都使用 setAccount,只需在开始时使用即可.我不介意 try{}catch{} 东西...我实际上不懂 JavaScript.

You don't need to use setAccount every time you post an event, only at the beginning. I don't bother with try{}catch{} stuff... I don't actually know JavaScript.

这篇关于如何使用异步脚本将 Google Analytics 集成到 GWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆