编辑:我修正了错误。但我仍然无法在Google Analytics窗口上看到实时访问者。我检查了三次跟踪ID。这是正确的,代码中没有错误。但为什么我不能访问我的访客?最少1位访客应该是实时访客区。因为我在我的应用上。
我想在我的应用程序中使用google analytics v4。我向google doc添加了一个according课程。
GoogleAnal.java
public class GoogleAnal extends Application {
public GoogleAnal() {
super();
}
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
if(trackerId == TrackerName.APP_TRACKER){
Tracker t = analytics.newTracker(R.xml.app_tracker);
mTrackers.put(trackerId, t);
}
}
return mTrackers.get(trackerId);
}
}
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
....
....
//Below line gives Error:
//11-26 09:52:35.005: E/AndroidRuntime(2239): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.helloworld.GoogleAnal
((GoogleAnal) getApplication()).getTracker(GoogleAnal.TrackerName.APP_TRACKER);
}
@Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
@Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
我做错了什么?我应该写什么而不是((GoogleAnal) getApplication()).getTracker(GoogleAnal.TrackerName.APP_TRACKER);
如果我的方式都错了,你能提出一些建议吗?
答案 0 :(得分:2)
选项1:您是否在清单中添加了GoogleAnal?
<application
android:name="com.xxx.helloworld.GoogleAnal"
...
选项2:这是我的应用程序中的getTracker方法:
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
analytics.setLocalDispatchPeriod(15);
analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
Tracker t = analytics.newTracker(PROPERTY_ID);
t.setAppName(getResources().getString(R.string.app_name));
t.enableAdvertisingIdCollection(true);
t.enableAutoActivityTracking(true);
t.enableExceptionReporting(true);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}