DEV Community

Cover image for Adding Custom Events in Android App KnowMyBoard Using Huawei Analytics Kit
HMS Community
HMS Community

Posted on

Adding Custom Events in Android App KnowMyBoard Using Huawei Analytics Kit

Introduction

In this article, we will learn how to integrate Huawei Analytics kit in Android application KnowMyBoard. Account Kit provides seamless login functionality to the app with large user base.

Huawei Analytics kit provides very easy and convenient way to add custom events, custom events can be used to meet personalized analysis requirements that cannot be met by automatically collected events or predefined events. Analytics Kit allows you to customize events and extend event parameters, or add personalized parameters for predefined events.

Supported Devices

Image descriptionRestrictions

Number of events: A maximum of 500 events are supported.
Number of event parameters: You can register a maximum of 25 parameters for each event, and a maximum of 500 event parameters for each project.
Development Overview

You need to install Android Studio IDE and I assume that you have prior knowledge of Android application development.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.
Android phone (with the USB cable), which is used for debugging.
Software Requirements

Java JDK 1.8 or later.
Android Studio software or Visual Studio or Code installed.
HMS Core (APK) 4.X or later
Integration steps

Step 1. Huawei developer account and complete identity verification in Huawei developer website, refer to register Huawei ID.

Step 2. Create project in AppGallery Connect

Step 3. Adding HMS Core SDK

Let's start coding

How to Add Custom Event in AGC?

Login to AppGallery Connect Console, select projects and choose your project and navigate to Events

Image description

Image description

Image description

How to initialize Analytics Kit?

public class MyApplication extends Application {
    public static Activity myactivity;
    static HiAnalyticsInstance instanceAnalytics;
    @Override
    public void onCreate() {
        super.onCreate();
        MLApplication.initialize(this);
        //initialize Analytics Kit
        instanceAnalytics = HiAnalytics.getInstance(this);
        MLApplication.getInstance().setApiKey(Constants.API_KEY);
        MapsInitializer.setApiKey(Constants.API_KEY);
    }
   public static HiAnalyticsInstance getAnalyticsInstance(){
      return  instanceAnalytics;
    }
    public static void setActivity(Activity activity) {
        myactivity= activity;
    }
    public static Activity getActivity() {
        return myactivity;
    }
}
Enter fullscreen mode Exit fullscreen mode

How to send Custom Events?

private void sendEvent(String eventName,String eventType) {
Bundle bundle = new Bundle();
bundle.putString(eventType,eventType);
bundle.putString( "event_time", getTime());
MyApplication.getAnalyticsInstance().onEvent(eventName, bundle);

}
Enter fullscreen mode Exit fullscreen mode

Result

Image description

Image description

Image description
Tricks and Tips

Makes sure that agconnect-services.json file added.
Make sure required dependencies are added
Make sure that service is enabled in AGC
Enable data binding in gradle.build file
Enable debug mode to see events details in get App debugging hit this command in terminal

adb shell setprop debug.huawei.hms.analytics.app pkg_name
Copy codeCopy code
Conclusion

Finally, we have learnt how to integrate Huawei Analytics kit in Android application KnowMyBoard. You can check the desired result in the result section. You can also go through previous article part-5 here. Hoping Huawei Analytics kit capabilities are helpful to you as well, like this sample, you can make use as per your requirement.

Thank you so much for reading. I hope this article helps you to understand the integration of Huawei Analytics kit in Android application KnowMyBoard.

Reference

Huawei Analytics KitTraining video

Top comments (0)