---
title: Leverage the Coveo UA library
slug: '1818'
canonical_url: https://docs.coveo.com/en/1818/
collection: build-a-search-ui
source_format: adoc
---
# Leverage the Coveo UA library
[Coveo Analytics](https://docs.coveo.com/en/182.md) lets you track user interactions with your [search interfaces](https://docs.coveo.com/en/2741.md) so that you can optimize your Coveo solution.
Search, click, and custom [events](https://docs.coveo.com/en/260.md)
provide the data to power most [Coveo Machine Learning (Coveo ML)](https://docs.coveo.com/en/188.md) [models](https://docs.coveo.com/en/1012.md), except for [Content Recommendations (CR)](https://docs.coveo.com/en/1016.md).

The output of a CR model depends on [view](https://docs.coveo.com/en/2949.md#view) events and the user's action history.
The Coveo search UI libraries ([Atomic](https://docs.coveo.com/en/lcdf0264.md), [Headless](https://docs.coveo.com/en/lcdf0493.md), and the [JavaScript Search Framework](https://docs.coveo.com/en/187.md)) don't
log these events for you, so you should ensure that you're [sending view events](#send-view-events) for each page that you want to be able to recommend.

> **Leading practice**
>
> Start sending view events as soon as you can to gather data that your CR models can learn from.

## Send view events

Use the [Coveo UA library](https://github.com/coveo/coveo.analytics.js) to send view events to Coveo UA.
You can load it from a [CDN link](#cdn) or install it as an [NPM package](#npm).

### CDN

The following code sample leverages the open source `coveoua.js` script to send a view event when a web page is loaded.

```html
<!DOCTYPE html>
<!-- Set `language` metadata for view events sent from this page. -->
<html lang="<PAGE_LANGUAGE>"> <1>
<head>
  <!-- Set `title` metadata for view events sent from this page. -->
  <title>PAGE_TITLE</title> <2>
  <!-- Import script to send view events and record actions history. -->
  <script type="text/javascript"
          src="https://static.cloud.coveo.com/coveo.analytics.js/2/coveoua.js">
  </script>
  <script>
    // Send view events.
    coveoua("init", "<ACCESS_TOKEN>", "<ANALYTICS_ENDPOINT>"); <3>
    coveoua("send", "view", { <4>
      contentIdKey: "<FIELD_NAME>", <5>
      contentIdValue: "<FIELD_VALUE>", <6>
      contentType: "<CONTENT_TYPE>", <7>
      customData: {
        context_CONTEXT_KEY: "<CUSTOM_CONTEXT_VALUE>" <8>
        // ... Additional custom context information ...
      }
    });
  </script>
  <!-- ... -->
</head>
<!-- ... -->
</html>
```

<1> `<PAGE_LANGUAGE>`: The language identifier of the tracked page.
Coveo UA uses this value to populate the `language` metadata when sending view events.

> **Important**
>
> Coveo ML models are split into distinct submodels for [each language](https://docs.coveo.com/en/1803.md#what-languages-do-coveo-ml-models-support), so you should set the `lang` HTML attribute for each page that you're tracking.

<2> `PAGE_TITLE`: The title of the tracked page.
Coveo UA uses this value to populate the `title` metadata when sending view events.

<3> `<ACCESS_TOKEN>`: A [public API key](https://docs.coveo.com/en/1718.md#api-key-templates) or a valid [search token](https://docs.coveo.com/en/1346.md) if the page requires user authentication (see [Choose and implement a search authentication method](https://docs.coveo.com/en/1369.md), [Search token authentication](https://docs.coveo.com/en/56.md), [Execute queries domain](https://docs.coveo.com/en/1707.md#execute-queries-domain), and [Analytics data domain](https://docs.coveo.com/en/1707.md#analytics-data-domain)).

`<ANALYTICS_ENDPOINT>`: Your [organization analytics endpoint](https://docs.coveo.com/en/mcc80216.md#analytics-endpoint).

--
* `\https://<ORG_ID>.analytics.org.coveo.com` for a non-HIPAA organization

* `\https://<ORG_ID>.analytics.orghipaa.coveo.com` for a HIPAA organization
--

Where `<ORG_ID>` is the unique identifier of your Coveo organization.

<4> The `send` command returns a promise.
To send multiple events sequentially, use `await`:

**Example**

```javascript
async function sendEvents() {
  try {
    await coveoua("send", "view", { /* event data */ });
    await coveoua("send", "view", { /* event data */ });
  } catch (error) {
    console.error("Error sending events:", error);
  }
}
```

<5> `<FIELD_NAME>`: The name of a [field](https://docs.coveo.com/en/1833.md) that can be used to uniquely and permanently identify the tracked page as an [item](https://docs.coveo.com/en/210.md) in the [index](https://docs.coveo.com/en/204.md).
The `@clickableuri` field is a good choice for pages in a public website, because you can retrieve a web page's URL using JavaScript code.

<6> `<FIELD_VALUE>`: The value of the `<FIELD_NAME>` field for the current tracked page.
If `<FIELD_NAME>` is set to `@clickableuri`, the `window.location.href` JavaScript function typically returns the matching `<FIELD_VALUE>` for the current page.

<7> `<CONTENT_TYPE>`: (Optional) The [type of content](https://docs.coveo.com/en/1744.md) being tracked.

<8> [[user-context]]`CONTEXT_KEY`/`<CUSTOM_CONTEXT_VALUE>`: (Optional) The [user context](https://docs.coveo.com/en/3389.md) key-value pairs to pass for more personalized recommendations.
When you log view events with Coveo UA, all user context key names must be prefixed with `context_`.

**Example**

In your search interface, the users are authenticated and you wrote a `getUserRole` function to return the user role (`customer`, `employee`, or `partner`) from the profile of the current user performing the query.
Your custom context key is `userRole`, so you would pass it as follows when logging a view event:

```javascript
context_userRole: getUserRole();
```

> **Note**
>
> If you're passing user context key-values along with view events, you'll likely want to ensure that your recommendation interface [does so as well](https://docs.coveo.com/en/1934.md#leverage-user-context-data-in-a-recommendation-interface) when it sends [queries](https://docs.coveo.com/en/231.md).

### NPM

The Coveo UA library is also available as an [npm package](https://www.npmjs.com/package/coveo.analytics).
You can install it with the following command:

```bash
npm i coveo.analytics
```

You'll want to have a module bundler (such as [webpack](https://webpack.js.org/)) installed and configured.

The following code sample references the Coveo UA library as an ESM module and sends a view event when a web page is loaded.

```javascript
import { CoveoAnalyticsClient } from 'coveo.analytics/modules';
const ua = new CoveoAnalyticsClient({token: '<ACCESS_TOKEN>', endpoint: '<ANALYTICS_ENDPOINT>'}); <1>
ua.sendViewEvent({
  contentIdKey: '<FIELD_NAME>', <2>
  contentIdValue: '<FIELD_VALUE>', <3>
  contentType: '<CONTENT_TYPE>' <4>
});
```

<1> `<ACCESS_TOKEN>`: A [public API key](https://docs.coveo.com/en/1718.md#api-key-templates) or a valid [search token](https://docs.coveo.com/en/1346.md) if the page requires user authentication (see [Choose and implement a search authentication method](https://docs.coveo.com/en/1369.md), [Search token authentication](https://docs.coveo.com/en/56.md), [Execute queries domain](https://docs.coveo.com/en/1707.md#execute-queries-domain), and [Analytics data domain](https://docs.coveo.com/en/1707.md#analytics-data-domain)).

`<ANALYTICS_ENDPOINT>`: Your [organization analytics endpoint](https://docs.coveo.com/en/mcc80216.md#analytics-endpoint).

--
* `\https://<ORG_ID>.analytics.org.coveo.com` for a non-HIPAA organization

* `\https://<ORG_ID>.analytics.orghipaa.coveo.com` for a HIPAA organization
--

Where `<ORG_ID>` is the unique identifier of your Coveo organization.

<2> `<FIELD_NAME>`: The name of a [field](https://docs.coveo.com/en/1833.md) that can be used to uniquely and permanently identify the tracked page as an [item](https://docs.coveo.com/en/210.md) in the [index](https://docs.coveo.com/en/204.md).
The `@clickableuri` field is a good choice for pages in a public site, because you can retrieve a web page's URL using JavaScript code.

<3> `<FIELD_VALUE>`: The value of the `<FIELD_NAME>` field for the current tracked page.
If `<FIELD_NAME>` is set to `@clickableuri`, the `window.location.href` JavaScript function typically returns the matching `<FIELD_VALUE>` for the current page.

<4> `<CONTENT_TYPE>`: (Optional) The [type of content](https://docs.coveo.com/en/1744.md) being tracked.