Area: Optimizely CMS
Applies to versions: 11.9.0 and higher

Session handling in visitor group criteria

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

You can use the built-in Optimizely visitor group criteria without requiring session state on the server side by manually disable ASP.NET session states. The visitor group system can autodetect if session state has been disabled and switch to a cookie-based approach for visitor group criteria that previously used sessions. You can also customize your own storage of users' visitor group sessions.

The RaiseStartSession event

If the session is enabled, then the RaiseStartSession event is raised by AspNet session start event. If the session is disabled, then the RaiseStartSession event is raised by HttpApplication AcquireRequestState event.

The Session State

The session-based visitor group criterion saves its session state inside session, cookie, or customized state storage. If the session is enabled and there is no registered custom storage, then it uses http session object as storage. If the session is disabled and there is no registered custom storage, then it uses http cookie as storage.

Enabling and disabling session state

By default, the http session object is used if the AspNet session state mode is not Off. It can be changed explicitly by setting false the property EnableSession on the VisitorGroupOptions options.

Customize State storage

To customize storage, it is needed to implement the IStateStorage interface and register it. 

This sample code shows both customized state storage and disabling session state.

internal class InMemoryStateStorage : IStateStorage
     IDictionary<string, string> _states = new Dictionary<string, string>();
     public bool IsAvailable => true;
     public object Load(string key)
          _states.TryGetValue(key, out string value);
          return value;

     public void Save(string key, object value)
          _states[key] = (string)value;

     public void Delete(string key)

and the custom state storage can be registered in IServiceCollection in the startup, like this:

public class Startup
     public void ConfigureServices(IServiceCollection services)
        services.Add<VisitorGroupOptions>((s) => new VisitorGroupOptions(), ServiceInstanceScope.Singleton); 
        services.Configure<VisitorGroupOptions>(s => s.EnableSession = EnableSession); 
        services.Add<IStateStorage>(s => new InMemoryStateStorage(), ServiceInstanceScope.Singleton);
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading