目录

HiAnalytics

Public Method Summary

Qualifier and Type

Method Name and Description

Promise<void>

setAnalyticsEnabled(flag: boolean)

Sets whether to enable event tracking.

Promise<void>

setUserId(userId:string)

Sets a user ID.

Promise<void>

setUserProfile(name:string,value:string)

Sets user attributes.

Promise<void>

setSessionDuration(sessionDuration: number)

Sets the session timeout interval.

Promise<void>

onEvent(eventId: string, params?: { [key: string]: any })

Records an event.

Promise<void>

pageStart(screenName:string)

Defines a page entry event.

Promise<void>

pageEnd(screenName:string)

Defines a page exit event.

Promise<void>

setAppVersion(version: string)

Sets the app version.

Promise<{ [key: string]: string | number }>

getUserProfiles(preDefined: boolean)

Obtains automatically collected or custom user attributes.

Promise<void>

setRestrictionEnabled(flag: boolean)

Sets whether to disable data analysis.

Promise<boolean>

isRestrictionEnabled()

Checks whether data analysis is disabled.

Promise<string>

getAAID()

Obtains the Anonymous Application ID (AAID).

Promise<void>

addDefaultEventParams(params?: { [key: string]: string | number | boolean | null })

Adds default event parameters.

Promise<void>

setRoutePolicy(id: string | null )

Sets a data processing location.

Promise<void>

setReportPolicies(policies: ReportPolicy)

Sets a reporting policy.

Promise<void>

onReport()

Reports all events immediately.

Public Methods

[h2]setAnalyticsEnabled

Method

setAnalyticsEnabled(flag: boolean): Promise<void>

Sets whether to enable event tracking. If the function is disabled, no data is recorded.

NOTE:
  • If you have called setRestrictionEnabled before, data analysis will not be enabled when you enable event tracking. To enable data analysis, you need to set the parameter of setRestrictionEnabled to false.
  • If you have not called setRestrictionEnabled before, data analysis will be enabled when you enable event tracking.

Parameters

Name

Description

flag

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.

...
agconnect.instance().configInstance(aGConnectServicesConfig);
window.analytics = agconnect.analytics();
window.analytics.setAnalyticsEnabled(false);

[h2]setUserId

Method

setUserId(userId:string): Promise<void>

Sets a user ID. 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

userId

User ID, a string containing a maximum of 256 characters. The value cannot be empty.

This anonymous user ID is associated with the user data by Analytics Kit. Only you can use this ID to trace a specific user, while Analytics Kit cannot.

NOTE:

When setting userId, 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.

[h2]setUserProfile

Method

setUserProfile(name:string,value:string): Promise<void>

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.

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

[h2]setSessionDuration

Method

setSessionDuration(sessionDuration: number): Promise<void>

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

sessionDuration

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

[h2]onEvent

Method

onEvent(eventId: string, params?: { [key: string]: any }): Promise<void>

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 key-value pairs cannot exceed 2048 and the length cannot exceed 204,800 characters. Each key in the key-value pairs can consist of digits, letters, and underscores (_), but cannot start with a digit.

[h2]pageStart

Method

pageStart(screenName:string): Promise<void>

Defines a page entry event. This method is usually used together with the pageEnd method.

Parameters

Name

Description

screenName

Name of the entry page.

[h2]pageEnd

Method

pageEnd(screenName:string): Promise<void>

Defines a page exit event. This method is usually used together with the pageStart method.

Parameters

Name

Description

screenName

Name of the exit page.

[h2]setAppVersion

Method

setAppVersion(version: string): Promise<void>

Sets the app version.

Parameters

Name

Description

version

App version. The maximum length is 100 characters.

[h2]getUserProfiles

Method

getUserProfiles(preDefined: boolean):Promise<{ [key: string]: string | number }>

Obtains automatically collected or custom user attributes.

NOTE:

This method cannot obtain the automatically collected attribute AAID.

Parameters

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

Type

Description

Promise<{ [key: string]: string | number }>

Automatically collected or custom user attributes.

[h2]setRestrictionEnabled

Method

setRestrictionEnabled(flag: boolean): Promise<void>

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

Parameters

Name

Description

flag

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

  • true: yes
  • false: no

[h2]isRestrictionEnabled

Method

isRestrictionEnabled(): Promise<boolean>

Checks whether data analysis is disabled.

Returns

Type

Description

Promise<boolean>

true if data analysis is disabled; false otherwise.

[h2]getAAID

Method

getAAID(): Promise<string>

Obtains the AAID from AppGallery Connect.

Returns

Type

Description

Promise<string>

Promise that is used to obtain the AAID.

[h2]addDefaultEventParams

Method

addDefaultEventParams(params?: { [key: string]: string | number | boolean | null }): Promise<void>

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, number, or Boolean type, or be null.

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.

setRoutePolicy

Method

setRoutePolicy (id: string | null ): Promise<void>

Sets a data processing location.

NOTE:
  • This method can take effect only after you have submitted a ticket online to apply for setting multiple data processing locations. For details, please refer to Submitting a Ticket Online.
  • To call the method for setting multiple data processing locations, use the latest SDK configuration. To obtain the latest SDK configuration, go to Project settings and download the agconnect-services.json file from the App information area.

Parameters

Name

Description

id

Data processing location.

The options are CN (China), DE (Germany), SG (Singapore), and RU (Russia).

NOTE:
  • The data processing location you set must have the data storage permission. Otherwise, data cannot be reported to it.
  • If this parameter is set to null, data will be reported only to the default processing location.

[h2]setReportPolicies

Method

setReportPolicies (policies: ReportPolicy): Promise<void>

Sets a reporting policy.

Parameters

Name

Description

policies

Reporting policy.

[h2]onReport

Method

onReport(): Promise<void>

Reports all events immediately.