如何使用异步脚本将谷歌分析集成到GWT中

时间:2021-11-20 13:40:31

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

我试图在GWT应用程序中使用谷歌分析跟踪页面。我已经检查了以下线程:将谷歌分析集成到GWT应用程序中

I think that the solution:

我认为解决办法是:

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

only works using the synchronous GA script.

只能使用同步GA脚本。

I'm trying with the following:

我正在尝试以下方法:

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);
  }
}-*/;

And is not working for me.

而不是为我工作。

3 个解决方案

#1


20  

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]);
}-*/;

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

我在主机页面中做了_setAccount之类的事情(需要在trackPageview()起作用之前执行):

<!-- 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>

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.

每次发布事件时都不需要使用setAccount,只需要在开始时使用。我不需要尝试{}catch{}东西…我不懂JavaScript。

#2


3  

There is a new version of Google Analytics out which uses a new analytics.js script. It's the same process though, just add the script in your html header:

有一个新的版本的谷歌分析,其中使用了一个新的分析。js脚本。这是同样的过程,只要在你的html头中添加脚本:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-YOUR_ACCOUNT-X', 'auto');
    ga('send', 'pageview');
</script>

And then can call the new methods like so:

然后可以这样调用新方法:

public static native void googleAnalyticsTrackPageView(String url) /*-{
    $wnd.ga('send', 'pageview', url);
}-*/;

#3


1  

there is a project on goole code called gwt-gatracker that implement GA function to use in GWT projects. check it out here

有一个关于goole代码的项目叫做GWT -gatracker,它实现了在GWT项目中使用的GA函数。检查一下这里

#1


20  

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]);
}-*/;

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

我在主机页面中做了_setAccount之类的事情(需要在trackPageview()起作用之前执行):

<!-- 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>

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.

每次发布事件时都不需要使用setAccount,只需要在开始时使用。我不需要尝试{}catch{}东西…我不懂JavaScript。

#2


3  

There is a new version of Google Analytics out which uses a new analytics.js script. It's the same process though, just add the script in your html header:

有一个新的版本的谷歌分析,其中使用了一个新的分析。js脚本。这是同样的过程,只要在你的html头中添加脚本:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-YOUR_ACCOUNT-X', 'auto');
    ga('send', 'pageview');
</script>

And then can call the new methods like so:

然后可以这样调用新方法:

public static native void googleAnalyticsTrackPageView(String url) /*-{
    $wnd.ga('send', 'pageview', url);
}-*/;

#3


1  

there is a project on goole code called gwt-gatracker that implement GA function to use in GWT projects. check it out here

有一个关于goole代码的项目叫做GWT -gatracker,它实现了在GWT项目中使用的GA函数。检查一下这里