HMS Unity integration for Analytics kit and Crash Services

YusufAltun
Huawei Developers
Published in
8 min readDec 14, 2020

Hello everyone,

In this article we are going to create Unity project and explain how to implement HMS Analytics kit and Crash Services.

Introduction

HUAWEI Analytics Kit offers a rich array of preset analytics models(Event, Behavior, Audience, Funnel, Retention, Real-Time) that help you gain an in-depth insight into your users, products, and content. With this insight, you can then take a data-driven approach to make informed decisions for product and marketing optimizations.

By integrating the HMS Core Analytics SDK, you can:

  • Collect and report custom events through coding.
  • Set a maximum of 25 user attributes.
  • Automate event collection and session calculation with predefined event IDs and parameters.

HUAWEI Analytics Kit identifies users and collects statistics on users by an anonymous application identifier (AAID). The AAID is reset in the following scenarios:

1) Uninstall or reinstall the app.

2) The user clears the app data.

After the AAID is reset, the user will be counted as a new user.

There are 3 types of events: Automatically collected, predefined, and custom.

Automatically collected events are collected from the moment you enable the kit in your code. Event IDs are already reserved by HUAWEI Analytics Kit and cannot be reused.

Predefined events include their own Event IDs which are predefined by the HMS Core Analytics SDK based on common application scenarios. The ID of a custom event cannot be the same as a predefined event’s ID. If so, you will create a predefined event instead of a custom event.

Custom events are the events that you can create for your own requirements.

Huawei Crash Service provides a powerful yet lightweight solution to app crash problems. With the service, you can quickly detect, locate, and resolve app crashes (unexpected exits of apps), and have access to highly readable crash reports in real time, without the need to write any code.

After you integrate the Crash SDK into your app, it will be automatically initialized when your app is launched. When an app crash occurs, the SDK will report the crash information to HUAWEI Analytics. A readable report will be generated in about 5–10 minutes, helping you quickly detect, locate, and rectify the problem.

Crash Service workflow

Integration Preparations

Please check HMS Unity plugin integration details below:

The Crash service uses capabilities of HUAWEI Analytics when reporting crash events. Therefore, you must enable HUAWEI Analytics before integrating the Crash SDK. For details, please refer to Service Enabling.

  1. Create an app in AppGallery Connect.
  2. Create Unity Project.
  3. Add the app package name.
  4. Sign in to AppGallery Connect and select My projects.
  5. Click the app for which you need to enable Analytics Kit.
  6. Select any menu under HUAWEI Analytics and click Enable Analytics service. (You need the management permission to perform this operation.)
  7. Turn on Enable the API permission. This option is selected by default. If you deselect this option, data will not be reported. If you do not enable the API permission here but the API permission is required later, go to Project Setting > Manage APIs and manually enable the API permission for Analytics Kit.
  8. Download the configuration(agconnect-services.json) file.

Enable Analytics Service on AGC >> Huawei Analytics >> Project OverView

Enable Analytics Service

Unity Integration Development

Development Steps

1- Download HMS Unity Plugin.

We are using EvilMindDevs/hms-unity-plugin git project. This plugin have two main branch for Unity versions.(version 2019 used)

  • Download plugin from this for Unity version 2019.x.
  • Download plugin from this for Unity version 2018.x .

2- Import the Plugin to your Unity project

3- Configure AndroidManifest file

4- Add configuration (agconnect-service.json) file

5- Use AnalyticsManager prefabs

1- Download HMS Unity Plugin

We have a two option for downloading we can download latest package or latest codes. In this article we will use latest package.

Latest Package

Optional - Latest Codes

Latest Codes

2- Import the Plugin to your Unity project

Import package to unity.

Import Package
Asset Folder

3- Configure AndroidManifest file

  • Open project_path\Assets\Plugins\Android\ AndoridManifest.
  • Update App ID, CP ID and package name, this informations exist in agconnect.json file.
  • Dont forget to Add this provider. “org.m0skit0.android.hms.unity.provider.AnalyticsContentProvider”
Android Manifest File

4- Add configuration (agconnect-service.json) file

