Skip to content

Automatically track pagviews when using GWTP

Richard Wallis edited this page Feb 10, 2015 · 1 revision

If you're using GWTP you can automatically track page views by creating a simple NavigationTracker class:

package YOUR_PACKAGE_NAME
 
import javax.inject.Inject;
 
import com.arcbees.analytics.shared.Analytics;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.proxy.NavigationEvent;
import com.gwtplatform.mvp.client.proxy.NavigationHandler;
import com.gwtplatform.mvp.shared.proxy.TokenFormatter;
 
public class NavigationTracker implements NavigationHandler {
  private final Analytics analytics;
  private final TokenFormatter tokenFormatter;
 
  @Inject
  NavigationTracker(TokenFormatter tokenFormatter, EventBus eventBus, Analytics analytics) {
    this.analytics = analytics;
    this.tokenFormatter = tokenFormatter;
    eventBus.addHandler(NavigationEvent.getType(), this);
  }
 
  @Override
  public void onNavigation(final NavigationEvent navigationEvent) {
    analytics.sendPageView().documentPath(tokenFormatter.toPlaceToken(navigationEvent.getRequest())).go();
  }
} 

and then binding it in your gin module:

bind(NavigationTracker.class).asEagerSingleton();
Clone this wiki locally