How to integrate Google Analytics into GWT using the asynchronous script

后端 未结 3 697
灰色年华
灰色年华 2020-12-29 11:35

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

I

相关标签:
3条回答
  • 2020-12-29 11:56

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

    0 讨论(0)
  • 2020-12-29 12:02

    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:

    <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);
    }-*/;
    
    0 讨论(0)
  • 2020-12-29 12:10

    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:

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

    0 讨论(0)
提交回复
热议问题