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
Notice on Closing the Huawei Analytics Service Access Portal

Dear developer!

Due to business adjustments, we will close the entrance for Huawei Analytics services on June 30, 2024 and no longer support new user registration. Developers of projects that have already activated Huawei Analytics services are not affected and can still continue to use Huawei Analytics services. If there are any changes, we will notify them separately. Thank you for your support!

ReferencesAnalytics KitAndroid - Java/Kotlincom.huawei.hms.analyticsClass SummaryHiAnalyticsInstance

HiAnalyticsInstance

Expand

Class Info

com.huawei.hms.analytics.HiAnalyticsInstance

Provides public methods to report user behavior data. This class uses the singleton pattern.

Public Method Summary

Expand

Qualifier and Type

Method Name and Description

void

setAnalyticsEnabled(final boolean enabled)

Sets whether to enable event tracking.

void

setAutoCollectionEnabled(boolean isEnabled)

NOTICE

This method has been deprecated.

void

setUserId(String id)

Sets a user ID.

void

setUserProfile(String name, String value)

Sets user attributes.

void

setPushToken(String token)

Sets the push token, which is obtained using Push Kit.

void

setMinActivitySessions(final long milliseconds)

Sets the minimum interval for refreshing a session when your app is running in the background.

void

setSessionDuration(final long milliseconds)

Sets the session timeout interval.

void

setCurrentActivity(Activity activity, String activityName, String activityClassOverride)

NOTICE

This method has been deprecated. Use pageStart(String pageName, String pageClassOverride) and pageEnd(String pageName) instead.

void

onEvent(String eventId, Bundle params)

Records an event.

void

clearCachedData()

Clears all the collected data in the local cache.

Task<String>

getAAID()

Obtains the Anonymous Application ID (AAID).

void

regHmsSvcEvent()

NOTICE

This method has been deprecated.

void

unRegHmsSvcEvent()

NOTICE

This method has been deprecated.

Map<String, String>

getUserProfiles(boolean preDefined)

Obtains automatically collected or custom user attributes.

void

pageStart(String pageName, String pageClassOverride)

Defines a page entry event.

void

pageEnd(String pageName)

Defines a page exit event.

void

setReportPolicies(Set<ReportPolicy> policies)

Sets automatic event reporting policies.

void

setRestrictionEnabled(boolean isEnabled)

Sets whether to disable data analysis.

boolean

isRestrictionEnabled()

Checks whether data analysis is disabled.

void

setCollectAdsIdEnabled(boolean isEnabled)

Sets whether to collect advertising IDs.

void

addDefaultEventParams(final Bundle params)

Adds default event parameters.

void

setChannel(String channel)

Sets the app installation source.

void

setPropertyCollection(String property, boolean enabled)

Sets whether to collect system attributes.

void

setCustomReferrer(String customReferrer)

Sets a custom referrer.

String

getDataUploadSiteInfo()

Obtains the processing location of the uploaded data.

Public Methods

setAnalyticsEnabled

Expand

Method

public void setAnalyticsEnabled(final boolean enabled)

Sets whether to enable event tracking. If event tracking is disabled, no data is recorded or analyzed.

NOTE

Parameters

Expand

Name

Description

enabled

Indicates whether to enable event tracking. The default value is true.

  • true: yes
  • false: no

Sample code:

The following code is used to disable event tracking.

Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. HiAnalyticsInstance instance = HiAnalytics.getInstance(context);
  2. instance.setAnalyticsEnabled(false);
Collapse
Word wrap
Dark theme
Copy code
  1. val instance = HiAnalytics.getInstance(context)
  2. instance.setAnalyticsEnabled(false)

setAutoCollectionEnabled

NOTICE

This method has been deprecated.

Expand

Method

public void setAutoCollectionEnabled(boolean isEnabled)

Sets whether to automatically enable collection of system events during initialization.

Parameters

Expand

Name

Description

isEnabled

