Overview

Analytics Kit predefines rich analysis models to help you easily 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 will create a demo app based on the provided demo project. After creating a demo app, you will be able to:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate Analytics Kit, you must complete the following preparations:

For details, please refer to Preparations.
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project and select the app that needs to integrate the Analytics SDK.
  3. Select any menu under HUAWEI Analytics and click Enable Analytics service. (You must have the app management permission to perform this operation.)

  4. On the Project access settings page displayed, set the data processing location and complete other settings. Then click Finish.
  5. Download the configuration file.

Integrating the Analytics 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 click My projects.
    2. Find your project and select the app that needs to integrate the Analytics SDK.
    3. Go to Project settings > General information. In the App information area, download the agconnect-services.plist file.
    4. Copy the agconnect-services.plist file to your project's root directory.
    5. Set Bundle Identifier of the demo project to the value of package_name under client in the agconnect-services.plist file.
    6. If you have made any changes on the Project settings page, such as setting the data processing location or enabling or managing APIs, you need to download the latest agconnect-services.plist file and use it to replace the existing one in the root directory of your Xcode project.
  2. Integrate the Analytics SDK in CocoaPods mode.

    1. Create a Podfile.
      Start the CLI and run the cd command to go to the root directory of your Xcode project. Then, run the vim Podfile command to create a Podfile.
    2. Add the pod dependency 'HiAnalytics' to the Podfile.
      Sample code:
      target 'hmsAnalyticsKitDemo' do # Pods for hmsAnalyticsKitDemo pod 'HiAnalytics' end end
    3. Run the pod install command.
      $ pod install

In this section, you will try to build a demo with the question answering function to learn how to use the main APIs of Analytics Kit. We have prepared the code for you, meaning you only need to follow the instructions in each step below to learn how to use Analytics Kit to report the events you are interested in, such as answering questions, calculating scores, and setting user attributes.

Download

Modifying the PrefixHeader.pch Class

Import the HiAnalytics class to 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 the configuration. [HiAnalytics config]; // TODO: Enable event collection. [HiAnalytics setAnalyticsEnabled:YES]; // TODO: Configure reporting policies. [HiAnalytics setReportPolicies:@[[HAReportPolicy onMoveBackgroundPolicy],[HAReportPolicy onCacheThresholdPolicy:200]]]; return YES; }

Modifying the MainViewController Class

  1. Add the following code to implement the reportAnswerEvt method:
  2. - (void)reportAnswerEvt:(NSString *)answer { // TODO: Report a custom 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 custom event. [HiAnalytics onEvent:@"Answer" setParams:parms]; }
  3. Add the following code to implement the postScore method:
  4. - (IBAction)postScoreBtnClick:(UIButton *)sender { // TODO: Report a score through the kHAEventSubmitScore event. NSDictionary *parms = @{kHAParamScore:[NSNumber numberWithInteger:self.score]}; // Report a predefined event. [HiAnalytics onEvent:kHAEventSubmitScore setParams:parms]; [HToast showMessage:@"Post score success."]; }

Modifying the SettingViewController Class

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

    For details about APIs, please refer to the Analytics Kit API Reference.

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

  1. Start the app in the emulator.
    On the Main screen, if you tap TRUE or FALSE to answer a question, the custom Answer event will be reported. If you tap POST SCORE to post your score, the predefined $SubmitScore event will be reported. If you tap NEXT, the next question will be displayed. If you tap SETTINGS, the user attribute setting screen will be displayed. During the redirection to the user attribute setting screen, events such as screen entry and exit will be automatically collected.
  2. On the Settings screen, enter your favorite sport, and tap SAVE. Tap CLEAR CACHE if you want to clear the cache. After completing the setting, tap Main in the upper left corner to return to the initial screen.
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project, and click the app to view its data.
  3. Go to HUAWEI Analytics > Overview > Real-time overview.
  4. Check the data. For details, please refer to the Analytics Kit 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 reporting policies.

FAQs

How should I perform initialization?

Initialize Analytics Kit in the main thread by using the application:didFinishLaunchingWithOptions method in the AppDelegate.m file. Otherwise, the processing of automatically collected lifecycle events may be affected.

How does Analytics Kit identify users?

Analytics Kit identifies users through an Anonymous Application ID (AAID).

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:

What permissions are required for using Analytics Kit?

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

Why can't 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 overview page. Analysis results on other pages such as Event analysis and Launch 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:

Related documents
To download the sample code, please click the button below:

Download

Code copied