---
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/) lets you track user interactions with your [search interfaces](https://docs.coveo.com/en/2741/) so that you can optimize your Coveo solution.
Search, click, and custom [events](https://docs.coveo.com/en/260/)
provide the data to power most [Coveo Machine Learning (Coveo ML)](https://docs.coveo.com/en/188/) [models](https://docs.coveo.com/en/1012/), except for [Content Recommendations (CR)](https://docs.coveo.com/en/1016/).
The output of a CR model depends on [view](https://docs.coveo.com/en/2949#view) events and the user's action history.
The Coveo search UI libraries ([Atomic](https://docs.coveo.com/en/lcdf0264/), [Headless](https://docs.coveo.com/en/lcdf0493/), and the [JavaScript Search Framework](https://docs.coveo.com/en/187/)) 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
<1>
PAGE_TITLE <2>
```
<1> ``: 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#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> ``: A [public API key](https://docs.coveo.com/en/1718#api-key-templates) or a valid [search token](https://docs.coveo.com/en/1346/) if the page requires user authentication (see [Choose and implement a search authentication method](https://docs.coveo.com/en/1369/), [Search token authentication](https://docs.coveo.com/en/56/), [Execute queries domain](https://docs.coveo.com/en/1707#execute-queries-domain), and [Analytics data domain](https://docs.coveo.com/en/1707#analytics-data-domain)).
``: Your [organization analytics endpoint](https://docs.coveo.com/en/mcc80216#analytics-endpoint).
--
* `\https://.analytics.org.coveo.com` for a non-HIPAA organization
* `\https://.analytics.orghipaa.coveo.com` for a HIPAA organization
--
Where `` 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> ``: The name of a [field](https://docs.coveo.com/en/1833/) that can be used to uniquely and permanently identify the tracked page as an [item](https://docs.coveo.com/en/210/) in the [index](https://docs.coveo.com/en/204/).
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> ``: The value of the `` field for the current tracked page.
If `` is set to `@clickableuri`, the `window.location.href` JavaScript function typically returns the matching `` for the current page.
<7> ``: (Optional) The [type of content](https://docs.coveo.com/en/1744/) being tracked.
<8> [[user-context]]`CONTEXT_KEY`/``: (Optional) The [user context](https://docs.coveo.com/en/3389/) 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#leverage-user-context-data-in-a-recommendation-interface) when it sends [queries](https://docs.coveo.com/en/231/).
### 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: '', endpoint: ''}); <1>
ua.sendViewEvent({
contentIdKey: '', <2>
contentIdValue: '', <3>
contentType: '' <4>
});
```
<1> ``: A [public API key](https://docs.coveo.com/en/1718#api-key-templates) or a valid [search token](https://docs.coveo.com/en/1346/) if the page requires user authentication (see [Choose and implement a search authentication method](https://docs.coveo.com/en/1369/), [Search token authentication](https://docs.coveo.com/en/56/), [Execute queries domain](https://docs.coveo.com/en/1707#execute-queries-domain), and [Analytics data domain](https://docs.coveo.com/en/1707#analytics-data-domain)).
``: Your [organization analytics endpoint](https://docs.coveo.com/en/mcc80216#analytics-endpoint).
--
* `\https://.analytics.org.coveo.com` for a non-HIPAA organization
* `\https://.analytics.orghipaa.coveo.com` for a HIPAA organization
--
Where `` is the unique identifier of your Coveo organization.
<2> ``: The name of a [field](https://docs.coveo.com/en/1833/) that can be used to uniquely and permanently identify the tracked page as an [item](https://docs.coveo.com/en/210/) in the [index](https://docs.coveo.com/en/204/).
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> ``: The value of the `` field for the current tracked page.
If `` is set to `@clickableuri`, the `window.location.href` JavaScript function typically returns the matching `` for the current page.
<4> ``: (Optional) The [type of content](https://docs.coveo.com/en/1744/) being tracked.