[Huawei Developers] mukremin_bagci:
Hello everyone, in this article, provides example of HUAWEI Analytics Kit("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/introduction-0000001050134725") using the Cordova mobile application. But first, let me inform you about HUAWEI Analytics Kit a little.

[IMG="HUAWEI Analytics Kit"]https://miro.medium.com/max/700/1*FTevL7Ai9S7HgbNEC0gVPw.jpeg[/IMG]

About HUAWEI Analytics Kit

Offers a rich array of preset analytics models that help you gain a deeper insight into your users, products, and content.

Helps you gain insight into how your users behave on different platforms based on the user behavior events and user attributes reported by your app.

Diverse analytics models: analyzes events, behavior, audiences, funnels, retention, attribution, real-time data.

App Debugging: During app development, the product manager and technical team can cooperate with each other through app debugging to verify data reporting, preventing missing events and event attribute errors.

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

With these insights, you can then take a data-driven approach to make informed decisions for product and marketing optimizations.

[IMG="HUAWEI Analytics Kit Advantages"]https://miro.medium.com/max/700/1*rq82TjI2l_0oWDT4GYfd8w.png[/IMG]

Privacy Statement: HUAWEI Analytics Kit does not collect users’ personal data. If your app needs to collect user data, it must do so in accordance with all applicable laws and regulations. You’ll also need to display a privacy statement, so users understand how you’re planning to use their data.

Integrating the AppGallery Connect SDK

Integrating AppGallery Connect("https://developer.huawei.com/consumer/en/service/josp/agc/index.html") is a prerequisite for using HUAWEI Analytics Kit is offering in your application. If you want to use the AppGallery Connect Analytics service, you must first have a AppGallery Connect developer account and integrate HMS Core in your application. You need to create an project from your developer account and then integrate the Cordova HMS Analytics Plugin into your project.

Creating an AppGallery Connect Project

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

2. Click Add project.

[IMG="AppGallery Connect Add Project"]https://miro.medium.com/max/700/1*NKmKm_BzNxSVYKpHB69lNg.png[/IMG]

3. Enter a project name and click OK.

4. After the project is created, the Project settings page is displayed. You need to add an app to the project.

Adding an App to the Project

Before you use development capabilities provided by AppGallery Connect, you need to add an app to your project first.

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

2. Click your project from the project list.

3. Go to Project settings > General information, and click Add app.

[IMG="AppGallery Connect Add App"]https://miro.medium.com/max/677/1*VU2qMHyU8VXI1rEupULv1w.png[/IMG]

4. On the Add app page, enter app information.

[IMG="Platform Android"]https://miro.medium.com/max/700/1*_jRZTVzHK2jDsk1VD9vbCA.png[/IMG]

[IMG="Platform iOS"]https://miro.medium.com/max/700/1*fIn_eFkYjmklDw7JQyNoIA.png[/IMG]

5. On the Project settings page, enter SHA-256 certificate fingerpring and then download the configuration file agconnect-services.json for Android platform.

[IMG="App Information - Android Platform"]https://miro.medium.com/max/700/1*xo9FyIQCy0BTi4edFFNkIQ.png[/IMG]

Generating a Signing SHA-256 Certificate Fingerprint("https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/game-preparation-v4#certificate")

6. On the Project settings page, download the configuration file agconnect-services.plist for iOS platform.

[IMG="App Information - iOS Platform"]https://miro.medium.com/max/700/1*p5hmsMU0NOCN9ZbCHY2pag.png[/IMG]

Please refer to Configuring App Information in AppGallery Connect and Enabling Huawei Analytics("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/config-agc-0000001050134733")

Integrating the HMS Analytics Plugin

You can either install the plugin through npm or by downloading it from the download page, Cordova Analytics plugin("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Library-V1/cordova-sdk-download-0000001050134773-V1").

Download Cordova Analytics Sample Project Code("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Examples/cordova-sample-code-0000001051065444")

Run the following command in the project root directory of your Cordova project to install it through npm.

cordova plugin add @hmscore/cordova-plugin-hms-analytics

# install the plugin manually after downloading plugin
cordova plugin add <CORDOVA_ANALYTICS_PLUGIN_PATH>

Add the Android platform to the Cordova project

Please refer to Android App Development("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/integrating-analytics-plugin-0000001059115018#EN-US_TOPIC_0000001059115018__section1258032454214")

Add the iOS platform to the Cordova project

Please refer to iOS App Development("https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/integrating-analytics-plugin-0000001059115018#EN-US_TOPIC_0000001059115018__section55836242421")

[IMG="Android and iOS Applications"]https://miro.medium.com/max/700/1*eUy1-E3EebtcyfdljvTxeA.jpeg[/IMG]

Using Debug Mode

During the development, you can enable the debug mode to view the event records in real time, observe the results, and adjust the event reporting policies.

Enabled Debug Mode and after then the data is successfully reported, you can go to HUAWEI Analytics > App debugging to view the reported data, as shown in the following figure.

Android Platform

Run the following command to enable the debug mode:

adb shell setprop debug.huawei.hms.analytics.app package_name

Run the following command to disable the debug mode:

adb shell setprop debug.huawei.hms.analytics.app .none.

iOS Platform

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

To enable the debug mode: 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.

[IMG="iOS Debug Mode Enabled"]https://miro.medium.com/max/700/1*l6MDyEzxkg0D-k88hY7zAQ.png[/IMG]

To disable the debug mode

[IMG="iOS Debug Mode Disabled"]https://miro.medium.com/max/700/1*8-gGOxuzlm78lidbeODnjA.png[/IMG]

Viewing Debugging Event Details (Real-time Update)

