Automated logging

更新时间:
复制 MD 格式

Automated logging records page switch events so you can analyze page views (PV) and unique visitors (UV) for features and pages in your application.

Initialize instrumentation

Call the following method to initialize automated log instrumentation.

MPLogger.enableAutoLog();
  • For Portal & Bundle projects, call this method in the postInit method of MockLauncherActivityAgent.

  • For native AAR projects, call this method in the onCreate method of Application after the mPaaS framework initialization method.

Configure an activity

A page view (PV) is recorded for an activity's lifecycle between onResume and onPause. The activity's class name serves as the page identity.

  • Activities that inherit from the mPaaS framework's BaseActivity, BaseFragmentActivity, or BaseAppCompatActivity are automatically recorded.

  • If your activity does not inherit from an mPaaS base class, add the lifecycle listener code to your BaseActivity:

      public class BaseActivity extends Activity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          MPTracker.onActivityCreate(this);
      }
    
      @Override
      public void onWindowFocusChanged(boolean hasFocus) {
          super.onWindowFocusChanged(hasFocus);
          MPTracker.onActivityWindowFocusChanged(this, hasFocus);
      }
    
      @Override
      protected void onResume() {
          super.onResume();
          MPTracker.onActivityResume(this);
      }
    
      @Override
      protected void onPause() {
          super.onPause();
          MPTracker.onActivityPause(this);
      }
    
      @Override
      protected void onDestroy() {
          super.onDestroy();
          MPTracker.onActivityDestroy(this);
      }
    }

Configure a fragment

  • You can inherit from com.mpaas.mas.adapter.api.BaseFragment, provided by mPaaS.

  • If you use the Fragment from the official support-v4 library, make your BaseFragment implement the TrackPageConfig interface and add the lifecycle listener code:

      public class BaseFragment extends Fragment implements TrackPageConfig {
    
      /**
       * The page identity. The class name is typically used.
       * If this is not passed, the page does not appear in the page analysis section of the console.
       */
      @Override
      public String getPageSpmId() {
          return this.getClass().getName();
      }
    
      @Override
      public Map<String, String> getExtParam() {
          return null;
      }
    
      @Override
      public boolean isTrackPage() {
          return true;
      }
    
      @Override
      public void onResume() {
          super.onResume();
          MPTracker.onFragmentResume(this);
      }
    
      @Override
      public void onPause() {
          super.onPause();
          MPTracker.onFragmentPause(this);
      }
    
      @Override
      public void onHiddenChanged(boolean hidden) {
          super.onHiddenChanged(hidden);
          MPTracker.onFragmentHiddenChanged(this, hidden);
      }
    
      @Override
      public void setUserVisibleHint(boolean isVisibleToUser) {
          super.setUserVisibleHint(isVisibleToUser);
          MPTracker.onFragmentSetUserVisibleHint(this, isVisibleToUser);
      }
    
      @Override
      public void onDestroy() {
          super.onDestroy();
          MPTracker.onFragmentDestroy(this);
      }
    }

Add custom parameters

In baseline 10.1.68.44 and later, use the following methods to add custom parameters to automated logs.

MPLogger.addAutoLogCustomParam("test_key1", "test_value1");
MPLogger.addAutoLogCustomParam("test_key2", "test_value2");

Map<String, String> params = new HashMap<>();
params.put("test_key3", "test_value3");
params.put("test_key4", "test_value4");
MPLogger.addAutoLogCustomParams(params);