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 turning off ASP.NET session states. The visitor group system can autodetect if the session state was disabled and switch to a cookie-based approach for visitor group criteria that previously used sessions. You can also customize the 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 a session, cookie, or customized state storage. If the session is enabled and there is no registered custom storage, then it uses the HTTP session object as storage. If the session is disabled and there is no registered custom storage, then it uses the HTTP cookie as storage.
Enable and disable session state
By default, CMS uses the HTTP session object if the AspNet
session state mode is not Off. You can change it explicitly by setting the property EnableSession
to false on the VisitorGroupOptions
options.
Customize State storage
To customize storage, implement the IStateStorage
interface and register it. This sample code shows 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 you can register the custom state storage 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 6 months ago