Indicates whether to enable automatic collection of system events. The default value is true.

  • true: yes
  • false: no

setUserId

Expand

Method

public void setUserId(String id)

Sets a user ID.

When this method is called, a new session will be generated if the old value of id is not empty and is different from the new value. If you do not want to use id to identify a user (for example, when a user signs out), you must set id to null.

The SDK does not save the user ID. You are advised to set a user ID each time after the app is launched.

Parameters

Expand

Name

Description

id

Anonymous user ID, a string containing a maximum of 256 characters. It is associated with the user data by Analytics Kit. Only you can use this ID to trace a specific user, while Analytics Kit cannot.

The value cannot be empty.

NOTE

When setting id, comply with the HUAWEI Analytics Service Agreement and avoid using the original ID that can be used to identify a user. You are advised to use a random ID.

setUserProfile

Expand

Method

public void setUserProfile(String name, String value)

Sets user attributes. The values of user attributes remain unchanged throughout the app lifecycle and during each session. 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.

Parameters

Expand

Name

Description

name

User attribute name, a string containing a maximum of 256 characters. The value cannot be empty. It can consist of digits, letters, and underscores (_) and must start with a letter.

value

User attribute value, a string containing a maximum of 256 characters.

NOTE
  • To delete user attributes, you can set value to null for these attributes when calling setUserProfile.
  • If you delete or add an attribute, you must do it in both the app and AppGallery Connect and associate the attribute based on the attribute name.

setPushToken

Expand

Method

public void setPushToken(String token)

Sets the push token. After obtaining a push token through Push Kit, call this method to save the push token so that you can use the audience defined by Analytics Kit to create HCM notification tasks.

Parameters

Expand

Name

Description

token

Push token, a string containing a maximum of 256 characters. The value cannot be empty.

setMinActivitySessions

Expand

Method

public void setMinActivitySessions(final long milliseconds)

Sets the minimum interval for refreshing a session when your app is running in the background. When the app is running in the background for a period of time longer than the specified threshold, a new session will be generated when the app switches to the foreground.

Parameters

Expand

Name

Description

milliseconds

Minimum interval for refreshing a session, in milliseconds.

The interval ranges from 5 seconds to 5 hours. If the specified value is beyond the value range, the boundary value is used. The default value is 30000 (that is, 30 seconds).

setSessionDuration

Expand

Method

public void setSessionDuration(final long milliseconds)

Sets the session timeout interval. A new session will be generated when an app is running in the foreground but the interval between two adjacent events exceeds the specified timeout interval.

Parameters

Expand

Name

Description

milliseconds

Session timeout interval, in milliseconds.

The interval ranges from 5 seconds to 5 hours. If the specified value is beyond the value range, the boundary value is used. The default value is 1800000 (that is, 30 minutes).

setCurrentActivity

NOTICE

This method has been deprecated. Use the pageStart and pageEnd APIs to collect complete entry and exit events of non-activity pages.

Expand

Method

public void setCurrentActivity(Activity activity, String activityName, String activityClassOverride)

  • Analytics Kit automatically collects the $CurActivityName parameter in the $EnterScreen and $ExitScreen events. To override the automatically collected $EnterScreen and $ExitScreen events, you can call this method in the onResume method of Activity and specify the page name (activityName, mandatory) and class name (activityClassOverride, optional). If activityClassOverride is null or left empty, Analytics Kit will obtain the class name based on the input activity.
  • You can also call this method to record non-activity page events (such as Fragment and custom View) and specify the page name (activityName, mandatory).

Parameters

Expand

Name

Description

activity

Current activity.

activityName

Name of the current activity page, a string containing a maximum of 256 characters. Its value cannot be empty.

activityClassOverride

Page class name, a string containing a maximum of 256 characters. This parameter is optional. If its value is set to null or left empty, the class name of the current activity will be used.

onEvent

Expand

Method

public void onEvent(String eventId, Bundle params)

Records an event.

Parameters

