Set a test cookie: Preview a live campaign or experiment

  • Updated

This topic describes how to:

  • Use a "test cookie" or query parameter to browse an experiment or campaign as if it is live
  • Set a test cookie on your page, then target based on it
  • Show a live experiment or campaign to an internal audience


When you QA, it is often useful to be able to start an experiment or campaign live on your site but allow only internal employees to see it.

Setting a test cookie allows you to show a running campaign or experiment to a set of internal stakeholders. You will create a bookmarklet in the browser that sets the cookie. Then, use audience conditions to target the cookie. Visitors who have the test cookie will be able to navigate across multiple pages, and fire events in the live experiment or campaign.

In Optimizely Web Experimentation Testing, the test cookie is a key tool in the Pre-Launch Checklist for QA.

In Optimizely Web Personalization, the test cookie will help you share running campaigns. Use the Preview tool to check that your campaign looks and works the way you would like before you publish. Then, use the test cookie to share the live campaign with internal holders for QA.

Read on to learn how to set up and target a test cookie.

Cookie targeting is not available to all plan types. If you do not see this feature in your Optimizely Web Experimentation account, please refer to our Optimizely Web Experimentation package matrices. You can also share the campaign with internal holders for QA by using the Share Link, Cross-Browser Testing, and Force Query Parameters


You will set the test cookie in every browser used to QA with this method.

To set a test cookie, add a bookmark to the browser. Then, replace the URL for this bookmark with the block of JavaScript code below. 

  1. First, open a browser in Chrome and create a bookmark on any page.


  2. Click the bookmark to change the name to Set test cookie. Select Done to confirm.

  3. In the Chrome toolbar, select Bookmarks Bookmark Manager. Click to edit the bookmark.

    Swap the URL with the following JavaScript function:

    javascript: (function () { var hostname = window.location.hostname; var parts = hostname.split("."); var publicSuffix = hostname; var last = parts[parts.length - 1]; var expireDate = new Date(); expireDate.setDate(expireDate.getDate() + 7); var TOP_LEVEL_DOMAINS = [ "com", "local", "net", "org", "top", "xxx", "edu", "es", "gov", "biz", "info", "fr", "gr", "nl", "ca", "de", "kr", "it", "in", "me", "ly", "tv", "mx", "cn", "jp", "il", "in", "iq", "ga", "tk", "us",]; var SPECIAL_DOMAINS = ["bb", "ck", "cr", "in", "id", "jp", "uk", "au", "nz", "za", "kr", "th", "il", "ve"]; if (parts.length > 2 && SPECIAL_DOMAINS.indexOf(last) != -1) { publicSuffix = parts[parts.length - 3] + "." + parts[parts.length - 2] + "." + last; } else if (parts.length > 1 && TOP_LEVEL_DOMAINS.indexOf(last) != -1) { publicSuffix = parts[parts.length - 2] + "." + last; }; document.cookie = "optly_" + publicSuffix.split(".")[0] + "_test=true; domain=." + publicSuffix + "; path=/; expires=" + expireDate.toGMTString() + ";";})();


  4. Next, navigate to the page of your site where you would like to set the test cookie.

  5. Click the bookmark to create a test cookie.

    You now have a cookie set on whichever domain you are browsing. The cookie will look something like this: optly_YOURDOMAINNAME_test=true

When you reload the page, you will be eligible to enter an experiment or campaigns that target that cookie.

To create a test cookie in Internet Explorer, you can apply the following JavaScript code to your console

javascript: (function () {
    var a, domain;
    a = document.createElement("a");
    a.href = document.URL;
    domain = a.hostname;
    domain = domain.replace(/^www\./, "");
    document.cookie = "optly_YOURDOMAINNAME_test=true;Domain=" + domain + ";Path=/";

Target the test cookie

After you have set your test cookie, use an audience condition to target it. Only visitors who have the test cookie will be eligible to enter the experiment or campaign.


  1. Navigate to the Experiments dashboard and choose the experiment or campaign.

  2. In the Manage Experiments sidebar, click Audiences.

  3. Click Create New Audience.

  4. Enter a name for the audience.

  5. Drag Cookie from the list of available conditions into the Audience Conditions.

  6. Enter the name of the test cookie, optly_YOURDOMAINNAME_testin the text box and set to has any value.

  7. Click Save Audience.

Target a query parameter

If your Optimizely Web Experimentation plan does not include targeting by cookie, you can use a query parameter instead. This also works well when QAing your experiment on a mobile device.

When you view your page live, you will have to append the query parameter that you selected. For instance, on Google's homepage, this may look like if you use the following query parameter:


Just note that if you use a query parameter instead of a cookie, you will have to re-add the query parameter to the URL when you browse to another page.

Once you're done, click Apply and then save the experiment.

Run the experiment or campaign

Now that you have set the test cookie, start the experiment or campaign to view it live.

To see the experiment or campaign, you still need to meet all of the targeting conditions. But once you are bucketed into a variation or experience, you can navigate around the site and stay in that variation. You can also test whether your goals are firing correctly.

Remove the test cookie

When you are ready to make the experiment live for other visitors, head back to targeting. Remove the cookie restriction by clicking the red x next to it. Remember to click Apply to save the change to the experiment.

For more information

For an in-depth tutorial on how to verify that your goals are firing in Optimizely Web Experimentation, check out this article on Troubleshooting: Metrics do not track correctly in Optimizely Web Experimentation.

To learn how to force yourself into a specific variation of your running experiment using query parameters in Optimizely Web Experimentation, check out this article on how to Force behaviors in Optimizely using query parameters.