English 中文(简体)
Integrating Google Analytics into GWT application
原标题:

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

最佳回答

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.

问题回答
<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.





相关问题
Refresh the UI in gwt

I have created some custom composite widget, which listens to an events (in my case loginEvent). Once the event is caught the state of the widget changes so as the way it should look (in my case I ...

How to create a development/debug and production setup

I recently deployed inadvertently a debug version of our game typrX (typing races at www.typrx.com - try it it s fun). It was quickly corrected but I know it may happen again. After digging on ...

GWT error at runtime: ....style_0 is null

My code works fine in IE 8, but on firefox 3.5, the javascript fails to load. I used firebug to figure out why, and the error I get is (GWT detailed mode) My suspicion is that some style is not ...

GWT s hosted mode not working

I ve been stumped for a while trying to figure out why my GWT demo app isn t working in hosted mode so I went back and downloaded the Google Web Toolkit again, unzipped it and simply went to the ...

How to disable Google Visualizations Tool Tips?

Does anyone know how to disable the tool-tip boxes that popup when a Google Visualizations chart is clicked (Selected)? Following the sample code at Getting Started with Visualizations, the "...

GWT 2 CssResource how to

I have a GWT 1.7 application and I want to upgrade it to GWT 2 Milestone 2. The application uses 2 big external CSS files. In GWT 1.7 I had a public folder and put both the CSS files in the folder and ...

热门标签