We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Sunset Announcement: The HMS Toolkit service will no longer receive version updates. Services in overseas regions will be decommissioned on December 30, 2026, and will no longer be available after sunset.
Tools GuidesHMS ToolkitUsage GuideConvertor Manual Conversion GuideAnalytics Kit Manual Conversion Guide

Analytics Kit Manual Conversion Guide

Precautions

HMS Core Analytics SDK Overview

Analytics Kit brings you a range of preset analytics models so you can understand your users' behavior and gain a greater understanding of your users, products, and content. With this insight, you can take a data-driven approach to marketing your apps and optimizing your products.

Analytics Kit implements the following functions using data collected from apps:

  • Track and report on custom events.
  • Set up to 25 user attributes.
  • Automate event collection and session calculation; preset event IDs and parameters.

Data Model Comparison

The Google Analytics SDK encapsulates various event matching types, such as screen views, events, social networking, and e-commerce, for you to use. After you set the operation type of an event, track() is used to report the event.

The HMS Core Analytics SDK uses an event analysis data model, which classifies all events by event name and event parameter. The Event-Param-User model is used to record data, describes events using parameters, and describes the characteristics of users.

Event Mapping Reference

Expand

Google Analytics Product Action

HUAWEI Analytics Kit Event

ACTION_ADD

ADDPRODUCT2CART

ACTION_CHECKOUT

STARTCHECKOUT

ACTION_CHECKOUT_OPTION

UPDATECHECKOUTOPTION

ACTION_CLICK

VIEWCONTENT

ACTION_DETAIL

VIEWPRODUCT

ACTION_PURCHASE

COMPLETEPURCHASE

ACTION_REFUND

REFUNDORDER

ACTION_REMOVE

DELPRODUCTFROMCART

ACTION_VIEW

CREATEORDER

Event Property Mapping Reference

Expand

Google Analytics Property

HUAWEI Analytics Kit Property

category

CATEGORY

product_id

PRODUCTID

name

PRODUCTNAME

price

PRICE

quantity

QUANTITY

query

KEYWORDS

shipping

SHIPPING

tax

TAXFEE

order_id

TRANSACTIONID

currency

CURRNAME

couponCode

VOUCHER

If an event name (property) is not in the preceding mapping table, you can customize the event name and have it reported through onEvent(). For details about the property parameters for the preset event model, please refer to Predefined Events.

API Comparison

For details about the API usage of the Google Analytics SDK and HMS Core Analytics SDK, please refer to the API reference.

Expand

Google Analytics SDK

HMS Core Analytics SDK

Description

getInstance(Context context)

getInstance(Context context)

This API is called to obtain an instance.

enableAutoActivityReports(Application application)

setAnalyticsEnabled(boolean isEnabled)

This API is called to specify whether to enable data collection based on predefined tracing points. If the function is disabled, no data is recorded.

send(Map<String, String> params)

onEvent(String eventId, Bundle params)

This API is called to record customized events.

enableAutoActivityTracking(boolean enabled)

N/A

The HMS Core Analytics SDK automatically reports activity screen events.

HitBuilders.ScreenViewBuilder()

pageStart(String pageName, String pageClassOverride)

pageEnd(String pageName)

This API is called to customize the screen start and end event collection.

Migration Procedure

SDK Integration

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

Initialization

After configurations are imported successfully, initialize Analytics Kit in the onCreate method of the first Activity to obtain a HiAnalyticsInstance instance. If the agconnect-services.json file is incorrect, data cannot be collected at specified tracing points.

After obtaining the HiAnalyticsInstance instance, onEvent can be called to collect data at specified tracing points.

Collapse
Word wrap
Dark theme
Copy code
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. HiAnalyticsTools.enableLog();
  6. HiAnalyticsInstance instance = HiAnalytics.getInstance(this);
  7. Bundle bundle = new Bundle();
  8. bundle.putString(CATEGORY, "screen");
  9. bundle.putString(PRODUCTNAME, "screen name");
  10. instance.onEvent(VIEWPRODUCT, bundle);
  11. }

Data Model Migration

Convert the events defined by the Google Analytics SDK to the preset models in the HMS Core Analytics SDK based on the event model mapping references in Event Mapping Reference and Event Property Mapping Reference. For details about the event model parameters, please refer to Predefined Events.

