Configuring cookies for Experiments
Experiments rely on cookies to store session data to ensure a visitor is always viewing the same Content Variant.
Experiment cookies
An Experiment defines variations of content (Content Variants) to display in a region of a webpage. The Content Variants are displayed randomly to website visitors within a given time frame. The objective of an Experiment is to see which Content Variant performed the best (resulted in the most conversions).
Experience Optimization writes Experiment cookies store session data about the Content Variant a particular visitor is viewing and makes sure that visitor always sees the same one. Sometimes, Experience Optimization cannot read and write cookies directly, and instead writes a JavaScript cookie. This can happen in the following circumstances:
- Experience Optimization cannot write the cookies in the header, such as when the response buffer is flushed.
- You are using
REL.
Response buffer size
By default, Experience Optimization tries to write httpOnly cookies; however, 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. 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 you may want to consider changing your web server's 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. To support Experiments, you need to add code to create cookies and add them to the Content Delivery web service request. The cookies are written back by adding a piece of JavaScript to the rendered content.
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);
}
}