16

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

Domchi
  • 10,097
  • 6
  • 50
  • 63
  • 4
    Just so you know _gaq.push(['_trackPageview()']); was a bug in Google's documentation. It should be _gaq.push(['_trackPageview']); without the parens. – Brian Mar 24 '10 at 19:04
  • 1
    you can use gwt-gatracker (https://code.google.com/p/gwt-gatracker/) which makes your life much easier – Sameeh Harfoush Aug 16 '13 at 06:43

2 Answers2

23

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.

Igor Klimer
  • 15,221
  • 3
  • 45
  • 57
  • 1
    This would of course also work with GA async: $wnd._gaq.push(['_trackPageview', pageName]); – Brian Mar 24 '10 at 19:07
0
<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.

Antonio
  • 10,838
  • 11
  • 54
  • 87
  • Antonio, can you please elaborate your answer? Question was about calling Google Analytics from Java GWT code. – Domchi Oct 12 '10 at 15:25
  • Actually I just wanted to say two things: 1) ga.js should be included before GWT code calls it; 2) $wnd.pageTracker._trackPageview(pageName); does not work for me and I had to use the code I found in official Google's FAQ. – Antonio Oct 13 '10 at 12:17