1# Event Reporting 2 3HiAppEvent provides APIs for reporting events. 4 5## Available APIs 6 7For details about how to use the APIs, see [Application Event Logging](../reference/apis-performance-analysis-kit/js-apis-hiviewdfx-hiappevent.md). 8 9**Data Processor APIs** 10 11| API | Description | 12| ----------------------------------------- | ------------------------------------------------ | 13| addProcessor(processor: Processor): number | Adds a data processor for reporting events. | 14| removeProcessor(id: number): void | Removes a data processor. | 15 16**User ID APIs** 17 18| API | Description | 19| ------------------------------------------ | -------------------------------------------- | 20| setUserId(name: string, value: string): void | Sets a user ID. The data processor can carry the user ID when reporting an event. | 21| getUserId(name: string): string | Obtains a user ID that has been set. | 22 23**User Property APIs** 24 25| API | Description | 26| ------------------------------------------------ | ------------------------------------------------ | 27| setUserProperty(name: string, value: string): void | Sets a user property. The data processor can carry user properties when reporting events. | 28| getUserProperty(name: string): string | Obtains a user property. | 29 30## How to Develop 31 32The following describes how to log a button onclick event and enable the data processor to report the event. 33 341. In the **entry/src/main/ets/ pages/Index.ets** file, add the **addprocessorTest** button with **Onclick()** to add the data processor. **analytics_demo** is the data processor library preset in the device.<!--Del--> For details, see [HiAppEvent Data Processor Library](../../device-dev/subsystems/subsys-dfx-hiappevent-extend-so.md).<!--DelEnd--> The sample code is as follows: 35 36 ```ts 37 import { BusinessError } from '@kit.BasicServicesKit'; 38 import { hiAppEvent, hilog } from '@kit.PerformanceAnalysisKit'; 39 40 @Entry 41 @Component 42 struct Index { 43 @State message: string = 'Hello World' 44 45 processorId: number = -1 46 47 build() { 48 Row() { 49 Column() { 50 Text(this.message) 51 .fontSize(50) 52 .fontWeight(FontWeight.Bold) 53 54 Button("addProcessorTest").onClick(()=>{ 55 // In Onclick(), add a data processor. 56 let eventConfig: hiAppEvent.AppEventReportConfig = { 57 domain: 'button', 58 name: 'click', 59 isRealTime: true 60 }; 61 let processor: hiAppEvent.Processor = { 62 name: 'analytics_demo', 63 debugMode: true, 64 routeInfo: 'CN', 65 onStartReport: true, 66 onBackgroundReport: true, 67 periodReport: 10, 68 batchReport: 5, 69 userIds: ['testUserIdName'], 70 userProperties: ['testUserPropertyName'], 71 eventConfigs: [eventConfig] 72 }; 73 this.processorId = hiAppEvent.addProcessor(processor); 74 }) 75 } 76 .width('100%') 77 } 78 .height('100%') 79 } 80 } 81 ``` 82 832. In the **entry/src/main/ets/ pages/Index.ets** file, add the **userIdTest** button with **onClick()** to obtain the user ID. The sample code is as follows: 84 85 ```ts 86 Button("userIdTest").onClick(()=>{ 87 // Set the user ID in onClick(). 88 hiAppEvent.setUserId('testUserIdName', '123456'); 89 90 // Obtain the user ID set in onClick(). 91 let userId = hiAppEvent.getUserId('testUserIdName'); 92 hilog.info(0x0000, 'testTag', `userId: ${userId}`) 93 }) 94 ``` 95 963. In the **entry/src/main/ets/pages/Index.ets** file, add the **userPropertyTest** button with **onClick()** to obtain the user property. The sample code is as follows: 97 98 ```ts 99 Button("userPropertyTest").onClick(()=>{ 100 // Set the user property in onClick(). 101 hiAppEvent.setUserProperty('testUserPropertyName', '123456'); 102 103 // Obtain the user property in onClick(). 104 let userProperty = hiAppEvent.getUserProperty('testUserPropertyName'); 105 hilog.info(0x0000, 'testTag', `userProperty: ${userProperty}`) 106 }) 107 ``` 108 1094. In the **entry/src/main/ets/pages/index.ets** file, add the **writeTest** button with **onClick()** to enable an event to be logged when the button is clicked. The sample code is as follows: 110 111 ```ts 112 Button("writeTest").onClick(()=>{ 113 // In onClick(), use hiAppEvent.write() to log an event when the button is clicked. 114 let eventParams: Record<string, number> = { 'click_time': 100 }; 115 let eventInfo: hiAppEvent.AppEventInfo = { 116 // Define the event domain. 117 domain: "button", 118 // Define the event name. 119 name: "click", 120 // Define the event type. 121 eventType: hiAppEvent.EventType.BEHAVIOR, 122 // Define the event parameters. 123 params: eventParams, 124 }; 125 hiAppEvent.write(eventInfo).then(() => { 126 hilog.info(0x0000, 'testTag', `HiAppEvent success to write event`) 127 }).catch((err: BusinessError) => { 128 hilog.error(0x0000, 'testTag', `HiAppEvent err.code: ${err.code}, err.message: ${err.message}`) 129 }); 130 }) 131 ``` 132 1335. In the **entry/src/main/ets/pages/index.ets** file, add the **removeProcessorTest** button with **onClick()** to remove the data processor (the one added in step 2). The sample code is as follows: 134 135 ```ts 136 Button("removeProcessorTest").onClick(()=>{ 137 // In Onclick(), add removeProcessor(). 138 hiAppEvent.removeProcessor(this.processorId); 139 }) 140 ``` 141 1426. Click the **Run** button in DevEco Studio to run the project. Then, click the **addProcessorTest**, **userIdTest**, **userPropertyTest**, **writeTest**, and **removeProcessorTest** buttons one by one to trigger an event reporting. 143 144 Once the event handler receives the event data, you can view the following information in the **Log** window: 145 146 ```text 147 HiAppEvent success to write event 148 ``` 149