Expand

Name

Description

eventId

Event ID, a string containing a maximum of 256 characters. The value cannot be empty or the ID of an automatically collected event. It can consist of digits, letters, and underscores (_), but cannot contain spaces or start with a digit.

Example: event_description10

params

Information carried in an event. The number of built-in key-value pairs in the Bundle cannot exceed 2048 and the Bundle cannot exceed 204,800 characters.

NOTE
  • The Bundle supports the following types of values: Bundle, ArrayList, String, int, long, double, boolean, float, char, byte, and short. A key can consist of digits, letters, and underscores (_), but cannot start with a digit.
  • The outermost Bundle supports only one nested Bundle and one ArrayList object. Parameters of the nested Bundle support only basic data types, while elements in the ArrayList object support the Bundle type and basic data types. In addition, parameters of the Bundle in the ArrayList object support only basic data types, and the ArrayList object supports a maximum of 50 elements.

clearCachedData

Expand

Method

public void clearCachedData()

Clears all the collected data in the local cache, including data that failed to be sent.

Data configured through the following APIs will be cleared:

NOTE

Calling this method will reset the AAID. In Analytics Kit 6.0.0 and later versions, calling this method will also reset the user ID.

getAAID

Expand

Method

public Task<String> getAAID()

Obtains the AAID.

Returns

Expand

Type

Description

Task<String>

Task that is used to obtain the AAID. For details about the Task class, please refer to Task<TResult>.

regHmsSvcEvent

NOTICE

This method has been deprecated.

Expand

Method

public void regHmsSvcEvent()

The Analytics SDK has been enabled to automatically bind the Analytics Kit app service.

unRegHmsSvcEvent

NOTICE

This method has been deprecated.

Expand

Method

public void unRegHmsSvcEvent()

The Analytics SDK has been enabled to automatically unbind the Analytics Kit app service.

getUserProfiles

Expand

Method

public Map<String, String> getUserProfiles(boolean preDefined)

Obtains automatically collected or custom user attributes.

NOTE

This method cannot obtain the following automatically collected attributes: AAID and advertising ID.

Parameters

Expand

Name

Description

preDefined

Indicates whether to obtain automatically collected or custom user attributes.

  • true: Obtains automatically collected user attributes.
  • false: Obtains custom user attributes.

Returns

Expand

Type

Description

Map<String, String>

Automatically collected or custom user attributes.

pageStart

Expand

Method

public void pageStart(String pageName, String pageClassOverride)

Defines a page entry event. This method applies only to non-activity pages. After calling this method, you need to call the pageEnd method.

This method does not need to be called for activity pages because they support automatic collection. If it is called for an activity page, statistics on page entry and exit events will be inaccurate.

Parameters

Expand

Name

Description

pageName

Name of the current page, a string containing a maximum of 256 characters. The value cannot be empty.

pageClassOverride

Class name of the current page, a string containing a maximum of 256 characters. The value cannot be empty.

pageEnd

Expand

Method

public void pageEnd(String pageName)

Defines a page exit event. This method applies only to non-activity pages. Before calling this method, you need to call the pageStart method.

This method does not need to be called for activity pages because they support automatic collection. If it is called for an activity page, statistics on page entry and exit events will be inaccurate.

NOTE

Ensure that when your app enters from a custom page (for example, a Fragment page) to an activity page, the custom page is the last page that calls pageEnd.

Parameters

Expand

Name

Description

pageName

Name of the current page, a string containing a maximum of 256 characters. The value cannot be empty. It must be the same as the value of pageName passed in pageStart().

setReportPolicies

Expand

Method

public void setReportPolicies(Set<ReportPolicy> policies)

Sets automatic event reporting policies.

Parameters

Expand

Name

Description

policies

