实验:patch

需要授权

更新一个现有的实验。此方法支持修补语义。 查看示例

除了标准参数之外,此方法还支持参数表中列出的参数。

请求

HTTP 请求

PATCH https://www.googleapis.com/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/profiles/profileId/experiments/experimentId

参数

参数名称 说明
路径参数
accountId string 要更新的实验的帐户 ID。
experimentId string 要更新的实验的实验 ID。
profileId string 要更新的实验的数据视图(配置文件)ID。
webPropertyId string 要更新的实验的网络媒体资源 ID。

授权

此请求至少需要获得以下任一范围的授权(详细了解身份验证和授权)。

范围
https://www.googleapis.com/auth/analytics
https://www.googleapis.com/auth/analytics.edit

请求正文

在请求正文中,根据修补语义的规则提供 management.experiment 资源的具备以下属性的相关部分:

属性名称 说明 备注
必需属性
id string 实验 ID。在进行修补和更新时必须提供。创建时不可用。
可选属性
description string 关于此实验的备注。 可写入
editableInGaUi boolean 如果为真,则最终用户可以通过 Google Analytics(分析)界面来编辑实验。 可写入
equalWeighting boolean 指明是否要在各变体之间均匀分配流量的布尔值。如果此值为 False,内容实验将遵循默认行为,即根据变体效果动态调整流量。可选 -- 默认为 False。对于状态为 ENDED 的实验,此字段不可更改。 可写入
minimumExperimentLengthInDays integer 在 [3, 90] 范围内的整数。指定实验的最短时间。对于运行中的实验,可以修改此字段。对于状态为 ENDED 的实验,此字段不可更改。 可写入
name string 实验名称。对于状态为 ENDED 的实验,此字段不可更改。创建实验时,此字段为必填。 可写入
objectiveMetric string 该实验正在进行优化的指标。有效的值包括:“ga:goal(n)Completions”、“ga:adsenseAdsClicks”、“ga:adsenseAdsViewed”、“ga:adsenseRevenue”、“ga:bounces”、“ga:pageviews”、“ga:sessionDuration”、“ga:transactions”、 “ga:transactionRevenue”。当状态为“RUNNING”,并且 servingFramework 是“REDIRECT”或“API”时,此字段为必填。 可写入
optimizationType string 指明 objectiveMetric 应该最小化还是最大化。可取的值为:“MAXIMUM”、“MINIMUM”。可选 -- 默认为“MAXIMUM”。 没有 objectiveMetric 则不能指定。状态为“RUNNING”或“ENDED”时不可修改。 可写入
rewriteVariationUrlsAsOriginal boolean 指明是否要重写变体网址以与原始版本的网址匹配的布尔值。对于状态为 ENDED 的实验,此字段不可更改。 可写入
servingFramework string 用于投放实验变体和评估实验结果的框架。为以下类型之一:
  • REDIRECT:Google Analytics(分析)将流量重定向到不同的变体页面,报告所选的变体并评估结果。
  • API:Google Analytics(分析)选择并报告要投放的变体,然后评估结果;调用者负责投放所选变体。
  • EXTERNAL:在外部投放变体,并将所选变体报告给 Google Analytics(分析)。调用者负责投放所选变体和评估结果。
可写入
status string 实验状态。可能的值有:“DRAFT”、“READY_TO_RUN”、“RUNNING”、“ENDED”。实验可以创建为“DRAFT”、“READY_TO_RUN”或“RUNNING”状态。创建实验时,此字段为必填。 可写入
trafficCoverage double 一个介于 0 和 1 之间的浮点数。指定用于实验的很小一部分流量。对于运行中的实验,可以修改此字段。对于状态为 ENDED 的实验,此字段不可更改。 可写入
variations[] list 变体的数组。数组中的第一个变体为原始版本。当实验处于 RUNNING 状态时,变体数量不可更改。变体数量必须至少为两个,才可以将状态设置为 RUNNING。 可写入
variations[].name string 变体名称。创建实验时,此字段为必填。对于状态为 ENDED 的实验,此字段不可更改。 可写入
variations[].status string 变体的状态。可能的值有:“ACTIVE”、“INACTIVE”。状态为 INACTIVE 的变体将不会投放。对于状态为 ENDED 的实验,此字段不可更改。 可写入
variations[].url string 变体的网址。对于状态为 RUNNING 或 ENDED 的实验,此字段不可更改。 可写入
winnerConfidenceLevel double 一个介于 0 和 1 之间的浮点数。指定为选出胜者所必需的置信度。对于状态为 ENDED 的实验,此字段不可更改。 可写入

响应

如果成功,此方法将在响应正文中返回一个 management.experiment 资源

示例

备注:此方法的代码示例并未列出所有受支持的编程语言(请参阅客户端库页面,查看受支持的语言列表)。

Java

使用 Java 客户端库

/*
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Experiments Developer Guide for details.
 */

// This example patches an existing experiment with new variations.
Variations variationA = new Variations();
variationA.setName("Home A");
variationA.setUrl("homeA.html");
Variations variationB = new Variations();
variationB.setName("Home B");
variationB.setUrl("homeB.html");
List<Variations> variations = Arrays.asList(variationA, variationB);

try {
  // First get an existing Experiment.
  Experiment body = analytics.management().experiments().get("123456",
      "UA-123456-1", "7654321", "122333444455555").execute();

  // Set the new variations.
  body.setVariations(variations);

  // Call Patch with the updated experiment.
  analytics.management().experiments().patch("123456", "UA-123456-1",
      "7654321", "12233344455555", body).execute();
} catch (GoogleJsonResponseException e) {
  System.err.println("There was a service error: "
      + e.getDetails().getCode() + " : "
      + e.getDetails().getMessage());
}

PHP

使用 PHP 客户端库

/**
 * Note: This code assumes you have an authorized Analytics service object.
 * See the Experiments Developer Guide for details.
 */

/**
 * This request patches an existing experiment.
 */

// Construct the first variation.
$variationA = new Google_Service_Analytics_ExperimentVariations();
$variationA->setName('VariationA');
$variationA->setUrl('index.html');

// Construct the second variation.
$variationB = new Google_Service_Analytics_ExperimentVariations();
$variationB->setName('VariationB');
$variationB->setUrl('indexB.html');

try {
  // Get an existing Experiment.
  $experiment = $analytics->management_experiments->get('123456',
      'UA-123456-1', '7654321', '122333444455555');

  // Set the new variations.
  $experiment->setVariations(array($variationA, $variationB));

  // Call the patch method with the updated experiment.
  $analytics->management_experiments->patch('123456', 'UA-123456-1',
      '7654321', '122333444455555', $experiment);
} catch (apiServiceException $e) {
  print 'There was an Analytics API service error '
      . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error '
      . $e->getCode() . ':' . $e->getMessage();
}

Python

使用 Python 客户端库

# Note: This code assumes you have an authorized Analytics service object.
# See the Experiments Developer Guide for details.

# This request patches an existing experiment with a new name,
# and new variations.
try:
  analytics.management().experiments().patch(
      accountId='123456',
      webPropertyId='UA-123456-1',
      profileId='7654321',
      experimentId='ABCDEFG123456abcdefg',
      body={
          'name': 'Landing Page Test April',
          'variations': [
              {
                  'name': 'First Variation',
                  'url': 'index.html'
              },
              {
                  'name': 'Proposed Change',
                  'url': 'indexB.html'
              }
          ]
      }
  ).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

JavaScript

使用 JavaScript 客户端库

/*
 * Note: This code assumes you have an authorized Analytics client object.
 * See the Experiments Developer Guide for details.
 */

/*
 * This request patches an existing experiment.
 */
function patchExperiment() {
  var request = gapi.client.analytics.management.experiments.patch(
    {
      'accountId': '123456',
      'webPropertyId': 'UA-123456-1',
      'profileId': '7654321',
      'experimentId': '122333444455555',
      'resource': {
        'name': 'Landing Page Test',
        'status': 'DRAFT',
        'variations': [
          {
            'name': 'VariationA',
            'url': 'index.html'
          },
          {
            'name': 'VariationB',
            'url': 'indexB.html'
          }
        ]
      }
    });
  request.execute(function (response) { // Handle the response. });
}