HUAWEI Analytics Kit predefines rich analytics models to help you clearly understand user behavior and gain in-depth insights into users, products, and content. As such, you can carry out data-driven operations and make informed decisions about app marketing and product optimization.
Analytics Kit implements the following functions using data collected from apps:

What You Will Create

In this codelab, you can call APIs in the specified way to collect and report the following user device information to the Analytics Kit cloud: device model, app name, package name, channel ID, app version number, operating system version, system language, manufacturer, screen width, screen height, and operation time (some of the information is automatically collected by internal APIs of the HMS Core Analytics SDK). Analytics Kit then conducts big data analytics based on the collected information.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate HUAWEI HMS Core services, you must complete the following preparations:

For details, please refer to Integrating the HMS Core SDK.

  1. Sign in to AppGallery Connect and select My projects.
  2. Click the app for which you need to enable Analytics Kit.
  3. Select any menu under HUAWEI Analytics and click Enable Analytics service. (You need the management permission to perform this operation.)
  4. 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 settings > Manage APIs and manually enable the API permission for Analytics Kit.
  5. Download the configuration file.

Integrating the HMS Core SDK into the Xcode Project in Cocoapods Mode

1. Add the AppGallery Connect configuration file of the app to your Xcode project.

1) Sign in to AppGallery Connect and select My projects.

2) Find your app project and click the app that needs to integrate the HMS Core SDK.

3) Go to Project Setting > Convention. In the App information area, download the agconnect-services.json file.

4) Copy the agconnect-services.json file to the app's root directory of your Xcode project.

5) Set Bundle Identifier in the demo to the value of package_name in the client field in the agconnect-services.plist file.

a) Before obtaining the agconnect-services.json file, ensure that you have enabled HUAWEI Analytics.

b) If you have made any changes on the Project settings page, such as setting the data storage location and enabling or managing APIs, you need to download the latest agconnect-services.json file and replace the existing file in the app's root directory.

2. Integrate the HMS Core SDK in Cocoapods mode.

1) Create a Podfile file.

Run the cd command to go to the root directory of your Xcode project. Then, run the vim Podfile command to create a Podfile file.

2) Add the pod on which the AppGallery Connect service depends.

Example :

target 'hmsAnalyticsKitDemo' do # Pods for hmsAnalyticsKitDemo pod 'HiAnalytics' end end

3) Run the pod install command to install the pod.

$ pod install

In the previous section, you have successfully integrated Analytics Kit. In this section, you will try to write a demo with the question answering function to learn how to use main APIs of Analytics Kit. You do not need to care about service logic. We have prepared ready-made code for you. You only need to follow the instructions in each step below to learn how to use Analytics Kit to report the events you care about, such as answering questions, calculating scores, and setting user attributes.

Download source code

Modifying the PrefixHeader.pch Class

Import the Analytics Kit header class into the PrefixHeader.pch class.

#ifndef PrefixHeader_pch #define PrefixHeader_pch // Include any system framework and library headers here that should be included in all compilation units. // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. #import <HiAnalytics/HiAnalytics.h> #import "HToast.h" #endif /* PrefixHeader_pch */

Modifying the AppDelegate Class

Add the following code to initialize Analytics Kit:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //TODO:Initialize configuration. [HiAnalytics config]; //TODO:Enable event collection. [HiAnalytics setAnalyticsEnabled:YES]; //TODO:Configure report policies. [HiAnalytics setReportPolicies:@[[HAReportPolicy onMoveBackgroundPolicy],[HAReportPolicy onCacheThresholdPolicy:200]]]; return YES; }