The following is the sample code of migrating purchase event recording from the Google Analytics SDK to the HMS Core Analytics SDK.

Collapse
Word wrap
Dark theme
Copy code
  1. Context context = this;
  2. // Purchase event recording by the Google Analytics SDK.
  3. HitBuilders.ScreenViewBuilder builder = new HitBuilders.ScreenViewBuilder();
  4. builder.setProductAction(
  5. new ProductAction(ProductAction.ACTION_PURCHASE)
  6. .setTransactionId("TT-1234")
  7. .setTransactionRevenue(3.14)
  8. .setTransactionCouponCode("EXTRA100"))
  9. .addProduct(new Product().setId("PID-4321").setName("Water Monkeys!"));
  10. Tracker tracker = new Tracker(); // Obtain a tracker.
  11. tracker.send(builder.build());
  12. // Purchase event recording by the HMS Core Analytics SDK.
  13. Bundle purchaseBundle = new Bundle();
  14. purchaseBundle.putString(TRANSACTIONID, "TT-1234");
  15. purchaseBundle.putDouble(REVENUE, 3.14);
  16. purchaseBundle.putString(VOUCHER, "EXTRA100");
  17. HiAnalyticsInstance instance = HiAnalytics.getInstance(context);
  18. instance.onEvent(COMPLETEPURCHASE, purchaseBundle );

Precautions

  • By default, the HMS Core Analytics SDK automatically collects activity screen events. Therefore, you do not need to manually set tracing points after migration from the Google Analytics SDK to the HMS Core Analytics SDK. To collect non-activity screen events, use the pageStart(String pageName, String pageClassOverride) and pageEnd(String pageName) APIs.
  • The HMS Core Analytics SDK cannot automatically report uncaught exceptions. For function details, please refer to the Crash SDK.

AppMeasurementReceiver

For details about how to modify the AppMeasurementReceiver() and onReceive(Context context, Intent intent) methods, please refer to the following:

Before modification, ensure that Convertor has been used for conversion.

Before modification:

Collapse
Word wrap
Dark theme
Copy code
  1. import org.xms.g.measurement.AppMeasurementReceiver;
  2. ...
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. ...
  6. AppMeasurementReceiver receiver = new AppMeasurementReceiver();
  7. receiver.onReceive(context, intent);
  8. ...
  9. }

After modification:

Collapse
Word wrap
Dark theme
Copy code
  1. import com.google.android.gms.measurement.AppMeasurementReceiver;
  2. ...
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. ...
  6. AppMeasurementReceiver receiver;
  7. if (!org.xms.g.utils.GlobalEnvSetting.isHms()) {
  8. receiver = new AppMeasurementReceiver ();
  9. receiver.onReceive(context, intent);
  10. }
  11. ...
  12. }

AppMeasurementInstallReferrerReceiver

For details about how to modify the AppMeasurementInstallReferrerReceiver() and onReceive(Context context, Intent intent) methods, please refer to the following:

Before modification, ensure that Convertor has been used for conversion.

Before modification:

Collapse
Word wrap
Dark theme
Copy code
  1. import org.xms.g.measurement.AppMeasurementInstallReferrerReceiver;
  2. ...
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. ...
  6. AppMeasurementInstallReferrerReceiver receiver = new AppMeasurementInstallReferrerReceiver();
  7. receiver.onReceive(context, intent);
  8. ...
  9. }

After modification:

Collapse
Word wrap
Dark theme
Copy code
  1. import com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver;
  2. ...
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. ...
  6. AppMeasurementInstallReferrerReceiver receiver;
  7. if (!org.xms.g.utils.GlobalEnvSetting.isHms()) {
  8. receiver = new AppMeasurementInstallReferrerReceiver();
  9. receiver.onReceive(context, intent);
  10. }
  11. ...
  12. }

setCurrentScreen(Activity, String, String)

By default, the HMS Core Analytics SDK automatically collects activity screen events. Therefore, you do not need to manually set tracing points. To collect non-activity screen events, you are advised to use the pageStart(String pageName, String pageClassOverride) and pageEnd(String pageName) APIs.

