> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-engage/17.latest/developers/ab-testing/csharp-api.md).

# Retrieving A/B test variants in C\#

## Retrieving Active A/B test variants

You can retrieve the active A/B test variants for a visitor in different ways depending on your specific scenario:

* `IAbTestingVisitorService.GetVisitorAbTestVariants(visitorExternalId, contentId, culture, contentTypeId)`
  * Namespace: `Umbraco.Engage.Infrastructure.AbTesting.Services.Interfaces`
  * Retrieves active A/B test variants on a specific page using numeric IDs, without requiring a request context.
  * The visitor external id can be retrieved using `IAnalyticsVisitorExternalIdHandler.GetExternalId()`
* `IAbTestingVisitorService.GetVisitorAbTestVariants(visitorExternalId, contentKey, contentTypeKey, culture)`
  * Namespace: `Umbraco.Engage.Infrastructure.AbTesting.Services.Interfaces`
  * Retrieves active A/B test variants on a specific page using GUID keys, without requiring a request context.
  * Preferred for new implementations as it uses Umbraco's GUID-based identifiers.
* `IAbTestVisitorToVariantManager.GetActiveVisitorVariants(visitorExternalId)`
  * Namespace: `Umbraco.Engage.Infrastructure.AbTesting`
  * Retrieves *all* active A/B test variants for the given visitor throughout the website.
  * The visitor external id can be retrieved using `IAnalyticsVisitorExternalIdHandler.GetExternalId()`

### Example - Getting the A/B test variants for the current visitor

This example demonstrates how to create a service that retrieves the active A/B test variants for a specific visitor, content, and culture variation. The service wraps the `IAbTestingVisitorService.GetVisitorAbTestVariants()` method, using the current HttpContext and UmbracoContext to obtain the visitor ID and requested content.

```cs
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Engage.Infrastructure.AbTesting.Models;
using Umbraco.Engage.Infrastructure.AbTesting.Services.Interfaces;
using Umbraco.Engage.Infrastructure.Analytics.Collection.Visitor;

namespace Umbraco.Example;

public class ExampleService
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    private readonly IAnalyticsVisitorExternalIdHandler _externalIdHandler;
    private readonly IAbTestingVisitorService _abTestingVisitorService;

    public ExampleService(
        IHttpContextAccessor httpContextAccessor,
        IUmbracoContextAccessor umbracoContextAccessor,
        IAnalyticsVisitorExternalIdHandler externalIdHandler,
        IAbTestingVisitorService abTestingVisitorService)
    {
        _httpContextAccessor = httpContextAccessor;
        _umbracoContextAccessor = umbracoContextAccessor;
        _externalIdHandler = externalIdHandler ;
        _abTestingVisitorService = abTestingVisitorService;
    }

    /// <summary>
    /// Gets the active A/B test variants for the current visitor using GUID keys (recommended).
    /// </summary>
    /// <returns>Active <see cref="AbTestVariant"/>s for the visitor, or an empty list if unavailable.</returns>
    public IEnumerable<AbTestVariant> GetCurrentVisitorActiveAbTestVariants()
    {
        if (_httpContextAccessor?.HttpContext is not HttpContext httpCtx ||
            _externalIdHandler.GetExternalId(httpCtx) is not Guid externalId)
            return [];

        if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbCtx) ||
            umbCtx.PublishedRequest?.Culture is not string culture ||
            umbCtx.PublishedRequest?.PublishedContent is not IPublishedContent content)
            return [];

        // Using GUID-based overload (recommended)
        return _abTestingVisitorService.GetVisitorAbTestVariants(
            externalId,
            content.Key,
            content.ContentType.Key,
            culture);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.umbraco.com/umbraco-engage/17.latest/developers/ab-testing/csharp-api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
