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