The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Advanced experiment configuration

Shows code samples you can use in Shared code or Variation code to help satisfy advanced use cases in Optimizely Web Experimentation.

These code samples can be used within Shared code or Variation code to help satisfy advanced use cases within Optimizely.

Execute code for X visits

This JavaScript lets you execute code a certain number of times within an experiment.

For example, you might want to show a popup to visitors only on their first three visits. In this case, you can place this code in either the Experiment JS or the Variation code.

/*
 * Usage
 *   The following allows you to set a limit on the number of times a Code Block will execute for any given visitor.
 */

 // the number of times the code should execute for a given visitor
 var limit = 3;
 // the number of days the evaluation limit should last
 var days = 180;
 // name of the cookie we use as the counter
 var cookieName = 'counterCookie';

 // function to fetch cookie values
 var getCookie = function(name) {
   var match = document.cookie.match(name+'=([^;]*)');
   return match ? match[1] : undefined;
 };

 // function to create cookies
  var setCookie = function(c_name,value,exdays,c_domain) {
    c_domain = (typeof c_domain === "undefined") ? "" : "domain=" + c_domain + ";";
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value + ";" + c_domain + "path=/";
  }

 // logic that counts and limits number of times code can evaluate for given visitor
 if (!getCookie(cookieName)) {
   setCookie(cookieName, 1, days, window.location.hostname);
 } else {
   var numberPops = parseInt(getCookie(cookieName)) + 1;
   setCookie(cookieName, numberPops, days, window.location.hostname);
 }

 if (getCookie(cookieName) <= limit) {
  // INSERT code to evaluate HERE
 }

Change page title

This JavaScript will modify the text of the browser tab when a visitor focuses and un-focuses on the tab. You can display one title when a visitor is focused on the tab and a different title when they toggle to a different tab or window.

/*
 * Usage
 *   The following code will modify the title of the browser tab on the "blur" event and change it back to the original on the "focus" event.
 *
 */

 // store the original tab title
 var origTitle = document.title;

 // function to change title when focusing on tab
 function oldTitle() {
   document.title = origTitle;
 }

 // function to change title when un-focusing on tab
 function newTitle() {
   document.title = 'HELLO WORLD';
 }

 // bind functions to blur and focus events
 window.onblur = newTitle;
 window.onfocus = oldTitle;

Get Optimizely data for custom events

This provides the Optimizely Data Object to return the information necessary to retrieve relevant data. You can use this data with internal tools, the Event API, or custom analytics integrations.

/*
 * Usage
 * Loops through the Optimizely Data Object to return the information necessary to construct the Event API payload.
 *
 */

 var campaign_data;
 var state = optimizely.get('state');

 // return all active campaigns
 campaign_data = state.getCampaignStates({"isActive": true});

 for (var campaign_id in campaign_data) {
    if (campaign_data.hasOwnProperty(campaign_id)) {
       // Returns campaign_id, experiment_id, variation_id
       var data = state.getDecisionObject({"campaignId": campaign_id});
       // code to use data
    }
 }