Update this two path

  • Open project_path\Assets\Huawei\agconnect.json replace with you downloanded from AGC.
  • Open project_path\Assets\Plugins\Android\assets\agconnect.json replace with you downloanded from AGC.(this path for Analytics provider)

Download configuration file from AGC

My Project >> Project Setting >> General Information >> App Information

agconnect-services.json

Do not forget to add Data storage location for your project.

My Project >> Project Setting >> General Information >> Project Information

Data Storage Location

Do not forget to sign your project and add certificate fingerprint on the AGC.

Create fingerprint command from your keystore file:

keytool -list -v -keystore yourkeyfile.keystore

5- Use AnalyticsManager prefabs

Drag and drop Analytics Manager prefabs to your project.

AnalyticsManager Prefabs

Analytics Manager prefabs has two manager scripts they will be initilize your Analytics and Crash services.

Do not change Prefabs names, because in code we are calling instance of prefabs object by their name.

Demo Project

Project settings configuration:

Build Setting >> Player Setting >> Project Settings

Other Settings

  • Script BackEnd >> Mono
  • Api Compatibility Level >> .Net 4.x
  • Minimum Api Level >> level 23

Publish Setting

  • Use you keystore file

In plugin we have demo scene for Analytics kit and Crash services. Upper side of the scene is for Analytics kit, down side of the scene for Crash services.

Analytics

Send Event button connectted to “AnalyticsDemoManager.SendEvet”.

Event ID, Key and Value should not be empty or null.

Demo Analytics Scene

Analytics Controller Script will get datas from scene.

AnalyticsManager Script invoke sdk functions. This sdk is using Huawei Analytics libraries.

Assets\Plugins\Android\hianalytics-5.0.3.300.aar

Plugin sdk also is open source, if you want you can add new features there.

Crash Services

Crash button directly invoke “CrashManager” scripts, just enabling crash services is enough to collect crash logs.

Sample Project Result

We have filled the scene for analytics event then we have clicked Send Event button.

Game Scene

We can check result simultaneously on AppGallert Connect.

Real-time overview

Display the events, user trend, popular events, and user attribute analysis in the last 30 minutes, and support top-N analysis by location, model, and app version, allowing you to dynamically access user behavior data.

Sign in to AppGallery Connect and click My projects.

  • Huawei Analytics >> Real-time overview:

Down side of Real-time overview page we can see our event.

Event Analysis

Crash kit testing results:

Click >> Enable Crash button.It will collect crash logs.

Crash Game >> Test it button. Game will be crash.

Logcat result:

AGC Crash Result:

Sign in to AppGallery Connect and click My projects.

  • Quality >> Crash

Click the Statistics tab and add filters or select a period in the upper right corner to filter crash data based on your needs. For example, if you triggered crashes on multiple devices to test the Crash service in the last 24 hours, you can select Last 24 hours in the upper right corner to check whether the crash data was reported.

AGC Crash Page

After confirming that crash data exists, click the Problems tab and select a period in the upper corner to check the crashes that took place during the period. For example, you can select Last 24 hours if your app crashed in the last 24 hours. Based on the crash time and quantity, you can find the specific crash. As shown in the following figure, java.lang.Error is the crash triggered during Crash service testing.

On the crash details page, analyze the crash trend based on the chart and view the cause of the crash on the Stack tab page. Below image show cause of the crash.

We crash application with Unity function : `Application.ForceCrash(0)`

After clicking “customReport” button, it will send custom crash report.

Querying Crash Information by Custom Key-Value Pair

  1. Click the Problems tab and click a crash for which you need to query information in the Problems column.
  2. Click the Status tab and enter a keyword in the search box to check the user status before and after the crash to further analyze the impact of the crash on users.
Custom Crash Report Result

Huawei Analytics Gradle Dependency

Check your libraries from “Assets\Plugins\Android”, if dependency not there you should add for Analytics kit. In plugin we add libraries with statics way. We can can use gradle approach also.

I’ll write about gradle integration in my next post.

Analytics kit 5.0.3.300 dependencies

--

--