AMP
  • websites

amp-experiment

Introduction

The amp-experiment component allows to perform user experience experiments on an AMP document and collect resulting data. This is, for example, good for A/B testing new features in your AMPs, but you can test as many variants as you want.

In this sample, we're using Google Analytics to track the experiment. Google Analytics requires different variants to be specified via an index variable. However, amp-experiment has no opinion about how these are specified.

Setup

Import the amp-experiment component.

<script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>

We are using amp-analytics to measure results of the experiment.

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>

Style different variants of the experiment by using body[amp-x-name-of-experiment="nameOfVariant"].

<style amp-custom>
  body[amp-x-button-color-experiment="0"] .button-color-experiment {
    background-color: yellow;
    color: black;
  }
  body[amp-x-button-color-experiment="1"] .button-color-experiment {
    background-color: red;
  }
  body[amp-x-button-color-experiment="2"] .button-color-experiment {
    background-color: blue;
  }
</style>

Basic usage

Configure experiments inside the amp-experiment inside a JSON object. Use variants within each experiment to declare how many users in percent will be part of an experiment variant. The total sum of the variants must be a number <= 100. If the sum is < 100, then the default behaviour is used. A user belongs to one of the variants based on a random number generated between 0 and 100.

For more details about variants read the official documentation. Multiple experiments can be run on the same AMP document in parallel with their own sets of variants. One AMP document can have at most one amp-experiment element.

<amp-experiment>
  <script type="application/json">
    {
      "button-color-experiment": {
        "variants": {
          "0": 30,
          "1": 30,
          "2": 30
        }
      }
    }
  </script>
</amp-experiment>

Sample setup

We use a button whose background is going to be yellow for the 30% of users, red for 30% of users, blue for 30% of users and, for the remaining 10% of users; the button background is going to be the default color which is white in this sample.

<button class="button-color-experiment">Click here</button>

Reporting with amp-pixel

Measure results of the experiment using amp-pixel.

<amp-pixel src="https://example.com/track/?xname=button-color-experiment&xvar=VARIANT(button-color-experiment)"></amp-pixel>

Reporting with amp-analytics

Measure results of the experiment using amp-analytics. Allocated variants are available as a URL substitution variable: VARIANT(name-of-the-experiment). You can check the analytics requests in the network tab of the developer tools.

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      "vars": {
        "account": "UA-73836974-1"
      },
      "requests": {
        "experiment": "${pageview}&xid=${experiment}&xvar=${variant}"
      },
      "triggers": {
        "default pageview": {
          "on": "visible",
          "request": "experiment",
          "vars": {
            "experiment": "W4kYemYmQBSTIYNpoezCmg",
            "variant": "VARIANT(button-color-experiment)"
          }
        }
      }
    }
  </script>
</amp-analytics>

Development tip

You can force an experiment to be in a specific variant by adding amp-x-experiment-name to the AMP page URL.

Please note you need to manually reload the page after selecting an experiment as amp-experiment only evaluates on page load and a hash change doesn't trigger a reload.

需要进一步说明?

如果此页面上的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与并做出贡献!我们希望您能成为我们开放源代码社区的持续参与者,但我们也欢迎您对所热衷问题做出一次性贡献。

编辑 GitHub 上的示例