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.)
integration with other products, including Google Ads, Google Data Studio, Salesforce Marketing Cloud, Google AdSense, Google Optimize 360, Google Search Ads 360, Google Display & Video 360, Google Ad Manager and Google Search Console.
Google Analytics helps you understand how people use your web, Apple, or Android app. The SDK automatically captures a number of events and user properties and also allows you to define your own custom events to measure the things that uniquely matter to your business.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With