[关闭]
@asce1885 2015-08-12T10:03:44.000000Z 字数 3082 阅读 498

Android统计SDK API规范调研

Android


@author ASCE1885的 Github 简书 微博 CSDN

本次调研主要对三款主流的统计SDK进行:TaklingData,友盟,百度。

TalkingData

1)初始化SDK

  1. //如果不想在AndroidManifest里的meta-data申明了TD_APP_ID,请调用:
  2. init(final Context context, final String appId, final String partnerid)
  3. //如果已经在AndroidManifest里的meta-data申明了TD_APP_ID,请调用:
  4. init(final Context context)

2)统计Activity

  1. @Override
  2. protect void onResume() {
  3. super.onResume();
  4. TCAgent.onResume(this);
  5. }
  6. @Override
  7. protect void onPause() {
  8. super.onPause();
  9. TCAgent.onPause(this);
  10. }

3)跟踪自定义的页面(如Fragment, View等)

  1. TCAgent.onPageStart(Context ctx, String pageName);
  2. TCAgent.onPageEnd(Context ctx, String pageName);

4)自定义事件

  1. TCAgent.onEvent(Context ctx, String EVENT_ID);
  2. TCAgent.onEvent(Context ctx, String EVENT_ID, String EVENT_LABEL);
  3. TCAgent.onEvent(Context ctx, String EVENT_ID, String EVENT_LABEL, Map<string, object=""> kv);
  4. TCAgent.setGlobalKV(String key, Object value);
  5. TCAgent.removeGlobalKV(String key);

5)收集应用错误日志

  1. TCAgent.setReportUncaughtExceptions(Boolean enabled)
  2. TCAgent.onError(Activity activity, Throwable throwable)

友盟

1)统计Activity

  1. public void onResume() {
  2. super.onResume();
  3. MobclickAgent.onResume(this);
  4. }
  5. public void onPause() {
  6. super.onPause();
  7. MobclickAgent.onPause(this);
  8. }

2)跟踪自定义的页面(如Fragment, View等)

  1. public void onResume() {
  2. super.onResume();
  3. MobclickAgent.onPageStart("SplashScreen"); //统计页面(仅有Activity的应用中SDK自动调用,不需要单独写)
  4. MobclickAgent.onResume(this); //统计时长
  5. }
  6. public void onPause() {
  7. super.onPause();
  8. MobclickAgent.onPageEnd("SplashScreen"); // (仅有Activity的应用中SDK自动调用,不需要单独写)保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
  9. MobclickAgent.onPause(this);
  10. }

3)自定义事件

  1. MobclickAgent.onEvent(Context context, String eventId);
  2. MobclickAgent.onEvent(Context context, String eventId, HashMap<String,String> map);

4)收集应用错误日志

  1. public static void reportError(Context context, String error)
  2. //或
  3. public static void reportError(Context context, Throwable e)

百度

1)统计Activity

  1. public void onResume() {
  2. super.onResume();
  3. /**
  4. * 页面起始(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加)
  5. * 不能与StatService.onPageStart以及onPageEnd函数交叉使用
  6. */
  7. StatService.onResume(this);
  8. }
  9. public void onPause() {
  10. super.onPause();
  11. /**
  12. * 页面结束(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加)
  13. * 不能与StatService.onPageStart以及onPageEnd函数交叉使用
  14. */
  15. StatService.onPause(this);
  16. }

4)跟踪自定义页面(Fragment,View等)

  1. @Override
  2. public void onResume() {
  3. super.onResume();
  4. StatService.onResume(this);
  5. }
  6. @Override
  7. public void onPause() {
  8. super.onPause();
  9. StatService.onPause(this);
  10. }
  1. public void onResume() {
  2. Log.w(Conf.TAG, "DemoDialogActivity.OnResume()");
  3. super.onResume();
  4. /**
  5. * 页面起始(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加)
  6. * 与StatService.onResume(this)类似;
  7. */
  8. StatService.onPageStart(this, "DemoDialogActivityp");//(this);
  9. }
  10. public void onPause() {
  11. Log.w(Conf.TAG, "DemoDialogActivity.onPause()");
  12. super.onPause();
  13. /**
  14. * 页面结束(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加)
  15. * 与StatService.onPause(this)类似;
  16. */
  17. StatService.onPageEnd(this, "DemoDialogActivityp");//(this);
  18. }

3)自定义事件

  1. void onEvent(Context context, String event_id, String label)
  2. void onEvent(Context context, String event_id, String label, int acc)
  3. void onEventStart(Context context, String event_id, String label)
  4. void onEventEnd(Context context, String event_id, String label)
  5. void onEventDuration(Context context, String event_id, String label, int milliseconds)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注