# Copilot event tracking

Track the prompts, responses, conversions and ratings of your AI copilot so Screeb can measure the quality of its conversations.

If your product has an AI copilot, track each conversation turn as a copilot event. Screeb uses these events to analyze the conversations and show the results in Agent analytics: see [Agent analytics](https://help.screeb.app/ai/agent-analytics/).

Track two events on every turn of a conversation, the user prompt and the copilot response, and add the optional conversion and satisfaction events to measure success. Every event accepts extra properties of your own, next to the ones listed below.

> [!TIP]
> If your copilot runs on your servers, which is the usual case for an LLM assistant, send the events from your backend with the [Screeb API](#track-from-your-server) instead: the call then lives in the code that already produces the prompt and the response.

## Track the prompt and the response

Send `screeb_copilot_user_prompt` when the user sends a message to the copilot:

```js
$screeb("event.copilot", "screeb_copilot_user_prompt", {
  prompt: "How can I export my data?",
  conversation_id: "conv_123",
  session_id: "sess_456",
  agent_id: "support_agent",
});
```

Send `screeb_copilot_response` when the copilot answers:

```js
$screeb("event.copilot", "screeb_copilot_response", {
  response: "You can export your data from the Settings page.",
  conversation_id: "conv_123",
  session_id: "sess_456",
  agent_id: "support_agent",
});
```

| Property          | Type   | Required                        |
| ----------------- | ------ | ------------------------------- |
| `prompt`          | string | On `screeb_copilot_user_prompt` |
| `response`        | string | On `screeb_copilot_response`    |
| `conversation_id` | string | Yes                             |
| `session_id`      | string | Yes                             |
| `agent_id`        | string | Yes                             |

The command fails when a required property is missing.

## Track every turn

The most common mistake is to send the prompt and the response once, at the start of the conversation. Send both again on every turn, with the same `conversation_id`, `session_id` and `agent_id` for as long as the conversation lasts. If your tracked conversations stop after one or two events, check this first.

```js
// Created once, when the conversation starts, then reused on every turn.
const conversationId = `conv_${crypto.randomUUID()}`;
const sessionId = `sess_${crypto.randomUUID()}`;
const agentId = "support_agent";

async function handleUserMessage(prompt) {
  $screeb("event.copilot", "screeb_copilot_user_prompt", {
    prompt,
    conversation_id: conversationId,
    session_id: sessionId,
    agent_id: agentId,
  });

  const response = await copilot.ask(prompt);

  $screeb("event.copilot", "screeb_copilot_response", {
    response,
    conversation_id: conversationId,
    session_id: sessionId,
    agent_id: agentId,
  });

  return response;
}
```

## Track conversions

Send `screeb_copilot_conversion` when a copilot interaction leads to a result that matters to you, such as a purchase, a sign-up or a completed workflow. Only `agent_id` is required; `conversation_id` and `session_id` link the conversion to a conversation.

```js
$screeb("event.copilot", "screeb_copilot_conversion", {
  agent_id: "support_agent",
  conversation_id: "conv_123",
  session_id: "sess_456",
});
```

## Track satisfaction

Send `screeb_copilot_satisfaction` when the user rates the copilot. `rating` and `rating_scale` are strings, and every property below is required.

```js
$screeb("event.copilot", "screeb_copilot_satisfaction", {
  rating: "4",
  rating_scale: "5",
  conversation_id: "conv_123",
  session_id: "sess_456",
  agent_id: "support_agent",
});
```

> [!NOTE]
> When data anonymization is on for the workspace, the tag redacts personal data patterns, such as emails, card numbers or phone numbers, in `prompt` and `response` before sending them: see [Data anonymization](https://developers.screeb.app/sdk-js/data-anonymization).

## Track from your server

When the conversation does not go through the browser, send the same events to `POST https://api.screeb.app/1.0/event` with the `copilot` event type and the same `event_name` values:

```bash
curl -X POST https://api.screeb.app/1.0/event \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <API-TOKEN>" \
  -d '[
    {
      "user_alias": "<USER-ID>",
      "type": "copilot",
      "event_name": "screeb_copilot_user_prompt",
      "agent_id": "support_agent",
      "conversation_id": "conv_123",
      "copilot_session_id": "sess_456",
      "prompt": "How can I export my data?"
    }
  ]'
```

Some field names differ from the tag: `session_id` becomes `copilot_session_id`. See the [`copilot` event type](https://developers.screeb.app/api-and-connectors/screeb-api#copilot) of the Screeb API for every field, the authorization and the rate limits.
