Session handling in visitor group criteria
Describes session handling in visitor group criteria.
Note
This content applies to Optimizely Content Management System (CMS) versions 11.9.0 and higher.
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 was 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.
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.
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.
Enable 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)
{
_states.Remove(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);
}
}
Updated 5 months ago