我正在使用此插件: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
答案 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。