Description Conduct user experience experiments (such as A/B testing and multivariate testing) on an AMP document and collect corresponding data with amp-pixel or amp-analytics.
Required Script <script async custom-element="amp-experiment" src="https://cdn.ampproject.org/v0/amp-experiment-0.1.js"></script>
Examples Annotated code example for A/B tests with amp-experiment

Behavior

The <amp-experiment> element is used to conduct user experience experiments (such as A/B testing and multivariate testing) on an AMP document. It provides hooks to define customizable variants and allocates traffic to each of the variants based on the configuration. For each page view, the variant allocation is also exposed to amp-pixel and amp-analytics so that the necessary data can be collected to perform statistical comparison across variants.

A user-sticky variant assignment is supported to provide a consistent user experience to the same client. This functionality relies upon AMP’s Client ID capability to provide random values that are consistent across page views. Please be aware that usage of this feature with this behavior might require updating your privacy policy, or obtaining end user consent in some jurisdictions. (If this is relevant for you, please see consentNotificationId below.)

Multiple experiments can be run on the same AMP document in parallel with their own sets of variants. In user sticky mode, the allocations are orthogonal among different experiments, meaning there will be no correlation between 2 variants (user groups) that are from different experiments.

Configuration

The configuration of the experiments is specified in a JSON object.

<amp-experiment>
  <script type="application/json">
    {
      aExperiment: {
        sticky: true,
        consentNotificationId: "consent-notif",
        variants: {
          treatment1: 12.5,
          treatment2: 12.5,
          treatment3: 25.0,
        },
      },
      bExperiment: {...}
    }
  </script>
</amp-experiment>

At top level, the JSON is a map of experiment configurations keyed by experiment names. In each experiment, available settings are described in the table below:

Name Is required field? Description
sticky No, default=true Whether the experiment assignment is sticky for a user or not.
consentNotificationIdNo, default=undefined The element ID of the amp-user-notification to be dismissed before a sticky experiment can be conducted. To not block the page rendering, an experiment with this field specified will be skipped if the consent is not provided prior to the current visit. That’s to say, only returning visits with user consent can trigger such an experiment. This setting is only relevant when sticky=true.
variants Yes A name-to-percentage map where percentage is a float number in range (0, 100) that indicates the amount of traffic will be allocated to the variant. Variants don’t have to sum up to 100%. In that case, there’ll be a portion of the traffic allocated to a variant named none, which is a reserved keyword that indicates no variant was allocated.
Advanced settings
cidScope No, default=amp-experiment The CID scope for user sticky experiment. Only useful when you want to reuse an existing CID. This setting is only relevant when sticky=true.
group No, default={experimentName}Experiments with the same group name will share the same CID space. Only useful when multiple experiments want to have correlated user grouping. This setting is only relevant when sticky=true.

Characters used in the experiment name and variant name are restricted to [a-z,A-Z,0-9,-,_]. none is a reserved keyword and cannot be used.

Style a variant

For each experiment, the allocated variant is exposed as attribute of the body element of the document.

<body amp-x-aExperiment="treatment1" amp-x-bExperiment="treatment3">

Notice that the experiment name is prefixed by amp-x- to avoid naming conflict. Experiments with no variant allocated are ignored.

Use CSS attribute selector to style the document. For example, the code below hide a test banner for the treatment1 group of experiment aExperiment:

body[amp-x-aExperiment="treatment1"] .test-banner {
  display: none;
}

Reporting

Allocated variants are available as a URL substitution variable: VARIANT(experiment)

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

For experiments with no variants allocated, this variable resolves to string literal none.

Variable VARIANTS returns all variants serialized in the format of

{experiment1}.{variant}!{experiment2}.{variant}...

For example, the URL https://example.com?variants=VARIANTS expands to:

https://example.com?variants=aExperiment.treatmentA!bExperiment.treatmentB

Override variant allocation

An experiment can be forced to a variant via URL fragment. This is useful in development.

https://example.com/amparticle#amp-x-experiment=treatment

Notice the same amp-x- prefix used as in body attributes.

Validation

One AMP document can have at most one amp-experiment element. See amp-experiment rules in the AMP validator specification.