Policy for data reporting. Four policies are supported. One or more policies can be specified.

  • ON_APP_LAUNCH_POLICY: An event will be reported immediately when this policy is set. After that, an event will be reported each time the app is launched.
  • ON_MOVE_BACKGROUND_POLICY: An event will be reported when the app is switched to the background (including app exit).
  • ON_SCHEDULED_TIME_POLICY: An event will be reported at the specified interval, in seconds. The value ranges from 60 to 1800. The default value is 60. If the specified value is beyond the value range, the boundary value is used.
  • ON_CACHE_THRESHOLD_POLICY: An event will be reported when the number of cached events reaches the threshold. The value ranges from 30 to 1000. The default value is 30. If the specified value is beyond the value range, the boundary value is used.
NOTE
  • The preceding reporting policies take effect only when the debug mode is disabled.
  • The ON_MOVE_BACKGROUND_POLICY and ON_CACHE_THRESHOLD_POLICY policies are mandatory and take effect regardless of which policy is used. For the ON_CACHE_THRESHOLD_POLICY policy, you can change the threshold for reporting an event.
  • Event reporting policies will be updated if a policy setting API is called multiple times. Only the last policy set by the API is effective.
  • When reporting conditions specified by a policy are met and event reporting is triggered, the event will be cached locally if no network is available and will be reported again when reporting conditions are met next time.
  • The specified event reporting policies are saved persistently.

Sample code:

The following code is used to set ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY for event reporting.

Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. // Create a policy that is used to report an event upon app switching to the background.
  2. ReportPolicy moveBackgroundPolicy = ReportPolicy.ON_MOVE_BACKGROUND_POLICY;
  3. // Create a policy that is used to report an event at the specified interval.
  4. ReportPolicy scheduledTimePolicy = ReportPolicy.ON_SCHEDULED_TIME_POLICY;
  5. // Set the event reporting interval to 600 seconds.
  6. scheduledTimePolicy.setThreshold(600);
  7. Set<ReportPolicy> reportPolicies = new HashSet<>();
  8. // Add the ON_SCHEDULED_TIME_POLICY and ON_MOVE_BACKGROUND_POLICY policies.
  9. reportPolicies.add(scheduledTimePolicy);
  10. reportPolicies.add(moveBackgroundPolicy);
  11. // Set the ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY policies.
  12. instance.setReportPolicies(reportPolicies);
Collapse
Word wrap
Dark theme
Copy code
  1. // Create a policy that is used to report an event upon app switching to the background.
  2. val moveBackgroundPolicy = ReportPolicy.ON_MOVE_BACKGROUND_POLICY
  3. // Create a policy that is used to report an event at the specified interval.
  4. val scheduledTimePolicy = ReportPolicy.ON_SCHEDULED_TIME_POLICY
  5. // Set the event reporting interval to 600 seconds.
  6. scheduledTimePolicy.threshold = 600
  7. val reportPolicies = HashSet<ReportPolicy>()
  8. // Add the ON_SCHEDULED_TIME_POLICY and ON_MOVE_BACKGROUND_POLICY policies.
  9. reportPolicies.add(scheduledTimePolicy)
  10. reportPolicies.add(moveBackgroundPolicy)
  11. // Set the ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY policies.
  12. instance.setReportPolicies(reportPolicies)

setRestrictionEnabled

Expand

Method

public void setRestrictionEnabled(boolean isEnabled)

Sets whether to disable data analysis. The default value is false, which indicates that data analysis is enabled.

Parameters

Expand

Name

Description

isEnabled

Indicates whether to disable data analysis. The default value is false.

  • true: yes
  • false: no

Sample code:

Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. // Disable data analysis and data sharing.
  2. HiAnalyticsInstance instance = HiAnalytics.getInstance(context);
  3. instance.setRestrictionEnabled(true);
  4. // Enable data analysis.
  5. instance.setRestrictionEnabled(false);
  6. // Enable data sharing.
  7. instance.setRestrictionShared (false);
