1e41f4b71Sopenharmony_ci# @ohos.hiAppEvent (应用打点)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供了应用事件打点能力,包括对打点数据的落盘,以及对打点功能的管理配置。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - 本模块接口从API version 9开始废弃,建议使用新接口[`@ohos.hiviewdfx.hiAppEvent`](js-apis-hiviewdfx-hiappevent.md)替代。
8e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport hiAppEvent from '@ohos.hiAppEvent';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## 使用说明
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci开发者在使用应用事件打点功能前,需要首先了解应用事件相关的参数规格定义。
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**事件名称**
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci事件名称为string类型,字符串非空且长度在48个字符以内,首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**事件类型**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci事件类型为[EventType](#eventtype)枚举类型。
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**事件参数**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci事件参数为object类型,key为事件的参数名称,value为事件的参数值,其规格定义如下:
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci- 参数名为string类型,字符串非空且长度在32个字符以内,首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符;
34e41f4b71Sopenharmony_ci- 参数值支持string、number、boolean、数组类型;
35e41f4b71Sopenharmony_ci- 参数值为string类型时,其长度需在8*1024个字符以内,超出会做丢弃处理;
36e41f4b71Sopenharmony_ci- 参数值为number类型时,其取值需在Number.MIN_SAFE_INTEGER~Number.MAX_SAFE_INTEGER范围内,超出可能会产生不确定值;
37e41f4b71Sopenharmony_ci- 参数值为数组类型时,数组中的元素类型只能全为string、number、boolean中的一种,且元素个数需在100以内,超出会做丢弃处理;
38e41f4b71Sopenharmony_ci- 参数个数需在32以内,超出的参数会做丢弃处理。
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci**事件回调**
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci开发者在调用事件打点方法后,可以在回调函数中对打点返回值进行处理,当前支持callback形式和Promise形式的回调,其返回值规格定义如下:
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci- 返回值为0,表示事件校验成功,将事件直接落盘到事件文件;
45e41f4b71Sopenharmony_ci- 返回值大于0,表示事件校验存在异常参数,在忽略异常参数后将事件落盘到事件文件;
46e41f4b71Sopenharmony_ci- 返回值小于0,表示事件校验失败,不将事件落盘到事件文件。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci## hiAppEvent.write
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ciwrite(eventName: string, eventType: EventType, keyValues: object, callback: AsyncCallback<void>): void
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci应用事件打点方法,将事件写入到当天的事件文件中,使用callback方式作为异步回调。
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**参数:**
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci| 参数名    | 类型                      | 必填 | 说明           |
59e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | -------------- |
60e41f4b71Sopenharmony_ci| eventName | string                    | 是   | 事件名称。     |
61e41f4b71Sopenharmony_ci| eventType | [EventType](#eventtype)   | 是   | 事件类型。     |
62e41f4b71Sopenharmony_ci| keyValues | object                    | 是   | 事件参数。     |
63e41f4b71Sopenharmony_ci| callback  | AsyncCallback<void> | 是   | 事件回调函数。 |
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci**示例:**
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci```ts
68e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_cilet eventParams: Record<string, number | string> = {
71e41f4b71Sopenharmony_ci  "int_data": 100,
72e41f4b71Sopenharmony_ci  "str_data": "strValue",
73e41f4b71Sopenharmony_ci};
74e41f4b71Sopenharmony_cihiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, eventParams, (err: BusinessError) => {
75e41f4b71Sopenharmony_ci  if (err) {
76e41f4b71Sopenharmony_ci    // 事件写入异常:事件存在异常参数时忽略异常参数后继续写入,或者事件校验失败时不执行写入
77e41f4b71Sopenharmony_ci    console.error(`failed to write event, code=${err.code}`);
78e41f4b71Sopenharmony_ci    return;
79e41f4b71Sopenharmony_ci  }
80e41f4b71Sopenharmony_ci  // 事件写入正常
81e41f4b71Sopenharmony_ci  console.log(`success to write event`);
82e41f4b71Sopenharmony_ci});
83e41f4b71Sopenharmony_ci```
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci## hiAppEvent.write
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciwrite(eventName: string, eventType: EventType, keyValues: object): Promise&lt;void&gt;
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci应用事件打点方法,将事件写入到当天的事件文件中,使用Promise方式作为异步回调。
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci**参数:**
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci| 参数名    | 类型                    | 必填 | 说明       |
97e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | ---------- |
98e41f4b71Sopenharmony_ci| eventName | string                  | 是   | 事件名称。 |
99e41f4b71Sopenharmony_ci| eventType | [EventType](#eventtype) | 是   | 事件类型。 |
100e41f4b71Sopenharmony_ci| keyValues | object                  | 是   | 事件参数。 |
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci**返回值:**
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci| 类型                | 说明                                                         |
105e41f4b71Sopenharmony_ci| ------------------- | ------------------------------------------------------------ |
106e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象,可以在其then()、catch()方法中分别对事件写入成功、写入异常的情况进行异步处理。 |
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**示例:**
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci```ts
111e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_cilet eventParams: Record<string, number | string> = {
114e41f4b71Sopenharmony_ci  "int_data": 100,
115e41f4b71Sopenharmony_ci  "str_data": "strValue",
116e41f4b71Sopenharmony_ci};
117e41f4b71Sopenharmony_cihiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, eventParams).then(() => {
118e41f4b71Sopenharmony_ci  // 事件写入正常
119e41f4b71Sopenharmony_ci  console.log(`success to write event`);
120e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
121e41f4b71Sopenharmony_ci  // 事件写入异常:事件存在异常参数时忽略异常参数后继续写入,或者事件校验失败时不执行写入
122e41f4b71Sopenharmony_ci  console.error(`failed to write event, code=${err.code}`);
123e41f4b71Sopenharmony_ci});
124e41f4b71Sopenharmony_ci```
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci## hiAppEvent.configure
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciconfigure(config: ConfigOption): boolean
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci应用事件打点配置方法,可用于配置打点开关、文件目录存储限额大小等功能。
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**参数:**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci| 参数名 | 类型                          | 必填 | 说明                     |
137e41f4b71Sopenharmony_ci| ------ | ----------------------------- | ---- | ------------------------ |
138e41f4b71Sopenharmony_ci| config | [ConfigOption](#configoption) | 是   | 应用事件打点配置项对象。 |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**返回值:**
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci| 类型    | 说明                                                        |
143e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------------- |
144e41f4b71Sopenharmony_ci| boolean | 配置结果,true&nbsp;表示配置成功,false&nbsp;表示配置失败。 |
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**示例:**
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci```ts
149e41f4b71Sopenharmony_ci// 配置应用事件打点功能开关
150e41f4b71Sopenharmony_cilet config1: hiAppEvent.ConfigOption = {
151e41f4b71Sopenharmony_ci  disable: true,
152e41f4b71Sopenharmony_ci};
153e41f4b71Sopenharmony_cihiAppEvent.configure(config1);
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci// 配置事件文件目录存储限额大小
156e41f4b71Sopenharmony_cilet config2: hiAppEvent.ConfigOption = {
157e41f4b71Sopenharmony_ci  maxStorage: '100M',
158e41f4b71Sopenharmony_ci};
159e41f4b71Sopenharmony_cihiAppEvent.configure(config2);
160e41f4b71Sopenharmony_ci```
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci## ConfigOption
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci此接口提供了应用打点的配置选项。
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci| 名称       | 类型    | 必填 | 说明                                                         |
169e41f4b71Sopenharmony_ci| ---------- | ------- | ---- | ------------------------------------------------------------ |
170e41f4b71Sopenharmony_ci| disable    | boolean | 否   | 应用打点功能开关。配置值为true表示关闭打点功能,false表示不关闭打点功能。 |
171e41f4b71Sopenharmony_ci| maxStorage | string  | 否   | 打点数据本地存储文件所在目录的配额大小,默认限额为“10M”。所在目录大小超出限额后会对目录进行清理操作,会按从旧到新的顺序逐个删除打点数据文件,直到目录大小不超出限额时停止。 |
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci## EventType
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci事件类型枚举。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci| 名称      | 值   | 说明           |
181e41f4b71Sopenharmony_ci| --------- | ---- | -------------- |
182e41f4b71Sopenharmony_ci| FAULT     | 1    | 故障类型事件。 |
183e41f4b71Sopenharmony_ci| STATISTIC | 2    | 统计类型事件。 |
184e41f4b71Sopenharmony_ci| SECURITY  | 3    | 安全类型事件。 |
185e41f4b71Sopenharmony_ci| BEHAVIOR  | 4    | 行为类型事件。 |
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci## Event
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci此接口提供了所有预定义事件的事件名称常量。
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| 名称                      | 类型   | 可读 | 可写 | 说明                 |
195e41f4b71Sopenharmony_ci| ------------------------- | ------ | ---- | ---- | -------------------- |
196e41f4b71Sopenharmony_ci| USER_LOGIN                | string | 是   | 否   | 用户登录事件。       |
197e41f4b71Sopenharmony_ci| USER_LOGOUT               | string | 是   | 否   | 用户登出事件。       |
198e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_START | string | 是   | 否   | 分布式服务启动事件。 |
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci## Param
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci此接口提供了所有预定义参数的参数名称常量。
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci| 名称                            | 类型   | 可读 | 可写 | 说明               |
208e41f4b71Sopenharmony_ci| ------------------------------- | ------ | ---- | ---- | ------------------ |
209e41f4b71Sopenharmony_ci| USER_ID                         | string | 是   | 否   | 用户自定义ID。     |
210e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_NAME        | string | 是   | 否   | 分布式服务名称。   |
211e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_INSTANCE_ID | string | 是   | 否   | 分布式服务实例ID。 |