• websites



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.


Import the amp-experiment component.

<script async custom-element="amp-experiment" src=""></script>

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

<script async custom-element="amp-analytics" src=""></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;

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.

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

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=""></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)"

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