AnalyticsApiClient Apex Class

Coveo for Salesforce 3.38 (August 2019)

An Apex wrapper to send Usage Analytics events.

You should also look at the UsageAnalytics Apex Class.

Methods

logSearchEvent

You can log a search event to the Coveo UsageAnalytics. You can use the following sample:

// Create a SearchEvent from a SearchApiResponse.
CoveoV2.AnalyticsApi.SearchEvent <MY_SEARCH_EVENT> = 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_VISITOR_ID>');

Where you replace:

  • <MY_SEARCH_EVENT> by the name of the search event.

  • <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_VISITOR_ID> by the unique identifier of the visitor who performed search actions. It’s contained in every response when a usage analytics 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 Visitor ID).

logClickEvent

You can log a click event to the Coveo UsageAnalytics API. You can use the following sample:

// Create a ClickEvent from a SearchApiResult returned in a SearchApiResponse.
CoveoV2.AnalyticsApi.ClickEvent <MY_CLICK_EVENT> = 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_VISITOR_ID>');

Where you replace:

  • <MY_CLICK_EVENT> by the name of the click event.

  • <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. We recommend that you 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_VISITOR_ID> by the unique identifier of the visitor who performed search actions. It’s contained in every response when a usage analytics 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 Visitor ID).

logCustomEvent

You can log a custom event to the Coveo UsageAnalytics API. You can use the following sample:

CoveoV2.AnalyticsApi.ClickEvent <MY_CUSTOM_EVENT> = new CoveoV2.AnalyticsApi.myCustomEvent(
    '<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_VISITOR_ID>');

Where you replace:

  • <MY_CUSTOM_EVENT> by the name of the custom event.

  • <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_VISITOR_ID> by the unique identifier of the visitor who performed search actions. It’s contained in every response when a usage analytics 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 Visitor ID).

Recommended Articles