Development of Alliance Statistical SDK
1. Setting up the Environment
- First, get the appKey, in Manage Background Home Page Create applications, fill in information, and the system will automatically generate appKey:
- Secondly, for the Android Studio environment, add gradle dependencies:
dependencies {
compile 'com.umeng.analytics:analytics:latest.integration'
}
- Then, configure appKey in the manifest file and add relevant permissions
<manifest......>
<uses-sdk android:minSdkVersion="8"></uses-sdk>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application ......>
<activity ....../>
<meta-data android:value="58b7fe1aae1bf82ad4001a1d" android:name="UMENG_APPKEY"/>
<-- Download Channel Configuration-->
<meta-data android:value="xiaomi" android:name="UMENG_CHANNEL"/>
</application>
</manifest>
2. Common statistical use
- Mobclick Agent is used to count new users of app and debug mode is enabled to facilitate log observation.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Open debug mode and print emeng's operation log
MobclickAgent.setDebugMode(true);
}
@Override
protected void onResume() {
super.onResume();
MobclickAgent.onResume(this);
}
@Override
protected void onPause() {
super.onStop();
MobclickAgent.onPause(this);
}
- Use Mobclick Agent to count app startup times
//Custom Start Session Period
MobclickAgent.setSessionContinueMillis(2000);
-
There are two kinds of statistics about page dwell time: Activity dwell time and Fragment dwell time.
Statistical Activity residence time, in principle, is not difficult, we only need to record two points of time when the interface is displayed and disappeared, then subtract it to a long time, and finally upload it to the server.
@Override
protected void onResume() {
super.onResume();
//Record page start time
MobclickAgent.onPageStart(getClass().getSimpleName());
MobclickAgent.onResume(this);
}
@Override
protected void onPause() {
super.onStop();
//Record page exit time
MobclickAgent.onPageEnd(getClass().getSimpleName());
MobclickAgent.onPause(this);
}
Note: If you need to count residence times for multiple Activities, you should write the above code into the base class.
Statistical residence time of Fragments, which is due to the design of UmengSDK framework, can not simultaneously count the usage of Activity and Fragments, so it is necessary to disable the tracking statistics function of Point Activity first.
//Setting Disabled Only Statistics Activity Stay Length
MobclickAgent.openActivityDurationTrack(false);
And you also need to remove the statistical Activity code from BaseActivity:
@Override
protected void onResume() {
super.onResume();
// MobclickAgent.onPageStart(getClass().getSimpleName());
MobclickAgent.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
// MobclickAgent.onPageEnd(getClass().getSimpleName());
MobclickAgent.onPause(this);
}
In the Fragment interface, add the same statistical code, and if multiple Fragments require statistical functionality, add the following code to BaseFragment.
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setText("I am Fragment");
textView.setTextSize(25);
textView.setTextColor(Color.GREEN);
return textView;
}
@Override
public void onResume() {
super.onResume();
MobclickAgent.onPageStart(getClass().getSimpleName());
}
@Override
public void onPause() {
super.onPause();
MobclickAgent.onPageEnd(getClass().getSimpleName());
}
}
-
Statistical crash error log
Alliance Statistics SDK automatically counts crash logs in apps. This function enables us to detect and locate errors in time, fix bug s in time, and prompt apps for stability and user experience.
//Turn off the function of statistical crash log
MobclickAgent.setCatchUncaughtExceptions(false);
Upload the errors you have captured to Umeng for statistical analysis:
public static void reportError(Context context, String error)
//or
public static void reportError(Context context, Throwable e)
-
Statistical Behavior and Events
In app usage behavior, some details of the behavior and events may need statistics, such as when the red envelope button is clicked, we think it is a red envelope event; for example, when the voice button is clicked, we think it is a voice event; and for example, when the user changes the avatar, we think it is a voice event. It's a change of head. Through the use behavior analysis of these users, we can understand users better and serve users better. For example, statistics show that users have a lot of voice behavior, so in the next version, we can make the voice function more usable and cool.
Since these events and actions are associated with the business logic of our app, there is no common event behavior. In our own apps, we need to define events and actions that need to be counted.
Umeng provides two types of event statistics:
- Counting events, we only care about the number of events, such as counting how many times a song has been played.
- In calculating events, we care about the duration of the event's behavior, such as counting how long a person listens to a song;
For example, we want to count the event behavior of a red envelope button being clicked, and this is a counting event. We need to add the counting event with the id of fahongbao in the background to view the statistics.
//Our calculated playback time
int duration = 500000;
String eventId = "hotsong";//Hot Song List
//Event description information, such as who the singer is and the title of the song
Map<String,String> eventDesc = new HashMap<String, String>();
eventDesc.put("singer","JJLin");
eventDesc.put("song","Jiangnan");
MobclickAgent.onEventValue(getContext(),eventId,eventDesc,duration);
- Statistical channels
MobclickAgent.UMAnalyticsConfig config = new MobclickAgent.UMAnalyticsConfi(this,appKey,channel);
MobclickAgent.startWithConfigure(config);