Do you build things with AMP? Fill out the AMP Developer Survey!
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에서 샘플 수정하기