[Huawei Developers] John Wo:
Hello everyone, in this article, provides example of HUAWEI Analytics Kit using the Cordova mobile application. But first, let me inform you about HUAWEI Analytics Kit a little.

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.

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 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

Sign in to AppGallery Connect and select My projects.

Click Add project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191044.94523768651331895388456371696110:50511125015541:2800:8B9FDEB71DBEBD0F2E4B0B0D005FC0786BAA31C798551AF1A32F0E0D83B90063.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191044.94523768651331895388456371696110:50511125015541:2800:8B9FDEB71DBEBD0F2E4B0B0D005FC0786BAA31C798551AF1A32F0E0D83B90063.png[/IMG]

Enter a project name and click OK.

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.

Sign in to AppGallery Connect and select My projects.

Click your project from the project list.

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191111.50878622845546811007239127420845:50511125015541:2800:1DB481D615901DD86FEBB407869575A3BA4804B7FBD419765304B9E8D2232332.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191111.50878622845546811007239127420845:50511125015541:2800:1DB481D615901DD86FEBB407869575A3BA4804B7FBD419765304B9E8D2232332.png[/IMG]

On the Add app page, enter app information.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112180427.89327748505979696088636448524682:50511125015541:2800:9A231FD65069801B621A12E3BAD5F403F276B6433C02821BFE54EF33BD112D12.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112180427.89327748505979696088636448524682:50511125015541:2800:9A231FD65069801B621A12E3BAD5F403F276B6433C02821BFE54EF33BD112D12.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112180527.82400331484249235066447039113403:50511125015541:2800:5569816067DD529BCD501CA6DF98872579EAAF66ED350D56BDD902B9E47B92BD.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112180527.82400331484249235066447039113403:50511125015541:2800:5569816067DD529BCD501CA6DF98872579EAAF66ED350D56BDD902B9E47B92BD.png[/IMG]

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191244.16696311097963642277170123596097:50511125015541:2800:1F7AF6DF3347A2112459D6929760D49A29BDADF6C2DE1F0EC183503347147659.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191244.16696311097963642277170123596097:50511125015541:2800:1F7AF6DF3347A2112459D6929760D49A29BDADF6C2DE1F0EC183503347147659.png[/IMG]

Generating a Signing SHA-256 Certificate Fingerprint

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191308.05626545107709838094354844138025:50511125015541:2800:850940A402F408828CCF29ACA3409AED14CCC90A45A7E2B14347941E25E163F4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191308.05626545107709838094354844138025:50511125015541:2800:850940A402F408828CCF29ACA3409AED14CCC90A45A7E2B14347941E25E163F4.png[/IMG]

App Information - iOS Platform

Please refer to Configuring App Information in AppGallery Connect and Enabling Huawei Analytics

Integrating the HMS Analytics Plugin

You can either install the plugin through npm or by downloading it from the download page, Cordova Analytics plugin.

Download Cordova Analytics Sample Project Code

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

Add the iOS platform to the Cordova project

Please refer to iOS App Development

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191342.96899993456646021346050111152074:50511125015541:2800:1A83EAC5E15EC6BE6EDA32E1601D62F0CEF37BACA63575262C493B5EC94B7D67.jpeg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191342.96899993456646021346050111152074:50511125015541:2800:1A83EAC5E15EC6BE6EDA32E1601D62F0CEF37BACA63575262C493B5EC94B7D67.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:

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="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191410.14063535745504600928806439124592:50511125015541:2800:42D4040E6BF6D4A168876F4BC2110CEE2D105D699876D8479273FDC927B58E0C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191410.14063535745504600928806439124592:50511125015541:2800:42D4040E6BF6D4A168876F4BC2110CEE2D105D699876D8479273FDC927B58E0C.png[/IMG]

To disable the debug mode

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191432.45724689794733875245657906471187:50511125015541:2800:FFD7081DDB666C5D90EA5604FEF4D94B44B8CAB1E47FEAB68701F63541AD0AA6.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191432.45724689794733875245657906471187:50511125015541:2800:FFD7081DDB666C5D90EA5604FEF4D94B44B8CAB1E47FEAB68701F63541AD0AA6.png[/IMG]

Viewing Debugging Event Details (Real-time Update)

Sign in to AppGallery Connect 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.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191448.87430823463736739717925038339414:50511125015541:2800:4E60E5B90F96B82B20AC6607F03E6326D0FEEE72E2D911976046D1FE981625AC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191448.87430823463736739717925038339414:50511125015541:2800:4E60E5B90F96B82B20AC6607F03E6326D0FEEE72E2D911976046D1FE981625AC.png[/IMG]

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191502.63299686352933648462766659459357:50511125015541:2800:26B572D8C6D21FA14D3136691388EB3BA47434FE1C75C4D36CB181193FD84C19.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/375/445/5190090000026375445.20201112191502.63299686352933648462766659459357:50511125015541:2800:26B572D8C6D21FA14D3136691388EB3BA47434FE1C75C4D36CB181193FD84C19.png[/IMG]

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

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

References

Stack Overflow is the best place for any programming questions. Be sure to tag your question with huawei-mobile-services.

GitHub is the official repository for these plugins, You can open an issue or submit your ideas.

Huawei Developer Forum HMS Core Module is great for general questions, or seeking recommendations and opinions.

Huawei Developer Docs 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 repository.