AnalyticsApiClient Apex class

In this article

An Apex wrapper to send Usage Analytics events.

Note

You should also look at the UsageAnalytics Apex class.

Methods

logSearchEvent

To log a search event to the Coveo UsageAnalytics API, use the following sample:

// Create a SearchEvent from a SearchApiResponse.
CoveoV2.AnalyticsApi.SearchEvent mySearchEvent = new CoveoV2.AnalyticsApi.SearchEvent(
    '<MY_SEARCH_API_RESPONSE>',
    '<LANGUAGE>',
    '<QUERY_TEXT>',
    '<ACTION_CAUSE>'
);
// 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, '<MY_CLIENT_ID>');

Where you replace:

  • <MY_SEARCH_API_RESPONSE> by the response received and parsed from the Search API. It’s typically the result of a sent query (see SearchApiResponse Apex class).

  • <LANGUAGE> by the language of the search interface from which the search event originates (see language (string)).

  • <QUERY_TEXT> by the original basic query expression (q) in the corresponding search request (see queryText (string)).

  • <ACTION_CAUSE> 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)).

  • <MY_CLIENT_ID> 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?).

logClickEvent

To log a click event to the Coveo UsageAnalytics API, use the following sample:

// Create a ClickEvent from a SearchApiResult returned in a SearchApiResponse.
CoveoV2.AnalyticsApi.ClickEvent myClickEvent = new CoveoV2.AnalyticsApi.ClickEvent(
    <MY_SEARCH_API_RESULT>,
    '<LANGUAGE>',
    '<MY_SEARCH_QUERY_UID>',
    '<ACTION_CAUSE>'
);
// 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, '<MY_CLIENT_ID>');

Where you replace:

  • <MY_SEARCH_API_RESULT> by the SearchApiResult to build the event from. It’s a CoveoV2.SearchApiResult that you can get from a CoveoV2.SearchApiResponse.

  • <LANGUAGE> by the language of the search interface from which the click event originates (see language (string)).

  • <MY_SEARCH_QUERY_UID> 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)).

  • <ACTION_CAUSE> 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)).

  • <MY_CLIENT_ID> 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?).

logCustomEvent

To log a custom event to the Coveo UsageAnalytics API, use the following sample:

CoveoV2.AnalyticsApi.CustomEvent myCustomEvent = new CoveoV2.AnalyticsApi.CustomEvent(
    '<MY_EVENT_TYPE>',
    '<MY_EVENT_VALUE>',
    '<LANGUAGE>'
);
// 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, '<MY_CLIENT_ID>');

Where you replace:

  • <MY_EVENT_TYPE> by the custom event type (see eventType (string)).

  • <MY_EVENT_VALUE> by the custom event value (see eventValue (string)).

  • <LANGUAGE> by the language of the search interface from which the custom event originates (see language (string)).

  • <MY_CLIENT_ID> 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?).