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

# A/B Tests and Translations

> Make and manage A/B tests and translations for your customers from your server, one customer at a time

An **A/B test** splits the visitors of one embed between two to four of a customer's compositions and measures which version reaches a goal. A **translation** makes one of a customer's compositions available in several languages on one embed: each language is its own copy, and a visitor sees the copy that matches their browser's language while the translation is active.

Your server can make and manage both for your customers, for **one customer at a time**: the same calls a team uses on widgetic.com, on the same addresses, with your partner key and the customer named in a header. A/B tests and translations are part of your partner agreement, and neither uses credits.

> [!NOTE]
>
> Only your server makes these calls. An [editor session](https://docs.widgetic.com/docs/partners/editor-sessions.md) has no screen for an A/B test or a translation, so a customer's browser cannot make one. Your own page can show what your server reads: a customer's tests, the numbers of one, the languages of a translation.

## What you need

* **A partner key.** Every call on this page is made from your server with your `wgt_prt_` key (see [Partner API Key](https://docs.widgetic.com/docs/partners/overview.md#partner-api-key)). Keep it there.
* **A customer, and compositions filed under them.** A test compares the customer's compositions, and a translation starts from one of them. Make the managed user first and file their compositions under them (see [Filing Content Under a Managed User](https://docs.widgetic.com/docs/partners/managed-users.md#filing-content-under-a-managed-user)).
* **The id of your partner workspace.** Everything your customers make is filed in your partner workspace. Its id is the `{workspaceId}` of the A/B test calls and the `{orgId}` of the two calls that list and make translations (`$WIDGETIC_PARTNER_WORKSPACE_ID` below), and it is the `workspaceId` that the answer of an [editor session](https://docs.widgetic.com/docs/partners/editor-sessions.md#make-a-session) carries. The calls on one translation take no workspace.

## Name the customer

Send `X-Managed-User-Id` (Widgetic's id for the managed user) or `X-Managed-User-External-Id` (your own `externalId`), one of the two, on every call about a customer's test or translation.

* **A list is the customer's.** With the header, `GET /workspaces/{workspaceId}/ab-tests` and `GET /organizations/{orgId}/translations` answer that customer's rows and no others. Without it, they answer everything in your partner workspace, and each row says whose it is in `managedUserId` and `managedUserExternalId` (both `null` for one that belongs to no customer in particular).
* **What you make is the customer's.** A test or a translation made with the header is filed under that customer, at that moment, and for good: there is no field to name another customer, and no way to move it. A `managedUserId` in a body or a query is not read.
* **What you change must be theirs.** A call on one test or translation with the header works only if it is filed under that customer.

## A/B tests

Each version of a test shows one composition, and the shares of all versions add up to 100. The goal, `primaryMetric`, is what the test is judged on: `GET /metrics/available` lists the goals a test can measure, and the one you pick has to be among the ones it says are available. That call needs no key and takes no customer header.

### Make a test

A test starts as a draft, so nothing a visitor sees changes until you start it. Every version has to be a composition filed under the customer.

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.widgetic.com/v1/workspaces/$WIDGETIC_PARTNER_WORKSPACE_ID/ab-tests" \
  -H "Authorization: Bearer $WIDGETIC_PARTNER_KEY" \
  -H "X-Managed-User-Id: $MANAGED_USER_ID" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Welcome banner",
    "primaryMetric": "clicks",
    "successMetrics": ["clicks"],
    "variants": [
      { "name": "Control", "compositionId": "COMPOSITION_A_ID", "trafficPercentage": 50 },
      { "name": "Bigger button", "compositionId": "COMPOSITION_B_ID", "trafficPercentage": 50 }
    ]
  }'
```

The answer is `201`, and the test is one level down, at `data.data`. It is shortened here:

```json theme={"theme":"css-variables"}
{
  "success": true,
  "data": {
    "success": true,
    "data": {
      "id": "c0a1f2b4-5d6e-4f70-8a91-b2c3d4e5f607",
      "name": "Welcome banner",
      "status": "draft",
      "managedUserId": "3f0c9a52-8e0a-4a52-9d52-5d8a1f1b7c10",
      "primaryMetric": "clicks",
      "revision": 1,
      "variants": [
        { "id": "5b1e0d3c-7a24-4c8e-9f10-6a3b2c1d0e9f", "name": "Control", "compositionId": "COMPOSITION_A_ID", "trafficPercentage": 50 },
        { "id": "9d8c7b6a-5f4e-4d3c-b2a1-0f9e8d7c6b5a", "name": "Bigger button", "compositionId": "COMPOSITION_B_ID", "trafficPercentage": 50 }
      ]
    }
  }
}
```

Keep the `id` of each version: you need one to name a winner. Some goals are counted by the embed itself and others have to be reported by the widget, which `GET /metrics/available` says for each. `POST /workspaces/{workspaceId}/ab-tests/validate-goal-tracking` checks what the compositions you picked can be measured on before you make the test, and `GET /workspaces/{workspaceId}/compositions/{id}/ab-goal-targets` lists the elements of a composition that can be a version's `goalTarget`, when a goal should count on one element and not anywhere in the widget.

With the TypeScript SDK the header is an argument of the call: `xManagedUserId`, or `xManagedUserExternalId` for your own id.

```typescript theme={"theme":"css-variables"}
import { randomUUID } from 'node:crypto';
import { ABTestingApi } from '@widgetic/api-sdk-ts';

const abTestingApi = new ABTestingApi(process.env.WIDGETIC_PARTNER_KEY!);

// Widgetic's id for the managed user, and the id of your partner workspace (the `workspaceId` of an editor session)
const managedUserId = 'MANAGED_USER_ID';
const partnerWorkspaceId = 'PARTNER_WORKSPACE_ID';

const created = await abTestingApi.createWorkspaceABTest({
  workspaceId: partnerWorkspaceId,
  xManagedUserId: managedUserId,
  idempotencyKey: randomUUID(),
  createABTestRequest: {
    name: 'Welcome banner',
    primaryMetric: 'clicks',
    successMetrics: ['clicks'],
    variants: [
      { name: 'Control', compositionId: 'COMPOSITION_A_ID', trafficPercentage: 50 },
      { name: 'Bigger button', compositionId: 'COMPOSITION_B_ID', trafficPercentage: 50 },
    ],
  },
});

// The test is at `data.data`
console.log(created.data.data?.id, created.data.data?.status);
```

### Run a test

| Call | What it does |
| - | - |
| `GET /workspaces/{workspaceId}/ab-tests` | The customer's tests. `status` narrows the list; `data.data` holds the tests and `data.meta.count` says how many |
| `GET /workspaces/{workspaceId}/ab-tests/{testId}` | One test, with its versions |
| `PUT /workspaces/{workspaceId}/ab-tests/{testId}` | Changes the name and the settings of the test. While it is a draft it also changes the goals and the goal element of each version. A `status` in the body moves the test |
| `POST /workspaces/{workspaceId}/ab-tests/{testId}/start` | Starts a draft, or resumes a paused test |
| `POST /workspaces/{workspaceId}/ab-tests/{testId}/pause` | Pauses a running test |
| `POST /workspaces/{workspaceId}/ab-tests/{testId}/stop` | Ends a running test. Name a version in `winningVariantId` to graduate it as the winner, or leave it out to end the test with no winner |
| `PUT /workspaces/{workspaceId}/ab-tests/{testId}/duration` | Sets how many days the test runs; its end date follows |
| `DELETE /workspaces/{workspaceId}/ab-tests/{testId}` | Deletes the test |

A test reaches visitors only while it is `running`. A test that has ended (`completed`, or `graduated` when it had a winner) stays ended. Every write but the delete takes an `Idempotency-Key` (see [Idempotency](https://docs.widgetic.com/docs/reference/idempotency.md)), and a write that carries the `revision` you last read is refused with `409` when the test changed since (`details.reason` is `revision_changed`), so that two writers do not overwrite each other.

### Read the results

* `GET /workspaces/{workspaceId}/ab-tests/{testId}/statistics` answers what the test has gathered: visitors and goals reached overall and for each version, how each version compares with the control, and the advice on what to do next. Unlike most calls of a test, it sends its data once, in `data`.
* `GET /workspaces/{workspaceId}/ab-tests/{testId}/auto-stop-check` says whether the test should stop by now: its duration is over, it reached significance, or it meets its graduation threshold.
* `GET /workspaces/{workspaceId}/ab-tests/{testId}/diagnosis` is an AI review of what the versions change, and whether the challenger is safe to promote. It calls another service, so it is slower than the other reads: ask for it when you want it.

## Translations

A translation is made while your call runs: the original is copied once for each language, and each copy is translated by a language model. **Give the request a generous timeout**, and do not send it again when it times out, because the call takes no `Idempotency-Key`: list the customer's translations first, and use the one that was made.

### Make a translation

`GET /translations/supported-languages` lists the languages, and `GET /translations/language-presets` the groups of them (EU, APAC, and so on) that can be chosen in one go. Neither needs a key or a customer header. The original has to be a composition filed under the customer, and each copy is filed under the customer too.

```bash theme={"theme":"css-variables"}
curl -sS -X POST "https://api.widgetic.com/v1/organizations/$WIDGETIC_PARTNER_WORKSPACE_ID/translations" \
  -H "Authorization: Bearer $WIDGETIC_PARTNER_KEY" \
  -H "X-Managed-User-Id: $MANAGED_USER_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Welcome banner in three languages",
    "rootCompositionId": "COMPOSITION_ID",
    "rootLanguage": "en",
    "selectedLanguages": ["es", "fr", "de"]
  }'
```

The answer is `201`, with the translation in `data.translation` and each language in its `variants`. Read the `status` of every variant: `active` is a copy that is ready, `translating` one that is not finished, and `error` one that failed. A language that failed is made again by asking for it again (see [Change the languages](#change-the-languages)). The translation starts as a draft, and the original language is always one of its variants.

With the TypeScript SDK:

```typescript theme={"theme":"css-variables"}
import { OrganizationsApi } from '@widgetic/api-sdk-ts';

const organizationsApi = new OrganizationsApi(process.env.WIDGETIC_PARTNER_KEY!);

// Widgetic's id for the managed user, and the id of your partner workspace (the `workspaceId` of an editor session)
const managedUserId = 'MANAGED_USER_ID';
const partnerWorkspaceId = 'PARTNER_WORKSPACE_ID';

const made = await organizationsApi.createOrganizationTranslation({
  orgId: partnerWorkspaceId,
  xManagedUserId: managedUserId,
  createWorkspaceTranslationRequest: {
    name: 'Welcome banner in three languages',
    rootCompositionId: 'COMPOSITION_ID',
    rootLanguage: 'en',
    selectedLanguages: ['es', 'fr', 'de'],
  },
});

console.log(made.data.translation?.id, made.data.translation?.status);
```

### Change the languages

These calls take the id of the translation and no workspace. The translation has to be filed under the customer in the header.

| Call | What it does |
| - | - |
| `GET /organizations/{orgId}/translations` | The customer's translations. `status` narrows the list; the answer holds `data.translations` and `data.count` |
| `GET /translations/{id}` | One translation, with each language |
| `POST /translations/{id}/languages` | Adds up to 10 languages in `languages`. A language that is already there and ready is skipped, and one that failed is translated again |
| `DELETE /translations/{id}/languages/{languageCode}` | Removes one language |
| `POST /translations/{id}/retranslate` | Translates the copies again from the original: all of them, or the ones in `languages`. Edits made by hand in a copy are lost |
| `PUT /translations/{id}/status` | Moves the translation to `draft`, `active`, `paused` or `completed` |
| `DELETE /translations/{id}` | Deletes the translation |

A translation holds up to 20 languages, the original included. Adding languages and translating again are slow for the same reason as making a translation. Adding languages, translating again and changing the status take an `Idempotency-Key`; making a translation and the two calls that remove one do not. The calls that change a translation, deleting it aside, also take the `revision` you last read, when you want a write refused if the translation changed since (`409`, and `details.reason` is `revision_changed`). A translation is shown to visitors only while it is `active`, so set it with `PUT /translations/{id}/status` when the languages are ready, and only a language whose copy is ready is shown.

### Read the views

`GET /translations/{id}/analytics` compares the views of the languages. Without a `period` it covers `30d`; the others are `7d`, `90d` and `all_time`.

## Show it on a page

A test and a translation reach a visitor through the same script as any other embed (see [Embedding widgets](https://docs.widgetic.com/docs/api/embedding.md#embed-code)), with an element that names the test or the translation in place of a composition:

```html theme={"theme":"css-variables"}
<script src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6" data-api-url="https://api.widgetic.com"></script>

<!-- An A/B test: each visitor is shown one of its versions -->
<widgetic-abtest experiment-id="TEST_ID"></widgetic-abtest>

<!-- A translation: the visitor is shown the copy in the language of their browser -->
<widgetic-translations experiment-id="TRANSLATION_ID"></widgetic-translations>
```

`TEST_ID` and `TRANSLATION_ID` are the `id` of the test and of the translation. A test is served only while it is `running`, and a translation only while it is `active`.

## What belongs to whom

* **A customer's tests and translations are theirs.** With the header, another customer's test or translation, one you made without a header and one that does not exist all answer `404` with the same words, so you cannot tell them apart: `RESOURCE_AB_TEST_NOT_FOUND` for a test, `RESOURCE_TRANSLATION_NOT_FOUND` for a translation. A test or a translation that is not filed under a customer is not any customer's.
* **A test compares the customer's compositions only.** A version, or the original of a translation, that is another customer's, one you made without naming a customer or another account's reads as missing: `404` with `RESOURCE_COMPOSITION_NOT_FOUND`.
* **The workspace is your partner workspace.** The id of another workspace answers `404` with `RESOURCE_WORKSPACE_NOT_FOUND`, and a word where the id belongs (`me`, say) answers `403` with `MANAGED_USER_SCOPE_ROUTE_NOT_AVAILABLE`.
* **The customer must be yours, and the key a partner's.** An id that is not one of your managed users answers `404` with `MANAGED_USER_NOT_FOUND`, and so does any API key that is not a partner's. A call that is not made with an API key answers `403` with `MANAGED_USER_SCOPE_NOT_ALLOWED`, and an empty, repeated or malformed header is `400` with `MANAGED_USER_SCOPE_INVALID`.
* **A customer you switched off** keeps its tests and translations, and you can still read and change them, but you cannot make a test or a translation for it, or add a language to one: `409` with `MANAGED_USER_INACTIVE`.
* **Your account owns them all.** They run under your account and your agreement, as your compositions do. Deleting a managed user does not delete its tests and translations: they stay in your account and keep working on every page that shows them, and only the label that said whose they were is removed (see [Deleting Managed Users](https://docs.widgetic.com/docs/partners/managed-users.md#deleting-managed-users)).
* **Other calls stay yours.** A call about A/B tests or translations that this page does not list is not about one customer's test or translation, and with either header it answers `403` with `MANAGED_USER_SCOPE_ROUTE_NOT_AVAILABLE`. The calls an embed makes for a visitor take no key and no header.

Without the header, a call is your own account's, as it always was: it sees every customer's tests and translations next to yours, and what you make that way is yours, not a customer's.

## Limits and cost

* **Up to 50 A/B tests and 25 translations for each customer, in any state.** Another one is refused with `422` and `BUSINESS_OPERATION_INVALID`, with `reason` in its details (`too_many_ab_tests` or `too_many_translations`) and `limit` (the number). The limit is counted for each customer, so one customer cannot use up the room of another, or yours: what you make yourself is not counted. Delete what a customer no longer needs.
* **A test has two to four versions.** A translation holds up to 20 languages, and one call adds at most 10.
* **They take no credits.** Making and running a test is not an AI call, and a translation, although it sends the text of the composition to a language model, does not use your credits or count against a customer's allowance either.

## Errors

| Status | Code | Meaning | What to do |
| - | - | - | - |
| 400 | `MANAGED_USER_SCOPE_INVALID` | The customer header is empty, repeated or malformed | Send one of `X-Managed-User-Id` and `X-Managed-User-External-Id`, with a value |
| 400 | `VALIDATION_FIELD_INVALID` or `VALIDATION_FIELD_REQUIRED` | A field is missing or has the wrong form: shares that do not add up to 100, a winner that is not one of the versions, a language that is not supported, or the `Idempotency-Key` of a write that needs one | Read `details` and fix the field |
| 403 | `MANAGED_USER_SCOPE_ROUTE_NOT_AVAILABLE` | The call is not about one test or one translation of your partner workspace (or a word is where the workspace id belongs) | Use the id of your partner workspace, or leave the header out |
| 403 | `MANAGED_USER_SCOPE_NOT_ALLOWED` | A customer header came with a credential that is not an API key | Send the header from your server, with your partner key |
| 403 | `AUTHZ_PLAN_RESTRICTION` | The plan of the workspace does not include the feature | Both are part of a partner agreement: ask Widgetic why yours does not |
| 404 | `RESOURCE_AB_TEST_NOT_FOUND` | The test is not this customer's, or does not exist | Check which customer you named |
| 404 | `RESOURCE_TRANSLATION_NOT_FOUND` | The translation is not this customer's, or does not exist | Check which customer you named |
| 404 | `RESOURCE_COMPOSITION_NOT_FOUND` | A version, or the original of a translation, is not a composition of this customer | File the composition under the customer (see [Filing Content Under a Managed User](https://docs.widgetic.com/docs/partners/managed-users.md#filing-content-under-a-managed-user)) |
| 404 | `RESOURCE_WORKSPACE_NOT_FOUND` | The workspace id in the path is not your partner workspace | Use the id of your partner workspace |
| 404 | `MANAGED_USER_NOT_FOUND` | The customer in the header is not one of your managed users, or the key is not a partner's | Check the id or the `externalId`, and send your partner key |
| 409 | `MANAGED_USER_INACTIVE` | You tried to make a test or a translation, or to add a language, for a customer you switched off | Switch the customer on, or make it without the header |
| 409 | `BUSINESS_STATE_CONFLICT` | The test changed state while the call ran (`details.reason` is `status_changed`), or since the `revision` you sent (`revision_changed`) | Read it again and send the call again |
| 422 | `BUSINESS_OPERATION_INVALID` | The customer already holds as many tests or translations as it may (`details.reason` is `too_many_ab_tests` or `too_many_translations`) | Delete one, or make it for yourself |

## Next Steps

> [card] [Managed Users](https://docs.widgetic.com/docs/partners/managed-users.md)
>
> File your customers' content under them

> [card] [Workflows](https://docs.widgetic.com/docs/partners/ui-components/workflows.md#from-your-server)
>
> The same two headers on the calls of workflows

> [card] [Embedding widgets](https://docs.widgetic.com/docs/api/embedding.md)
>
> Put a published composition on a page

> [card] [Idempotency](https://docs.widgetic.com/docs/reference/idempotency.md)
>
> Send a write twice and have it done once


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.