Cookies for Experiments

Content Experiments rely on cookies to store session data about the Content Variant a visitor is viewing and make sure they always see the same one. By default, Experience Optimization tries to write httpOnly cookies. But if you are using REL, or if it cannot write the cookies in the header (for example, when the response buffer is flushed), Experience Optimization cannot directly read and write cookies. In such circumstances, a JavaScript cookie is written.

Changing the buffer size
When the response buffer is flushed (that is, it is kept low for performance reasons), the header is sent in the response before Experience Optimization can write the httpOnly cookie. In this case, the tags or controls will output JavaScript to set a cookie to avoid invalidating the Experiment.
In most cases it is preferable to write the httpOnly cookie, so consider changing your Web Server buffer size.
Writing out cookies for REL–based implementation
If your Output Format is REL (Render Engine Language), the Content Delivery Web service renders content at request time. Since REL has no Web context, Experience Optimization cannot directly read and write cookies.
If your Output Format is REL, to support Experiments you need to add code to create cookies. The code adds cookies to the Content Delivery Web service request. The cookies are written back by adding a piece of JavaScript to the rendered content.
The following is an example of how to write out cookies (the actual code depends on your Web site language and SDL Tridion Sites implementation):
String createCookiesString(Cookie[] cookies)
{
  List<String> cookieKvps = new ArrayList<String>();
  for(Cookie cookie : cookies)
  {	
    cookieKvps.add(String.format("%s=%s", cookie.getName(), cookie.getValue())); }
    return StringUtils.join(cookieKvps, ";");
  }
  URL url = new URL("http://localhost:8080/staging_web/ws/odata.svc/PageContents(PageId=216,PublicationId=3)/Content");
  URLConnection connection = url.openConnection();
  String cookieString = "";
  if(request.getCookies() != null)
  { 
    cookieString = createCookiesString(request.getCookies()); 
    connection.setRequestProperty("Cookie", cookieString);
  }
}