Sign in to AppGallery Connect("https://developer.huawei.com/consumer/en/service/josp/agc/index.html") and click My projects.

Find your project, and click the app for which you want to view analytics data.

Go to HUAWEI Analytics > App debugging.

The App debugging page displays events reported by the app in the last 60 seconds or last 30 minutes.

If you select Last 60 seconds, the displayed page is as follows.

If you select Last 30 minutes, the displayed page is as follows.

What is AAID(Anonymous Application ID)?

Anonymous device ID opened to third-party apps. Each app is allocated with a unique AAID on the same device so that statistics can be collected and analyzed for different apps (for example, statistics on the number of active users). In addition, personal data from different apps is isolated to protect user data privacy and security.

/**
* Obtains the app instance ID from AppGallery Connect.
*/
async function onGetAAID() {
try {
const aaid = await HMSAnalytics.getAAID();
alert('getAAID -> Success -> aaid : ' + aaid);
} catch (err) {
alert('getAAID -> Error : ' + err);
}
}

How to records event?

Records custom event

Such events can be used to meet personalized analysis requirements that cannot be met by automatically collected events and predefined events.

Note: The ID of a custom event cannot be the same as that of a predefined event. Otherwise, the custom event will be identified as a predefined event.

/**
* Report custom events.
*/
async function onEvent() {
const name = 'event_name';
const value = {
"my_event_key": "my_event_value",
"my_event_key_two": "my_event_value_two"
};
try {
const event = await HMSAnalytics.onEvent(name, value);
alert('onEvent -> Success');
} catch (err) {
alert('onEvent -> Error : ' + err);
}
}

Records predefined event

Such events have been predefined by the HMS Core Analytics SDK based on common application scenarios. It is recommended you use predefined event IDs for event collection and analysis.

/**
* Report predefined events.
*/
async function onSendPredefinedEvent() {
const event_name = HMSAnalytics.HAEventType.SUBMITSCORE;
const event_value = {}
event_value[HMSAnalytics.HAParamType.SCORE] = 12;
event_value[HMSAnalytics.HAParamType.CATEGORY] = "SPORT";
try {
const event = await HMSAnalytics.onEvent(event_name, event_value);
alert('sendPredefinedEvent -> Success');
} catch (err) {
alert('sendPredefinedEvent -> Error : ' + err);
}
}

Please refer to Event Management("https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/meta-manage-0000001050985177-V5#EN-US_TOPIC_0000001058394233__section6951855205619")

Setting User Profiles

Sets user attributes. The values of user attributes remain unchanged throughout the app lifecycle and during each session.

Note: A maximum of 25 user attributes are supported. If the name of an attribute set later is the same as that of an existing attribute, the value of the existing attribute is updated.

async function onSetUserProfile() {
const userProfileName = "user_profile_name";
const userProfileValue = "user_profile_value";
try {
const setUserProfile = await HMSAnalytics.setUserProfile(userProfileName, userProfileValue);
alert('setUserProfile -> Success');
} catch (err) {
alert('setUserProfile -> Error : ' + err);
}
}

When is the clearCacheData() method used?

It is used when deletes all collected data cached locally, including cached data that failed to be sent.

Note: AAID will be reset. Custom user attributes will be delete.

async function onClearCachedData() {
try {
const clearCachedData = await HMSAnalytics.clearCachedData();
alert('clearCachedData -> Success');
} catch (err) {
alert('clearCachedData -> Error : ' + err);
}
}

How to define a custom page?

Customizes a page entry event. The API applies only to non-activity pages because automatic collection is supported for activity pages.

Note: If this API is called for an activity page, statistics on page entry and exit events will be inaccurate.

Defines a custom page

After this pageStart() method is called, the pageEnd() API must be called.

/**
* Defines a custom page entry event.
* @note This method is only to support on Android Platform.
*/
async function onPageStart() {
const start_page_name = "start_page_name";
const start_page_class_override = "start_page_class_override";
try {
const pageStart = await HMSAnalytics.pageStart(start_page_name, start_page_class_override);
alert('pageStart -> Success');
} catch (err) {
alert('pageStart -> Error : ' + err);
}
}

Before this pageEnd() method is called, the pageStart() API must be called.

/**
* Defines a custom page exit event.
* @note This method is only to support on Android Platform.
*/
async function onPageEnd() {
const end_page_name = "start_page_name";
try {
const pageEnd = await HMSAnalytics.pageEnd(end_page_name)
alert('pageEnd -> Success' + pageEnd);
} catch (err) {
alert('pageEnd -> Error : ' + err);
}
}

Conclusion

In this article you have learned how to integrate HMS Analytics to your Cordova projects, record custom events and monitor them in AppGallery Connect. You can use custom events with user attributes in your apps to see user behaviors, so that you can improve your app depend on them.

Thank you!

Other Huawei Developers Cordova Medium Publications("https://medium.com/huawei-developers/tagged/cordova")

References

Stack Overflow("https://stackoverflow.com/questions/tagged/huawei-mobile-services") is the best place for any programming questions. Be sure to tag your question with huawei-mobile-services.

GitHub("https://github.com/HMS-Core/hms-cordova-plugin") is the official repository for these plugins, You can open an issue or submit your ideas.

Huawei Developer Forum("https://forums.developer.huawei.com/forumPortal/en/home?fid=0101187876626530001") HMS Core Module is great for general questions, or seeking recommendations and opinions.

Huawei Developer Docs("https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin") is place to official documentation for all HMS Core Kits, you can find detailed documentations in there.

If you run into a bug in our samples, please submit an issue to the GitHub("https://github.com/HMS-Core/hms-cordova-plugin") repository.