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