Modifying the MainViewController Class

  1. Add the following code to implement the reportAnswerEvt method:
    - (void)reportAnswerEvt:(NSString *)answer { // TODO: Report a customzied Event // Event Name: Answer // Event Parameters: // -- question: String // -- answer:String // -- answerTime: String NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *answerTime=[dateFormat stringFromDate:[NSDate date]]; NSDictionary *parms = @{@"question":self.questionsArray[self.curQuqestionIndex], @"answer":answer, @"answerTime":answerTime}; // Report a customzied Event [HiAnalytics onEvent:@"Answer" setParams:parms]; }
  2. Add the following code to implement the postScore method:
    - (IBAction)postScoreBtnClick:(UIButton *)sender { // TODO: Report score by using SUBMITSCORE Event NSDictionary *parms = @{kHAParamScore:[NSNumber numberWithInteger:self.score]}; // Report a preddefined Event [HiAnalytics onEvent:kHAEventSubmitScore setParams:parms]; [HToast showMessage:@"Post score success."]; }

Modifying the SettingViewController Class

  1. Add the following code to save user attributes:
    - (IBAction)saveBtnClick:(UIButton *)sender { [self.textField resignFirstResponder]; if (self.textField.text.length<=0) { [HToast showMessage:@"Please enter your favorite sport."]; return; } // TODO: save favorite sport by user setUserProperty [HiAnalytics setUserProfile:@"favor_sport" setValue:self.textField.text]; [HToast showMessage:@"Set user profile success."]; }
  2. Add the following code to clear the cache:
    - (IBAction)clearCacheBtnClick:(UIButton *)sender { // TODO: clear cached dada [HiAnalytics clearCachedData]; [HToast showMessage:@"Clear cache success."]; }

So far, your app has been equipped with the required service functions. Next, you will use Analytics Kit to report the events you care about.

For details about APIs, please refer to HUAWEI AnalyticsKit API Reference.

The demo provides an example of integrating Analytics Kit with an app. It provides two pages, one for question answering and one for user attribute setting, to demonstrate how to collect predefined events and custom events.

Start the app in the emulator.

On the Main page, if you touch TRUE or FALSE to answer a question, the custom Answer event is reported. If you touch POST SCORE to post your score, the predefined SubmitScore is reported. If you touch NEXT, the next question is displayed. If you touch SETTINGS, the user attribute setting page is displayed. During the redirection, events such as page exit and page entry are automatically collected.

After entering your favorite sport, click Save to save the settings. Touch CLEAR CACHE if you want to clear the cache; touch Back if you want to return to the initial page.

  1. Sign in to AppGallery Connect and select My projects.
  2. Click the app to manage.
  3. Go to HUAWEI Analytics > Overview > Real-time monitoring.
  4. Check the data. For details, please refer to Huawei Analytics Operation Guide.

App Debugging

During the development, you can use DebugView to view the event records in real time, observe the results, and adjust the event collection scheme.

Choose Product > Scheme > Edit Scheme from the Xcode menu. On the Arguments page, click + to add the -HADebugEnabled parameter. After the parameter is added, click Close to save the setting.


The debug mode remains enabled until you explicitly disable it by setting the -HADebugDisabled parameter.


Successfully reported data is displayed on Huawei Developers in real time, as shown in the figure below.

Add any of the following arguments:

For example, if you want to set the log level to HALogLevelDebug:

Choose Product > Scheme > Edit Scheme from the Xcode menu.

On the Arguments page, click + to add the -HALogLevelDebug parameter.

After the parameter is added, click Close to save the setting.


1. When should I perform initialization?

Initialize Analytics Kit in the main thread by using the application:didFinishLaunchingWithOptions method in the .m file of the AppDelegate class. Otherwise, the processing of lifecycle events on the automatic collection page may be affected.

2. How does Analytics Kit identify users?

Analytics Kit identifies users by their AAID.

3. When will the AAID be reset? How does Analytics Kit collect user statistics after AAID reset?

The AAID will be reset in the following scenarios:

4. What permissions are required for using Analytics Kit?

Network access permissions are required for Analytics Kit to report data to the AppGallery Connect. The Kit inherits the permissions from the app. That is, you do not need to set network permissions for the Kit separately.

5. Why cannot I view the analysis result of the data reported during the test?


(1) When app debugging is disabled, you can only view some analysis results on the Real-time monitoring page. Analysis results on the Event analysis and Activity analysis are available only after the corresponding data is processed on the next day.

(2) When app debugging is enabled, the reported test data will not be analyzed and can only be viewed on the App debugging page.

Well done! You have successfully completed this codelab and learned how to:

For more information, please click the following link:


You can click the button below to download the source code:

Download source code

Code copied