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

# Kameleoon.API.Experiments

> Reference for the Kameleoon.API.Experiments methods that read, assign, and trigger live experiments.

This module provides methods to access live experiments.

> [!NOTE]
>
> This module is only available with Kameleoon Web Experimentation solution.

## assignVariation

The `assignVariation()` method forces the association of a specific variation for an experiment, which overrides the standard allocation algorithm. If you call this before the experiment triggers, the engine pre-allocates the variation for later activation. If the experiment has already triggered, set the **override** argument to **true** to replace the existing association.

```javascript theme={null}
var experimentID = 2468;
var variationID;

if (Kameleoon.API.CurrentVisit.device.type == "Desktop") {
  variationID = 123456;
} else if (Kameleoon.API.CurrentVisit.device.type == "Tablet") {
  variationID = 654321;
} else {
  variationID = 987654;
}

Kameleoon.API.Experiments.assignVariation(experimentID, variationID);
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| experimentID | Number | ID of the experiment. This field is mandatory. |
| variationID | Number | ID of the variation. This field is mandatory. |
| override | Boolean | When **true**, the engine replaces any existing variation association with the provided value. If omitted, the method defaults to **false**. |

## block

The `block()` method prevents an experiment from triggering or activating, including manual calls through `trigger()`. The block applies to the current page by default until the next `Kameleoon.API.Core.load()`. Set the **visit** argument to **true** to block the experiment for the entire visit.

```javascript theme={null}
// The experiment will be blocked for the current page
Kameleoon.API.Experiments.block(12345);

// The experiment will be blocked for the whole visit
Kameleoon.API.Experiments.block(54321, true);
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| experimentID | Number | ID of the experiment. This field is mandatory. |
| visit | Boolean | When **true**, the engine blocks the experiment for the entire visit. If omitted, the block applies only to the current page (default: **false**). |

## getAll

The `getAll()` method returns all live experiments (running, not draft/paused/stopped).

```javascript theme={null}
Kameleoon.API.Experiments.getAll().forEach(function (experiment) {
  if (experiment.id == 123456 && experiment.active) {
    console.log(experiment.name);
  }
});
```

**Return value**

| Name | Type | Description |
| - | - | - |
| experiments | Array | List of [Experiment objects](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## getActive

The `getActive()` method returns active experiments for the current visit and page. An experiment is active if its variation code has executed in the current session context. To retrieve experiments that you activated on other URLs during the visit, use `getActivatedInVisit()`.

```javascript theme={null}
if (Kameleoon.API.Experiments.getActive().length == 0) {
  Kameleoon.API.Events.trigger("No Experiments Events");
}
```

**Return value**

| Name | Type | Description |
| - | - | - |
| experiments | Array | List of [Experiment objects](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## getById

The `getById()` method returns the experiment for the specified ID.

```javascript theme={null}
var experiment = Kameleoon.API.Experiments.getById(123456);

if (experiment && experiment.active) {
  console.log(experiment.name);
}
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| id | Number | ID of the experiment. This field is mandatory. |

**Return value**

| Name | Type | Description |
| - | - | - |
| experiment | Object | [Experiment object](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## getByName

The `getByName()` method returns the experiment for the specified name.

```javascript theme={null}
var experiment = Kameleoon.API.Experiments.getByName("MyExperimentName");

if (experiment && experiment.active) {
  console.log(experiment.id);
}
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| name | String | Name of the experiment. This field is mandatory. |

**Return value**

| Name | Type | Description |
| - | - | - |
| experiment | Object | [Experiment objects](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## getTriggeredInVisit

The `getTriggeredInVisit()` method returns all experiments triggered during the current visit.

```javascript theme={null}
Kameleoon.API.Experiments.getTriggeredInVisit().forEach(function (experiment) {
  if (experiment.id == 123456) {
    console.log(experiment.name);
  }
});
```

**Return value**

| Name | Type | Description |
| - | - | - |
| experiments | Array | List of [Experiment objects](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## getActivatedInVisit

The `getActivatedInVisit()` method returns all experiments activated during the current visit.

```javascript theme={null}
Kameleoon.API.Experiments.getActivatedInVisit().forEach(function (experiment) {
  if (experiment.id == 123456) {
    console.log(experiment.name);
  }
});
```

**Return value**

| Name | Type | Description |
| - | - | - |
| experiments | Array | List of [Experiment objects](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/experiment-and-personalization.md#experiment). |

## trigger

The `trigger()` method forces an experiment to trigger, bypassing targeting segment conditions. This action initiates the experiment but might not activate it.

```javascript theme={null}
let experimentID = 123456;
Kameleoon.API.Experiments.trigger(experimentID, true);
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| experimentID | Number | ID of the experiment. This field is mandatory. |
| trackingOnly | Boolean | Set to **true** for hybrid experiments (backend implementation with frontend tracking) to perform only tracking actions without executing variation assets (JS, CSS, redirections). If omitted, the method defaults to **false**. |
