Server-side Experiments

In the standard implementation of Google Optimize experiments, the browser executes JavaScript in the web page to determine which variant to serve. These experiments, while easy to implement, are limited to client-side changes, for example, changing the style or position of an element on a rendered web page.

In a server-side experiment, your code must perform all the tasks that Optimize handles in a client-side experiment. For example, your code will target audiences and consistently serve the appropriate variant to each user. You only use the Optimize interface to create experiments, set objectives, create variants, and view reports.

This article describes how to use Optimize to report on experiments running on your server or other Internet-connected device.

When running server-side experiments with this method, you'll manage:

  • Serving content.
  • Experiment inclusion.
  • Variant allocation.
  • Persistence of variants for users.
  • Sending users’ hits (e.g., pageview hits) on the variants to Google Analytics.

To run a server-side experiment against variants of a web page:

  1. Create an experiment in Optimize.
  2. Set experiment objectives.
  3. Set experiment targeting.
  4. Create variants.
  5. Start the experiment.
  6. Get the experiment ID.

When the experiment is running:

  1. Set the experiment ID and choose a variant from the server.
  2. Add Google Analytics tracking code to variants.

Create an experiment in Optimize

When using this method, you can create and run an A/B test or multivariate test (MVT).

To create an A/B test:

  1. Open your Optimize account.
  2. Select a container.
  4. Enter an experiment name.
  5. The URL field is ignored here. Enter a placeholder URL that doesn't exist on your website.
  6. Select A/B test.
  7. Click CREATE.

Set experiment objectives

These objectives will be used to measure the results of your test.

  1. In your experiment page, click OBJECTIVES under Configuration.
  2. Select a primary objective from the dropdown list.
  3. Click SAVE.

Set experiment targeting

Your server-side code handles experiment targeting, so Optimize does not use the information you enter here. However, Optimize requires a URL rule to be set.

  1. In your experiment page, click TARGETING under Configuration.
  2. Click CREATE RULE.
  3. Click URLs.
  4. Select the equals match type and enter SERVER_SIDE for the value. Do not use a URL or string that begins with "http" or "https" because if your site is tagged with Optimize, this server-side experiment will never be triggered for your site.

    targeting URL value

  5. Click ADD.
  6. Click SAVE.

Create variants

Repeat the following steps to create variants:

  1. In your experiment page, click NEW VARIANT under Variants.
  2. Enter a variant name (e.g. Variant 1).
  3. Click ADD.
  4. Click SAVE.

Start the experiment

In your experiment page, click START EXPERIMENT.

start experiment

Get the experiment ID

The experiment ID is shown on the right-hand panel on the experiment details page:

experiment ID

Implement experiment

An experiment has an experiment ID and two or more variants, including the original. Each variant has a variation ID which is an index starting at 0. For example, if a web page has 6 variants, the variation IDs will be the same as the order of variations in the UI, starting with 0 for the original.

Set the experiment ID and choose a variant from the server

If an experiment is running on a web page, when a user opens the page, the server returns a variant to the browser. Because your users' experience will be more consistent if you continually serve them the same variant, we suggest a variant distribution approach that allows for this, such as an ID hashing algorithm. Additionally, if multiple different variants for the same experiment are seen by a single user in one session, that session will not be counted in the experiment. For example, the following code picks a variant for an experiment with 3 variants:

// Sets the ID of the experiment on variations of this web page.
$experimentId = '16iQisXuS1qwXDixwB-EWgQ';

// Randomly picks a variation for the user.
$variationId = rand(0, 2);

Add Google Analytics tracking code to variants

For each variant, add Google Analytics tracking code to:

  1. Load the analytics.js library.
  2. Create a tracker.
  3. Set the experiment ID and variation ID.
  4. Send at least one hit (pageview, e.g.) to Google Analytics.

For example:

  // 1. Load the analytics.js library.
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  // 2. Create a tracker.
  ga('create', 'UA-XXXXX-Y', 'auto');

  // 3. Set the experiment ID and variation ID.
  ga('set', 'exp', '$experimentId.$variationId');
  // 4. Send a pageview hit to Google Analytics.
  ga('send', 'pageview');
<!-- Content -->

Report multivariate test hits

Multivariate tests (MVT) test two or more elements, or sections, to understand their effects on each other. Instead of showing which page variant is most effective (as in an A/B experiment), a multivariate test identifies the most effective combination of variants. For example, an MVT experiment may have two sections named "Color" and "Font": the "Color" section might have three variants (red, green, and blue) but the "Font" section might have only two variants (10pt and 12pt).

Your server will need to select a variant for each section, and then report that information to Google Analytics. The procedure is the same as it is for an A/B test, with the exception of setting the variants.

To report the variant for an MVT experiment, delimit the selected variant of each section by "-":

[variant for section 1]-[variant for section 2]-...-[variant for section N]

For example, the following lists three variants of the above MVT experiment. So, given the above MVT experiment with color and font sections (a color section with four variants: 0 = original, 1 = red, 2 = green, 3 = blue and a font section section with three variants: 0 = original, 1 = 10pt, 2 = 12pt):

  • "0-0" indicates the original color and font
  • "1-0" indicates red and the original font
  • "2-1" indicates green and a 10pt font

For example, the following code selects 0-0 (the original color and font) for the experiment:

ga('set', 'exp', '$experimentId.0-0');