# A/B Testing

Spara lets you test multiple agents side by side to measure which performs better before rolling out changes to all your traffic. This helps you iterate on your sales conversations with confidence, without risking your funnel performance.

## What can be tested

Anything unique to an agent can be tested, including:

* **AI instructions** (prompt) — Test different conversational styles, objection handling, or qualification approaches
* **Preview messages** — Test different pop-up messages to see which drives more engagement
* **Suggested responses** — Test different suggested reply buttons
* **Media and calendars** — Test whether showing a video or calendar at different points improves conversion

Since each agent has its own configuration, you can test any combination of these variables by creating separate agents with different settings.

## How to set up an A/B test

A/B tests are managed through the **Experiments** panel on the Agents page and through each agent's **Overview** tab. No setup from the Spara team is required.

### Create a variant

1. Open the [Agents page](https://app.spara.co/agents) and find the chat agent you want to test.
2. In the **Experiments** panel, click **Create variant**. The variant inherits the control's trigger automatically.
3. Configure the variant's prompt, preview message, suggested responses, media, or calendar — whatever you're testing.
4. Use the **breadcrumb switcher** at the top of any agent in the experiment to jump between the control and all variants. Each shows a **Published** or **Draft** badge so you can see what's live at a glance.

### Set the traffic split

In the **Experiments** panel, adjust the traffic percentage for each variant. The control receives whatever traffic isn't allocated to variants. Traffic is split deterministically — the same visitor always sees the same agent.

### Manage variants

* **Archive** a variant to stop it receiving traffic without deleting it. Archived variants can be restored later.
* **Promote** a variant to control if it outperforms the original. The promoted variant takes over the control role and the former control becomes a variant.
* Each agent's **Overview** tab shows its current role (control, variant, or archived), its inherited trigger, and its share of traffic.

### How traffic splitting works

Spara uses an `experiment_hash` function to deterministically assign each visitor to a group. The function takes the visitor's unique identifier and an experiment name, and produces a consistent number. This means:

* The same visitor always sees the same agent (no mid-conversation switching)
* The split is random and evenly distributed
* You control the percentage using the traffic controls in the Experiments panel

### Assigning leads to groups

To track which group each lead was in, configure each agent to assign a field value when it activates. For example:

* Control agent assigns `experiment_group = "control"`
* Experiment agent assigns `experiment_group = "experiment"`

This field persists on the lead record, so you can filter analytics and exports by group even after the experiment ends.

## Measuring results

Use the [Analytics](/platform/analytics.md) page to compare performance between your agents. Use the **Agent** filter to view metrics for each agent individually, then compare:

* **Leads engaged** — Which agent drives more conversations?
* **Calls scheduled** — Which agent converts more leads to meetings?
* **Emails collected** — Which agent captures more contact information?
* **Conversation length** — Are leads more or less engaged with each variant?

### How long to run a test

Run your test until you have enough data to be confident in the results. As a general guideline, aim for at least 100 engaged leads per variant before drawing conclusions. High-traffic sites may reach significance in days; lower-traffic sites may need several weeks.

## Using third-party analytics tools

{% hint style="warning" %}
Spara does not natively integrate with third-party A/B testing tools. The approach below describes a custom integration using Spara's APIs.
{% endhint %}

You can use Spara's [Web API](/developers/spara-api/web-api.md) or [Javascript API](/developers/spara-api/javascript-api.md) to connect an external experimentation platform (e.g., Amplitude, LaunchDarkly) with Spara's agent routing:

1. Set up your experiment in the third-party tool, assigning visitors to groups
2. Use the Spara Javascript API to set a field on each visitor (e.g., `experiment_group = "control"`)
3. Configure your Spara agents to activate based on that field value

{% hint style="info" %}
Do not switch a visitor's agent mid-conversation. Only assign the experiment group before the first interaction.
{% endhint %}

## Loading Spara for only some visitors

To show Spara to only a percentage of website visitors, you can conditionally load the embed snippet:

```javascript
<script>
var SPARA_TRAFFIC_PERCENTAGE = 10; // 10 = 10%, 100 = always show

(function() {
  var k = 'spara_traffic', d = 30;
  var s = localStorage.getItem(k);
  var run = SPARA_TRAFFIC_PERCENTAGE >= 100 || (s ? JSON.parse(s).i && Date.now() < JSON.parse(s).e : Math.random() * 100 < SPARA_TRAFFIC_PERCENTAGE);

  if (!s || Date.now() > JSON.parse(s).e) {
    localStorage.setItem(k, JSON.stringify({i: run, e: Date.now() + d * 864e5}));
  }

  if (run) {
    var script = document.createElement('script');
    script.src = 'https://app.spara.co/embed-<app_id>.js';
    document.head.appendChild(script);
  }
})();
</script>
```

Set `SPARA_TRAFFIC_PERCENTAGE` to control the percentage. The assignment persists for 30 days so returning visitors stay in the same group.

{% hint style="info" %}
It is usually simpler to load Spara for 100% of visitors and use the agent-based A/B testing approach instead. Configure a "control" agent that shows only the avatar (no preview message) and an "experiment" agent that shows a pop-up — this achieves the same result without needing to modify your embed code.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.spara.com/agents/agent-overview/a-b-testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
