> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trieve.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Analytics with Trieve

> Learn how to get started with Trieve Analytics

## Overview

Trieve Analytics is a powerful tool that allows you to analyze your queries and gain insights from them.

## Setting Up Trieve Analytics

Trieve automatically collects all of the searches and RAG chats that are made in your dataset. You don't have to worry about manually tracking them.

## Automatically Tracked Analytics

## Search Analytics

We provide extensive analytics on your searches all of which can be found in the [search analytics route](api-reference/analytics/get-search-analytics).
These analytics include:

* **Head Queries**: Head queries is a collection of the most common queries.
* **Low Confidence Queries**: The queries where all your results have scores, which can be useful for improving your search results.
* **No Results Queries**: This table shows the queries that returned no results, which can be useful for improving your search results.
* **Popular Filters**: This table shows the most popular filters used in searches.
* **Latency / time**: This graph shows the average latency of searches over time.
* **Searches / time**: This graph shows the number of searches made over time.

You can see all of this data on the dashboard in the analytics section, as well as be able to export them to a CSV so that you can analyze them in your own tools.
![](https://trieve.b-cdn.net/Screenshot%202024-10-01%20at%201.46.51%E2%80%AFPM.png)

## RAG Chat Analytics

We provide extensive analytics on your RAG chats all of which can be found in the [RAG analytics route](api-reference/analytics/get-rag-analytics).
These analytics include:

* **Usage over time**: This graph shows the number of RAG chats made over time.
* **All RAG Chats**: This table shows all of the RAG chats made in your dataset.

You can see all of this data on the dashboard in the analytics section, as well as be able to export them to a CSV so that you can analyze them in your own tools.
![](https://trieve.b-cdn.net/Screenshot%202024-10-01%20at%202.06.27%E2%80%AFPM.png)

## Custom Metrics

### 1. Getting the Request ID

All searches, recommendations, and chats return a `requestID`. With this you can track:

* Clicks
* user ratings (on any scale you want)
* Add to Cart's
* User Views
* Purchase's

forwarding this `query_id` to any custom

#### Request ID from Searches

All Searches Return an `id`, this `id` is your Request ID

```json search_response.json {5} theme={null}
{
    "chunks" : [
        // ... Your search response
    ], 
    "id": "28f37011-179e-4927-9138-771132d0b6c3" // Request ID
}
```

#### Request ID from Recommendations

Calls to `/api/chunk/recommend` return an `id`

```json recommend_response.json {5} theme={null}
{
    "chunks" : [
        // ... Your Chunk Data
    ], 
    "id": "28f37011-179e-4927-9138-771132d0b6c3" // Request ID
    // ... other data
}
```

#### Request ID from LLM Messages

Calls to `/api/message` return a streaming response so the id is not located in the payload, the id is instead placed on the header `TR-QueryID`.

### 2. Enriching Events

#### Track Click Through Rate data

Send Click-Through Rate data to Trieve using the [send CTR data route](/api-reference/analytics/send-ctr-data). Referencing your search `id` from the previous step.

`chunk_id` in this case is the chunk the user clicked on from the search

<CodeGroup>
  ```json curl theme={null}
  curl -X POST https://api.trieve.ai/api/analytics/ctr \
    -H "TR-Dataset: YOUR_DATASET_ID" \
    -H "Authorization: <YOUR_API_KEY>" \
      -d '{
          "ctr_type": "search | rag",
          "clicked_chunk_id": "<chunk_id>",
          "position": 1,
          "request_id": "<your-request-id>"
      }'
  ```

  ```ts TS SDK theme={null}
  import { TrieveSDK } from "trieve-ts-sdk";

  let search_id = ""; // Set your search_id here
  let chunk_id = ""; // The chunk clicked
  let api_key = "tr-**************"; // Your api key here

  export const trieve = new TrieveSDK({
    apiKey: "tr-********************************",
    datasetId: api_key,
  });

  const data = await trieve.sendCTRData({
      ctr_type: "search | rag",
      clicked_chunk_id: chunk_id,
      position: 1,
      request_id: search_id,
  });
  ```
</CodeGroup>

#### Allow the user to rate the search

You can also send user feedback to the Trieve API using the [rate search query route](/api-reference/analytics/rate-search) or [rate RAG query route](/api-reference/analytics/rate-rag).

The `rating` parameter can be any number of your choosing. Feel free to make your own rating scales

<CodeGroup>
  ```json curl theme={null}
  curl -X POST https://api.trieve.ai/api/analytics/rate-search \
    -H "TR-Dataset: YOUR_DATASET_ID" \
    -H "Authorization: YOUR_API_KEY" \
      -d '{
          "query_id": "<your-request-id>",
          "rating": 5,
          "note": "Great results!"
      }'
  ```

  ```ts TS SDK theme={null}
  import { TrieveSDK } from "trieve-ts-sdk";

  let search_id = ""; // Set your search_id here
  let api_key = "tr-**************"; // Your api key here

  export const trieve = new TrieveSDK({
    apiKey: "tr-********************************",
    datasetId: "<your-api-key>",
  });

  const data = await trieve.rateSearchQuery({
      query_id: search_id,
      rating: 5,
      note: "Great results!",
  });
  ```
</CodeGroup>

#### Custom event types

You can send custom events to the Trieve API using the [send event route](/api-reference/analytics/send-user-event-data). Referencing your search `id` from the previous step

<CodeGroup>
  ```json curl theme={null}
  curl -X POST https://api.trieve.ai/api/analytics/event \
      -H "TR-Dataset: YOUR_DATASET_ID" \
      -H "Authorization: YOUR_API_KEY" \
      -d '{
          "event_type": "add_to_cart",
              "event_name": "Add to Cart",
              "items": [
                  "Cheesesticks",
              "Pizza",
              ],
              "request_id": "<your-request-id>"
      }'
  ```

  ```ts TS SDK theme={null}
  import { TrieveSDK } from "trieve-ts-sdk";

  let search_id = ""; // Set your search_id here
  let api_key = "tr-**************"; // Your api key here

  export const trieve = new TrieveSDK({
      apiKey: "tr-********************************",
      datasetId: api_key,
  });

  const data = await trieve.sendEventData({
          event_type: "add_to_cart",
          event_name: "Add to Cart",
          items: ["Cheesesticks", "Pizza"],
          request_id: search_id,
  });
  ```
</CodeGroup>

### Querying Custom Metrics

You can query this custom event data using the [get all events route](/api-reference/analytics/get-all-events). This will return all of the custom events that you have sent to the Trieve API.

```bash curl theme={null}
curl --request POST \
  --url https://api.trieve.ai/api/analytics/events \
  --header 'Authorization: <api-key>' \
  --header 'Content-Type: application/json' \
  --data '{
  "filter": {
    "date_range": {
      "gt": "2021-08-10T00:00:00Z",
      "lt": "2021-08-11T00:00:00Z"
    },
    "event_type": "<your_event_type>",
    "is_conversion": true,
    "metadata_filter": "path = \"value\"",
    "user_id": "user1"
  }
}'
```
