Measure AMP experiments with Optimize

Accelerated Mobile Pages (AMP) use special components like <amp-analytics> to measure activity and <amp-experiment> run experiments. If you use <amp-experiment> to run experiments on AMP documents, you can use Google Optimize to report on the performance of your variants and to select a leader based on your objectives.

This article describes how to use Google Optimize to measure and report on AMP experiments instead of <amp-pixel>. When configured, you can use Google Analytics goals to inform Optimize's Bayesian statistical model and allow Optimize to choose a leader.

Prerequisites

Experiment on Accelerated Mobile Pages (AMP)

To create an Optimize experiment to measure the performance of an <amp-experiment> component:

  1. Confirm that you have the <amp-experiment> and <amp-analytics> components in the <HEAD> of your AMP document. For example:
    <script async custom-element="amp-analytics"
            src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
    <script async custom-element="amp-experiment"
            src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>
    
  2. Create an AB test in your Optimize container. Note the experiment name you entered when creating the A/B test. It will be later referred to as experimentName.
  3. Create variants in your <amp-experiment> component and set their weights. Create matching variants in Optimize and set their weights.

    Optionally, you can use the Optimize visual editor to help create variants.

  4. Set your experiment's objective(s) in Optimize. Objectives inform the statistical model used to pick leaders and determine how your variants perform against your original.
  5. Create an URL targeting rule in Optimize for the AMP document that you wish to target. This isn't used by Optimize, but can be used for reference.
  6. Start your experiment in Optimize and note the Analytics Experiment ID, a 23-character alphanumeric value (e.g. q0AKJL-XRuCmApEIQelcCg) found in the Experiment information panel in Optimize. The Analytics Experiment ID will be later referred to as gaExperimentID.
  7. Paste the correct <amp-experiment> and <amp-analytics> components into your AMP document.

    Example 1: <amp-experiment>

    <amp-experiment>
      <script type="application/json">
        {
          "experimentName": {
            "sticky": true,
            "variants": {
              "0": 33.4,
              "1": 33.3,
              "2": 33.3
            }
          }
        }
      </script>
    </amp-experiment>
    

    Example 2: <amp-analytics>

    <amp-analytics id='analytics1' type='googleanalytics'>
    <script type='application/json'>
    {
      "vars": {
        "account": "gaPropertyID"
      },
      "requests": {
         "experiment": "${pageview}&xid=${xid}&xvar=${xvar}"
      },
      "triggers": {
        "trackPageview": {
          "on": "visible",
          "request": "experiment",
           "vars": {
             "xid": "gaExperimentID",
             "xvar": "VARIANT(experimentName)"
           }
        }
      }
    }
    </script>
    </amp-analytics>
    

    Replace gaPropertyID, gaExperimentID, and experimentName with the values from the steps above.

  8. Confirm that your AMP is receiving experiment traffic by checking the real-time active users on the Optimize container page or the experiment details page.

Example: Use the Optimize CSS code editor

You can use the Optimize CSS code editor to change the style of a variant by adding the appropriate AMP prefix to each CSS rule. For example, if the CSS code editor outputs the following for "variant 1":

.selector {
  property: value
}

you need to add the following prefix before the selector:

body[amp-x-[experimentName]="1"] .selector {
  property: value
}

Replace experimentName with the value from step 2 above.