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, such as 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 targets audiences and consistently serves 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 devices. Choose from the following options based on the Analytics measurement code you've added to your site:

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 measurement 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.
  3. Click Create experience.
  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

Use objectives to measure the results of your experiment.

  1. In your experiment page, click Add experiment objective under Measurement and objectives.
  2. Select a primary objective from the dropdown list.

Set experiment targeting

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

To set experiment targeting:

  1. In your experiment page, click Add variant under Targeting and variants.
  2. Enter a variant name (e.g. Variant 1) and click Done.
  3. Click + Add URL rule under Page targeting.
  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.

Create variants

To create variants:

  1. In your experiment page, click + Add variant under Variants.
  2. Enter a variant name (e.g. Variant 1) and click Done.
  3. Click Add.

Start the experiment

In your experiment page, click Start.

start experiment

Get the experiment ID

The experiment ID is available under Google Analytics in the Measurement and objectives section.

experiment ID

Implement experiment

An experiment has an experiment ID and two or more variants, including the original. Each variant has a variant ID, which is an index starting at 0. For example, if a web page has 6 variants, the variant IDs will be the same as the order of variants 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 variants of this web page.
$experimentId = '16iQisXuS1qwXDixwB-EWgQ';

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

Add Google Analytics measurement code to variants

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) and the "Font" section might have 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: