需要授权
创建一个新实验。 查看示例。
除了标准参数之外,此方法还支持参数表中列出的参数。
请求
HTTP 请求
POST https://www.googleapis.com/analytics/v3/management/accounts/accountId/webproperties/webPropertyId/profiles/profileId/experiments
参数
参数名称 | 值 | 说明 |
---|---|---|
路径参数 | ||
accountId |
string |
要为其创建实验的帐户 ID。 |
profileId |
string |
要为其创建实验的数据视图(配置文件)ID。 |
webPropertyId |
string |
要为其创建实验的网络媒体资源 ID。 |
授权
此请求至少需要获得以下任一范围的授权(详细了解身份验证和授权)。
范围 |
---|
https://www.googleapis.com/auth/analytics |
https://www.googleapis.com/auth/analytics.edit |
请求正文
在请求正文中提供一个具有以下属性的 management.experiment 资源:
属性名称 | 值 | 说明 | 备注 |
---|---|---|---|
必需属性 | |||
name |
string |
实验名称。对于状态为 ENDED 的实验,此字段不可更改。创建实验时,此字段为必填。 | 可写入 |
status |
string |
实验状态。可能的值有:“DRAFT”、“READY_TO_RUN”、“RUNNING”、“ENDED”。实验可以创建为“DRAFT”、“READY_TO_RUN”或“RUNNING”状态。创建实验时,此字段为必填。 | 可写入 |
variations[].name |
string |
变体名称。创建实验时,此字段为必填。对于状态为 ENDED 的实验,此字段不可更改。 | 可写入 |
可选属性 | |||
description |
string |
关于此实验的备注。 | 可写入 |
editableInGaUi |
boolean |
如果为真,则最终用户可以通过 Google Analytics(分析)界面来编辑实验。 | 可写入 |
equalWeighting |
boolean |
指明是否要在各变体之间均匀分配流量的布尔值。如果此值为 False,内容实验将遵循默认行为,即根据变体效果动态调整流量。可选 -- 默认为 False。对于状态为 ENDED 的实验,此字段不可更改。 | 可写入 |
minimumExperimentLengthInDays |
integer |
在 [3, 90] 范围内的整数。指定实验的最短时间。对于运行中的实验,可以修改此字段。对于状态为 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 |
用于投放实验变体和评估实验结果的框架。为以下类型之一:
|
可写入 |
trafficCoverage |
double |
一个介于 0 和 1 之间的浮点数。指定用于实验的很小一部分流量。对于运行中的实验,可以修改此字段。对于状态为 ENDED 的实验,此字段不可更改。 | 可写入 |
variations[] |
list |
变体的数组。数组中的第一个变体为原始版本。当实验处于 RUNNING 状态时,变体数量不可更改。变体数量必须至少为两个,才可以将状态设置为 RUNNING。 | 可写入 |
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 request creates a new Experiment. */ // Construct the body of the request. Experiment body = new Experiment(); body.setName("Landing Page Experiment"); body.setStatus("DRAFT"); // Construct the first variation. Variations variationA = new Variations(); variationA.setName("Variation A"); variationA.setUrl("index.html"); // Construct the second variation. Variations variationB = new Variations(); variationB.setName("Variation B"); variationB.setUrl("indexB.html"); // Set the variations. body.setVariations(Arrays.asList(variationA, variationB)); try { analytics.management().experiments().insert("123456", "UA-123456-1", "7654321", 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 creates a new experiment. */ // Construct the body of the request. $experiment = new Google_Service_Analytics_Experiment(); $experiment->setName('Landing Page Experiment'); $experiment->setStatus('DRAFT'); // 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'); // Set the variations. $experiment->setVariations(array($variationA, $variationB)); try { $analytics->management_experiments->insert('123456', 'UA-123456-1', '7654321', $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. # Example #1: # Creates a new DRAFT experiment with two variations. try: experiments = analytics.management().experiments().insert( accountId='123456', webPropertyId='UA-123456-1', profileId='98765432', body={ 'name': 'Landing Page Test', 'status': 'DRAFT', 'variations': [ { 'name': 'Variation A', 'url': 'index.html' }, { 'name': 'Variation B', '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 creates a new Experiment. */ function insertExperiment() { var request = gapi.client.analytics.management.experiments.insert( { 'accountId': '123456', 'webPropertyId': 'UA-123456-1', 'profileId': '7654321', '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. }); }