我正在尝试在我新创建的应用中添加Google Analytics代码,但它无效。
我认为现在分析设置指南已更改。我正在学习教程https://developers.google.com/analytics/devguides/collection/android/v4/start。我尝试了那里的样本应用程序,但它也无法正常工作。实时或应用概述中没有任何内容。
当我尝试使用旧的设置指南(如下所示)提供我之前应用的属性ID时,它运行正常。
以下是我用于旧应用的代码:
//This is code from one of my activity.
MyApplication m = new MyApplication();
Tracker t = m.getTracker(
MyApplication.TrackerName.APP_TRACKER,this);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory("analytics demo 123")
.setAction("Test")
.setLabel(null)
.build());
//MyApplication class
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
// ignored
}
}
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXXXXXX-X";
public static int GENERAL_TRACKER = 0;
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public MyApplication() {
super();
}
synchronized Tracker getTracker(TrackerName trackerId,Activity activity) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(activity);
analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: analytics.newTracker(R.xml.global_tracker);
t.enableAdvertisingIdCollection(true);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
此代码与旧属性ID一起正常工作,但只要我用新创建的id替换旧ID,就无法跟踪事件。
当我按照新步骤设置Google分析时,它甚至不会实时显示。
是否有人面临同样的问题?请回复。