Published on, Time to read
🕒 2 min read

Optimizely / Episerver: How to get anonymous user ID via AnonymousIdFeature?

Optimizely / Episerver: How to get anonymous user ID via AnonymousIdFeature?

Optimizely allows to obtain the ID of the anonymous user, even if the user is logged in. One of the example of the usage is merging cart when the user logs in.

How it works?

The AnonymousIdFeature:

  • is the HTTP context feature
    • is a part of the AnonymousIdMiddleware
  • retrieves a cookie value named EPiServer_Commerce_AnonymousId.

It's definition is as the following:

public interface IAnonymousIdFeature
{
    string AnonymousId { get; set; }
}

The AnonymousIdMiddleware needs to be registered in the Startup.cs file:

app.UseAnonymousId();

Then, we can retrieve ID of anonymous user by calling:

var httpContext = _httpContextAccessor.HttpContext;

string? idOfAnonymousAsString = httpContext.Features.Get<IAnonymousIdFeature>()?.AnonymousId;

var canBeParsed = Guid.TryParse(idOfAnonymousAsString, out Guid idOfAnonymous);

Example of usage

The anonymous ID can be used when the user logs in and there's a need to merge the cart. Then, we can pass the anonymous ID to the IOrderRepository.Load in order to load the cart of the anonymous user and merge it with the cart of the logged in user.

Please note that cart migration might be a complex operation as we need to handle migration of the cart items, addresses, payments, shipments, promotions, etc. Simple migration can be done by the Optimizely class ProfileMigrator which is described as follows:

Class to migrate orders/carts/wishlists when a customer logs in.

The migrator uses ClassMigrator under the hood which is public but intended to be used by the Optimizely classes only. However, we might look into the the implementation in order to understand how the migration is done.