需要授權
建立新實驗。 參閱範例。
除了標準參數以外,這個方法也支援參數表格中列出的參數。
要求
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 |
若為 true,使用者可以透過 Google Analytics (分析) 使用者介面修改實驗。 | 可寫入 |
equalWeighting |
boolean |
布林值,可指定是否將流量平均分配到所有變化版本。如果值為 False,內容實驗會採用預設行為,根據變化版本成效動態調整流量。選用 -- 預設為 False。如果實驗狀態為「ENDED」,則無法更改這個欄位。 | 可寫入 |
minimumExperimentLengthInDays |
integer |
以 [3, 90] 表示的整數。指定實驗的最短長度。執行中的實驗可以變更。如果實驗狀態為「ENDED」,則無法更改這個欄位。 | 可寫入 |
objectiveMetric |
string |
實驗要最佳化的指標。有效值:「ga:goal(n)Completes」、「ga:adsenseAdsClicks」、「ga:adsenseAdsViewed」、「ga:adsenseRevenue」、「ga:bounces」、「ga:pageviews」、「ga:sessionDuration」、「ga:transactions」、「ga:transactionRevenue」。如果狀態為「RUNNING」,而放送 Framework 為「REDIRECT」或「API」,則此為必填欄位。 | 可寫入 |
optimizationType |
string |
是否應最小化或最大化 goalMetric。可能的值:「MAXIMUM」、「MINIMUM」。選用:預設為「MAXIMUM」。如果不使用 goalMetric,就無法指定目標指標。狀態為「RUNNING」或「ENDED」時無法修改。 | 可寫入 |
rewriteVariationUrlsAsOriginal |
boolean |
布林值,用於指定是否要重新編寫變化網址以符合原始版本的網址。如果實驗狀態為「ENDED」,則無法更改這個欄位。 | 可寫入 |
servingFramework |
string |
用於放送實驗變化版本和評估結果的架構。以下其中之一:
|
可寫入 |
trafficCoverage |
double |
介於 0 和 1 之間的浮點數。指定參與實驗的流量比例。執行中的實驗可以變更。如果實驗狀態為「ENDED」,則無法更改這個欄位。 | 可寫入 |
variations[] |
list |
變化版本的陣列。陣列中的第一個變體為原始版本。實驗進入「執行中」狀態後,變化版本數量不得變動。必須先提供至少兩種變化版本,狀態才能設為「執行中」。 | 可寫入 |
variations[].status |
string |
變化形式的狀態。可能的值:「ACTIVE」、「INACTIVE」。系統不會放送「有效」的變化版本。如果實驗狀態為「ENDED」,則無法更改這個欄位。 | 可寫入 |
variations[].url |
string |
變化版本的網址。如果實驗狀態為「執行中」或「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
/* * 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. }); }