AMP

amp-experiment

Description

Used to conduct user experience experiments on AMP pages.

 

Required Scripts

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

Usage

Use the amp-experiment component to conduct user experience experiments, such as A/B testing and multivariate testing, and collect corresponding data.

<amp-experiment> 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.
consentNotificationId No, 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 that is greater than 0 and less than 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 will 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.

Analytics

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

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

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.

Styling

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"></body>

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;
}

Validation

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

需要更多帮助?

您已多次阅读本文档,但它仍未能涵盖您的所有问题?也许其他人也这么觉得:在 Stack Overflow 上与他们联系。

前往 Stack Overflow
发现错误或缺少功能?

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

前往 GitHub