Collapse
Word wrap
Dark theme
Copy code
  1. // Disable data analysis and data sharing.
  2. val instance = HiAnalytics.getInstance(context)
  3. instance.setRestrictionEnabled(true)
  4. // Enable data analysis.
  5. instance.setRestrictionEnabled(false)
  6. // Enable data sharing.
  7. instance.setRestrictionShared (false)

isRestrictionEnabled

Expand

Method

public boolean isRestrictionEnabled()

Checks whether data analysis is disabled.

Returns

Expand

Type

Description

boolean

true if data analysis is disabled; false otherwise.

setCollectAdsIdEnabled

Expand

Method

public void setCollectAdsIdEnabled(boolean isEnabled)

Sets whether to collect advertising IDs.

NOTE
  • To set whether to collect advertising IDs, you can also add the meta-data configuration to set the value of analyticskit_adsid_collection_enabled in the AndroidManifest.xml file. For details, please refer to (Optional) Configuring the AndroidManifest.xml File.
  • The value set through this method has a higher priority than that set in the AndroidManifest.xml file.

Parameters

Expand

Type

Description

isEnabled

Indicates whether to collect advertising IDs. The default value is true.

  • true: yes
  • false: no

addDefaultEventParams

Expand

Method

public void addDefaultEventParams(final Bundle params)

Adds default event parameters. These parameters will be added to all events except the automatically collected events. If the name of a default event parameter is the same as that of an event parameter, the event parameter will be used.

Parameters

Expand

Name

Description

params

Default event parameters.

A maximum of 100 key-value pairs are supported. The key in each key-value pair can contain a maximum of 256 characters and can consist of only digits, letters, and underscores (_), but cannot start with a digit. The value in each key-value pair can contain a maximum of 256 characters, and can only be of the string, int, long, double, boolean, float, char, byte, or short type.

NOTE
  • If params is set to null, all cached default event parameters will be cleared. If the value in a passed key-value pair is null, the corresponding default event parameter will be deleted. If the key in a passed key-value pair already exists, the corresponding default event parameter will be updated.
  • The number and value length of default event parameters will be counted in onEvent, so pay attention to the parameter usage restrictions.

setChannel

Expand

Method

public void setChannel(String channel)

Sets the app installation source. The setting takes effect only when the method is called for the first time.

NOTE
  • To set the app installation source, you can also add the meta-data configuration to set the value of install_channel in the AndroidManifest.xml file. For details, please refer to Procedure.
  • If you have already set an installation source in the AndroidManifest.xml file, the value of channel set through this method will not take effect.

Parameters

Expand

Name

Description

channel

App installation source, a string containing a maximum of 128 characters. The value cannot be empty. The value can consist of only letters, digits, underscores (_), hyphens (-), and spaces. It cannot start or end with a space.

setPropertyCollection

Expand

Method

public void setPropertyCollection (String property, boolean enabled)

Sets whether to collect system attributes. Currently, this method applies only to the userAgent attribute.

NOTE
  • To disable system attribute collection, you can also add the meta-data configuration to set the value of analyticskit_properties_collection_disabled in the AndroidManifest.xml file. For details, please refer to (Optional) Configuring the AndroidManifest.xml File.
  • The value set through this method has a higher priority than that set in the AndroidManifest.xml file.

Parameters

Expand

Name

Description

property

System attribute. Only userAgent is supported now.

enabled

Indicates whether to collect system attributes. The default value is true.

  • true: yes
  • false: no

setCustomReferrer

Expand

Method

public void setCustomReferrer(String customReferrer)

Sets a custom referrer.

NOTE

This method takes effect only when it is called for the first time.

Parameters

Expand

Name

Description

customReferrer

Custom referrer, a string containing a maximum of 256 characters. The value cannot be empty.

getDataUploadSiteInfo

Expand

Method

public String getDataUploadSiteInfo()

Obtains the processing location of the uploaded data.

Returns

Expand

Type

Description

String

Country or region code of the data processing location. The options are CN, SG, RU, and DE.

Search in References
Enter a keyword.