--- title: UsageAnalytics Apex class slug: '2987' canonical_url: https://docs.coveo.com/en/2987/ collection: coveo-for-salesforce source_format: adoc --- # UsageAnalytics Apex class You can use the `UsageAnalytics` Apex class to send usage analytics data to Coveo. > **Note** > > You can also look at the [`AnalyticsApiClient` Apex class](https://docs.coveo.com/en/3118/). ## `AnalyticsApi` Sends usage analytics data to Coveo. ### Methods These methods exist for every specific event category listed below ([Search event](https://docs.coveo.com/en/2987#search-event), [Click event](https://docs.coveo.com/en/2987#click-event), and [Custom event](https://docs.coveo.com/en/2987#custom-event)). #### `withParam` Adds a parameter to the event by providing a key-value pair. ```apex CoveoV2.AnalyticsApi.SearchEvent = new CoveoV2.AnalyticsApi.SearchEvent( , '', '', '' ); .withParam('', ''); // Now MY_SEARCH_EVENT.params.get('FOO') => 'BAR' ``` Where you replace: * `` by the name of the search event. * `` by the response received and parsed from the Search API. It's the response to a query that have been sent (see [SearchApiResponse Apex class](https://docs.coveo.com/en/3117/)). * `` by the language from which the usage analytics event originates. * `` by the original basic query expression (q) in the corresponding search request. * `` by the type of operation that triggered the event (for example, `interfaceLoad`). * `` and `` by a key-value pair. #### `withParamsMap` Adds many parameters to the event by providing a map. ```apex .withParamsMap(new Map { 'foo' => 'bar' }); // Now MY_SEARCH_EVENT.params.get('foo') => 'bar' ``` Where you replace `` by the name of the search event. #### `withCustomData` Adds a key-value pair to the customData of the analytics event. ```apex .withCustomData('', ''); // Now MY_EVENT.params.get('customData') => Map that contains 'foo' => 'bar' ``` Where you replace: * `` by the name of the usage analytics event. * `` and `` by a key-value pair. #### `stringify` Stringifies the event into a JSON string for sending to the analytics API. ### Search event When an end user triggers a query to the Search API, a search event is recorded (see [Event categories](https://docs.coveo.com/en/2949#event-categories)). You can create a search event from a Search API response: ```apex CoveoV2.AnalyticsApi.SearchEvent mySearchEvent = new CoveoV2.AnalyticsApi.SearchEvent( , '', '', '' ); ``` Where you replace: * `` by the response received and parsed from the Search API. It's typically the result of a sent query (see [SearchApiResponse Apex class](https://docs.coveo.com/en/3117/)). * `` by the language of the search interface from which the search event originates (see [language (string)](https://docs.coveo.com/en/1502#language-string)). * `` by the original basic query expression (`q`) in the corresponding search request (see [queryText (string)](https://docs.coveo.com/en/1502#querytext-string)). * `` by the identifier of the end-user action that triggered a query and caused the search interface to log a usage analytics event (see [actionCause (string)](https://docs.coveo.com/en/1502#actioncause-string)). For more information, see a [complete example](https://docs.coveo.com/en/3118#logsearchevent). ### Click event When the end user opens or previews a result, the search interface logs a click event (see [Event categories](https://docs.coveo.com/en/2949#event-categories)). You can create a click event from a Search API response: ```apex CoveoV2.AnalyticsApi.ClickEvent myClickEvent = new CoveoV2.AnalyticsApi.ClickEvent( , '', '', '' ); ``` Where you replace: * `` by the SearchApiResult to build the event from. It's a `CoveoV2.SearchApiResult` that you can get from a `CoveoV2.SearchApiResponse`. * `` by the language of the search interface from which the click event originates (see [language (string)](https://docs.coveo.com/en/2064#language-string)). * `` by the unique identifier of the query which led the search interface to log the click event. It can be found in a `CoveoV2.SearchApiResponse` and it corresponds to the `searchUid`. Use the `searchUid` in the response of the corresponding search request to the Search API (see [searchQueryUid (string)](https://docs.coveo.com/en/2064#searchqueryuid-string)). * `` by the identifier of the end-user action that triggered a query and caused the search interface to log a usage analytics event (see [actionCause (string)](https://docs.coveo.com/en/2064#actioncause-string)). For more information, see a [complete example](https://docs.coveo.com/en/3118#logclickevent). ### Custom event Typically, when the end user interacts with the search interface, but that's neither a search nor a click, there should be a custom event (see [Event categories](https://docs.coveo.com/en/2949#event-categories)). You can create a custom event from a Search API response: ```apex CoveoV2.AnalyticsApi.CustomEvent myCustomEvent = new CoveoV2.AnalyticsApi.CustomEvent( '', '', '' ); ``` Where you replace: * `` by the custom event type (see [eventType (string)](https://docs.coveo.com/en/2650#eventtype-string)). * `` by the custom event value (see [eventValue (string)](https://docs.coveo.com/en/2650#eventvalue-string)). * `` by the language of the search interface from which the custom event originates (see [language (string)](https://docs.coveo.com/en/2650#language-string)). For more information, see a [complete example](https://docs.coveo.com/en/3118#logcustomevent).