Add HMS API

  • The following provides a code modification example for the scenario where the setCurrentScreen(Activity, String, String) API is used on the activity screen:
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // todo
    2. import com.google.firebase.analytics.FirebaseAnalytics;
    3. ...
    4. @Override
    5. protected void onResume() {
    6. super.onResume();
    7. ...
    8. // todo
    9. if (!org.xms.g.utils.GlobalEnvSetting.isHms()) {
    10. FirebaseAnalytics instanceFA = FirebaseAnalytics.getInstance(this);
    11. instanceFA.setCurrentScreen(this, "ScreenName", null);
    12. }
    13. ...
    14. }
  • The following provides a code modification example for the scenario where the setCurrentScreen(Activity, String, String) API is used on the non-activity screen:

    Before modification, ensure that Convertor has been used for conversion.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import org.xms.f.analytics.ExtensionAnalytics;
    2. ...
    3. @Override
    4. public void onResume() {
    5. super.onResume();
    6. ...
    7. ExtensionAnalytics instanceFA = ExtensionAnalytics.getInstance(this.getActivity());
    8. instanceFA.setCurrentScreen(this.getActivity(), "ScreenName", "com.test.Fragment");
    9. ...
    10. }

    After modification:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // todo
    2. import com.google.firebase.analytics.FirebaseAnalytics;
    3. import com.huawei.hms.analytics.HiAnalytics;
    4. import com.huawei.hms.analytics.HiAnalyticsInstance;
    5. ...
    6. @Override
    7. public void onResume() {
    8. super.onResume();
    9. ...
    10. // todo
    11. if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    12. HiAnalyticsInstance instanceHA = HiAnalytics.getInstance(this.getActivity());
    13. instanceHA.pageStart("ScreenName", "com.test.Fragment");
    14. } else {
    15. FirebaseAnalytics instanceFA = FirebaseAnalytics.getInstance(this.getActivity());
    16. instanceFA.setCurrentScreen(this.getActivity(), "ScreenName", "com.test.Fragment");
    17. }
    18. ...
    19. }
    20. ...
    21. @Override
    22. public void onPause() {
    23. super.onPause();
    24. ...
    25. // todo
    26. if (org.xms.g.utils.GlobalEnvSetting.isHms()) {
    27. HiAnalyticsInstance instanceHA = HiAnalytics.getInstance(this.getActivity());
    28. instanceHA.pageEnd("ScreenName");
    29. }
    30. ...
    31. }

To HMS API

  • The following provides a modification suggestion for the scenario where the setCurrentScreen(Activity, String, String) API is used on the activity screen:

    By default, the HMS Core Analytics SDK automatically collects activity screen events. Therefore, you do not need to manually set tracing points. You are advised to delete the code related to the setCurrentScreen(Activity, String, String) API.

  • The following provides a code modification example for the scenario where the setCurrentScreen(Activity, String, String) API is used on the non-activity screen:
    Before modification, ensure that Convertor has been used for conversion.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import com.google.firebase.analytics.FirebaseAnalytics;
    2. ...
    3. @Override
    4. public void onResume() {
    5. super.onResume();
    6. ...
    7. FirebaseAnalytics instanceFA = FirebaseAnalytics.getInstance(this.getActivity());
    8. instanceFA.setCurrentScreen(this.getActivity(), "ScreenName", "com.test.Fragment");
    9. ...
    10. }

    After modification:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import com.huawei.hms.analytics.HiAnalytics;
    2. import com.huawei.hms.analytics.HiAnalyticsInstance;
    3. ...
    4. @Override
    5. public void onResume() {
    6. super.onResume();
    7. ...
    8. // todo
    9. HiAnalyticsInstance instanceHA = HiAnalytics.getInstance(this.getActivity());
    10. instanceHA.pageStart("ScreenName", "com.test.Fragment");
    11. ...
    12. }
    13. ...
    14. @Override
    15. public void onPause() {
    16. super.onPause();
    17. ...
    18. // todo
    19. HiAnalyticsInstance instanceHA = HiAnalytics.getInstance(this.getActivity());
    20. instanceHA.pageEnd("ScreenName");
    21. ...
    22. }