--- title: AnalyticsApiClient Apex class slug: '3118' canonical_url: https://docs.coveo.com/en/3118/ collection: coveo-for-salesforce source_format: adoc --- # AnalyticsApiClient Apex class An Apex wrapper to send Usage Analytics events. > **Note** > > You should also look at the [`UsageAnalytics` Apex class](https://docs.coveo.com/en/2987/). ## Methods ### `logSearchEvent` To log a search event to the Coveo `UsageAnalytics` API, use the following sample: ```apex // Create a SearchEvent from a SearchApiResponse. CoveoV2.AnalyticsApi.SearchEvent mySearchEvent = new CoveoV2.AnalyticsApi.SearchEvent( '', '', '', '' ); // Create an AnalyticsApiClient CoveoV2.AnalyticsApiClient myAnalyticsClient = new CoveoV2.AnalyticsApiClient(); // Generate a search token to the currently linked Coveo organization. myAnalyticsClient.token = CoveoV2.Globals.generateSearchToken(); // Send the SearchEvent to the usage analytics API. CoveoV2.AnalyticsApiClient.Response resp = myAnalyticsClient.logSearchEvent(mySearchEvent, ''); ``` 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)). * `` by the unique value that identifies the user who performed search actions. It's contained in every response when a usage analytics event is logged, and keeps the same value throughout the visit. Leave the value empty to have one generated automatically at the first event of a visit (see [What's the client ID?](https://docs.coveo.com/en/masb0234/)). ### `logClickEvent` To log a click event to the Coveo `UsageAnalytics` API, use the following sample: ```apex // Create a ClickEvent from a SearchApiResult returned in a SearchApiResponse. CoveoV2.AnalyticsApi.ClickEvent myClickEvent = new CoveoV2.AnalyticsApi.ClickEvent( , '', '', '' ); // Create an AnalyticsApiClient CoveoV2.AnalyticsApiClient myAnalyticsClient = new CoveoV2.AnalyticsApiClient(); // Generate a search token to the currently linked Coveo organization. myAnalyticsClient.token = CoveoV2.Globals.generateSearchToken(); // Send the SearchEvent to the usage analytics API. CoveoV2.AnalyticsApiClient.Response resp = myAnalyticsClient.logClickEvent(myClickEvent, ''); ``` 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`, 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)). * `` by the unique value that identifies the user who performed search actions. It's contained in every response when a usage analytics event is logged, and keeps the same value throughout the visit. Leave the value empty to have one generated automatically at the first event of a visit (see [What's the client ID?](https://docs.coveo.com/en/masb0234/)). ### `logCustomEvent` To log a custom event to the Coveo `UsageAnalytics` API, use the following sample: ```apex CoveoV2.AnalyticsApi.CustomEvent myCustomEvent = new CoveoV2.AnalyticsApi.CustomEvent( '', '', '' ); // Create an AnalyticsApiClient CoveoV2.AnalyticsApiClient myAnalyticsClient = new CoveoV2.AnalyticsApiClient(); // Generate a search token to the currently linked Coveo organization. myAnalyticsClient.token = CoveoV2.Globals.generateSearchToken(); // Send the SearchEvent to the usage analytics API. CoveoV2.AnalyticsApiClient.Response resp = myAnalyticsClient.logCustomEvent(myCustomEvent, ''); ``` 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)). * `` by the unique value that identifies the user who performed search actions. It's contained in every response when a usage analytics event is logged, and keeps the same value throughout the visit. Leave the value empty to have one generated automatically at the first event of a visit (see [What's the client ID?](https://docs.coveo.com/en/masb0234/)).