还需要一些其他的东西。
首先,您需要使用以下资源文件设置您的 trackingId:
<resources>
<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-00000000-3</string>
<!--Enable automatic activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>
<!--Enable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
同样在您的活动中,您需要以下内容:
@Override
protected void onStop() {
super.onStop();
analytics.stop(this);
}
@Override
protected void onStart() {
super.onStart();
analytics.start(this);
}
我在这里的实用程序类中定义了这些:
/**
*
*/
package co.uk.benbun.nvrrclubapp.utils;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GAServiceManager;
/**
* @author RichardKavanagh
*
*/
public class analytics {
public static void raiseEvent(Context context, String a, String b, String c, String makeToast) {
String msg = a + " : " + b + " : " + c;
EasyTracker.getTracker().sendEvent(a, b, c, null);
GAServiceManager.getInstance().dispatch();
Log.i("Analytics", msg);
if (makeToast.equals("Y")) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
public static void start(Activity activity) {
EasyTracker.getInstance().activityStart(activity);
}
public static void stop(Activity activity) {
EasyTracker.getInstance().activityStop(activity);
}
}