Vaadin的Google分析跟踪器

时间:2016-01-31 18:27:54

标签: google-analytics vaadin

我正在使用此插件:https://vaadin.com/directory#!addon/googleanalyticstracker

代码示例中的

表示要使用

GoogleAnalyticsTracker tracker = new GoogleAnalyticsTracker("UA-658457-8", "vaadin.com");
mainWindow.addComponent(tracker);

但是GoogleAnalyticsTracker没有实现接口组件,因此当我尝试将其添加到视图中时,我正在

ava.lang.ClassCastException: org.vaadin.googleanalytics.tracking.GoogleAnalyticsTracker cannot be cast to com.vaadin.ui.Component

2 个答案:

答案 0 :(得分:1)

我遇到了相同的错误,因为GoogleAnalyticsTracker没有扩展Component。以下代码对我有用:

private void initGATracker(final String trackerId) {
   tracker = new GoogleAnalyticsTracker(trackerId, "demo.vaadin.com");

   // GoogleAnalyticsTracker is an extension add-on for UI so it is
   // initialized by calling .extend(UI)
   tracker.extend(UI.getCurrent());

   // Track all page views
   tracker.trackPageview("");
}

答案 1 :(得分:0)

试试这段代码:

GoogleAnalyticsTracker tracker = new GoogleAnalyticsTracker("UA-658457-8", "vaadin.com");
tracker.extend(myUI);
tracker.trackPageview("/samplecode/googleanalytics");

并查看java doc以获取更多详细信息,因为最新版本扩展了AbstractJavaScriptExtension。