Adding Custom Events in Android App KnowMyBoard Using Huawei Analytics Kit
Tags: huaweianalyticskit, huaweidevelopers, hmscore
Published: 2022-07-22 14:51:39

**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 description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvfetctwfc2hu0y69ipt.PNG)Restrictions

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e933g8ihcv44vnm8gpd0.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nzh021npn56ess0w22nj.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syfqzii0ps0dst5jboqt.PNG)

**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;
    }
}
```
**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);

}
```
**Result**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hzjkuh6x3zdww3z6xdi.gif)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gag5oxmxgpj6zkndzcss.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxr8pqjt3erk23d7tsyv.PNG)
**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 Kit](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050745149) – [Training video](https://developer.huawei.com/consumer/en/training/course/video/101582991973534154)