Area: Optimizely CMS
Applies to versions: 11.9.0 and higher

Session handling in visitor group criteria

Recommended reading 

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 , IConfigurableModule
     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)
     public void ConfigureContainer(ServiceConfigurationContext context)
          context.Services.Add<VisitorGroupOptions>((s) => new VisitorGroupOptions(), ServiceInstanceScope.Singleton); 
          context.Services.Configure<VisitorGroupOptions>(s => s.EnableSession = EnableSession); 
          context.Services.Add<IStateStorage>(s => new InMemoryStateStorage(), ServiceInstanceScope.Singleton);
     public void Initialize(InitializationEngine context){}
     public void Uninitialize(InitializationEngine context){}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jun 18, 2018

Recommended reading