Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
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!
Class Info |
|---|
com.huawei.hms.analytics.HiAnalyticsInstance Provides public methods to report user behavior data. This class uses the singleton pattern. |
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) |
void | onEvent(String eventId, Bundle params) Records an event. |
void | Clears all the collected data in the local cache. |
getAAID() Obtains the Anonymous Application ID (AAID). | |
void | NOTICE This method has been deprecated. |
void | 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 | 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 | Obtains the processing location of the uploaded data. |
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
Name | Description |
|---|---|
enabled | Indicates whether to enable event tracking. The default value is true.
|
Sample code:
The following code is used to disable event tracking.
- HiAnalyticsInstance instance = HiAnalytics.getInstance(context);
- instance.setAnalyticsEnabled(false);
- val instance = HiAnalytics.getInstance(context)
- instance.setAnalyticsEnabled(false)
This method has been deprecated.
Method |
|---|
public void setAutoCollectionEnabled(boolean isEnabled) Sets whether to automatically enable collection of system events during initialization. |
Parameters
Name | Description |
|---|---|
isEnabled | Indicates whether to enable automatic collection of system events. The default value is true.
|
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
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. |
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
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. |
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
Name | Description |
|---|---|
token | Push token, a string containing a maximum of 256 characters. The value cannot be empty. |
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
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). |
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
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). |
Method |
|---|
public void setCurrentActivity(Activity activity, String activityName, String activityClassOverride)
|
Parameters
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. |
Method |
|---|
public void onEvent(String eventId, Bundle params) Records an event. |
Parameters
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
|
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. |
Method |
|---|
public Task<String> getAAID() Obtains the AAID. |
Returns
Type | Description |
|---|---|
Task that is used to obtain the AAID. For details about the Task class, please refer to Task<TResult>. |
This method has been deprecated.
Method |
|---|
public void regHmsSvcEvent() The Analytics SDK has been enabled to automatically bind the Analytics Kit app service. |
This method has been deprecated.
Method |
|---|
public void unRegHmsSvcEvent() The Analytics SDK has been enabled to automatically unbind the Analytics Kit app service. |
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
Name | Description |
|---|---|
preDefined | Indicates whether to obtain automatically collected or custom user attributes.
|
Returns
Type | Description |
|---|---|
Map<String, String> | Automatically collected or custom user attributes. |
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
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. |
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
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(). |
Method |
|---|
public void setReportPolicies(Set<ReportPolicy> policies) Sets automatic event reporting policies. |
Parameters
Name | Description |
|---|---|
policies | Policy for data reporting. Four policies are supported. One or more policies can be specified.
|
Sample code:
The following code is used to set ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY for event reporting.
- // Create a policy that is used to report an event upon app switching to the background.
- ReportPolicy moveBackgroundPolicy = ReportPolicy.ON_MOVE_BACKGROUND_POLICY;
- // Create a policy that is used to report an event at the specified interval.
- ReportPolicy scheduledTimePolicy = ReportPolicy.ON_SCHEDULED_TIME_POLICY;
- // Set the event reporting interval to 600 seconds.
- scheduledTimePolicy.setThreshold(600);
- Set<ReportPolicy> reportPolicies = new HashSet<>();
- // Add the ON_SCHEDULED_TIME_POLICY and ON_MOVE_BACKGROUND_POLICY policies.
- reportPolicies.add(scheduledTimePolicy);
- reportPolicies.add(moveBackgroundPolicy);
- // Set the ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY policies.
- instance.setReportPolicies(reportPolicies);
- // Create a policy that is used to report an event upon app switching to the background.
- val moveBackgroundPolicy = ReportPolicy.ON_MOVE_BACKGROUND_POLICY
- // Create a policy that is used to report an event at the specified interval.
- val scheduledTimePolicy = ReportPolicy.ON_SCHEDULED_TIME_POLICY
- // Set the event reporting interval to 600 seconds.
- scheduledTimePolicy.threshold = 600
- val reportPolicies = HashSet<ReportPolicy>()
- // Add the ON_SCHEDULED_TIME_POLICY and ON_MOVE_BACKGROUND_POLICY policies.
- reportPolicies.add(scheduledTimePolicy)
- reportPolicies.add(moveBackgroundPolicy)
- // Set the ON_MOVE_BACKGROUND_POLICY and ON_SCHEDULED_TIME_POLICY policies.
- instance.setReportPolicies(reportPolicies)
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
Name | Description |
|---|---|
isEnabled | Indicates whether to disable data analysis. The default value is false.
|
Sample code:
- // Disable data analysis and data sharing.
- HiAnalyticsInstance instance = HiAnalytics.getInstance(context);
- instance.setRestrictionEnabled(true);
-
- // Enable data analysis.
- instance.setRestrictionEnabled(false);
-
- // Enable data sharing.
- instance.setRestrictionShared (false);
- // Disable data analysis and data sharing.
- val instance = HiAnalytics.getInstance(context)
- instance.setRestrictionEnabled(true)
-
- // Enable data analysis.
- instance.setRestrictionEnabled(false)
-
- // Enable data sharing.
- instance.setRestrictionShared (false)
Method |
|---|
public boolean isRestrictionEnabled() Checks whether data analysis is disabled. |
Returns
Type | Description |
|---|---|
boolean | true if data analysis is disabled; false otherwise. |
Method |
|---|
public void setCollectAdsIdEnabled(boolean isEnabled) Sets whether to collect advertising IDs. NOTE
|
Parameters
Type | Description |
|---|---|
isEnabled | Indicates whether to collect advertising IDs. The default value is true.
|
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
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
|
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
|
Parameters
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. |
Method |
|---|
public void setPropertyCollection (String property, boolean enabled) Sets whether to collect system attributes. Currently, this method applies only to the userAgent attribute. NOTE
|
Parameters
Name | Description |
|---|---|
property | System attribute. Only userAgent is supported now. |
enabled | Indicates whether to collect system attributes. The default value is true.
|
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
Name | Description |
|---|---|
customReferrer | Custom referrer, a string containing a maximum of 256 characters. The value cannot be empty. |
Method |
|---|
public String getDataUploadSiteInfo() Obtains the processing location of the uploaded data. |
Returns
Type | Description |
|---|---|
String | Country or region code of the data processing location. The options are CN, SG, RU, and DE. |