Experiments: insert

Requires authorization

Create a new experiment. See an example.

In addition to the standard parameters, this method supports the parameters listed in the parameters table.

Request

HTTP request

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

Parameters

Parameter name Value Description
Path parameters
accountId string Account ID to create the experiment for.
profileId string View (Profile) ID to create the experiment for.
webPropertyId string Web property ID to create the experiment for.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/analytics
https://www.googleapis.com/auth/analytics.edit

Request body

In the request body, supply a management.experiment resource with the following properties:

Property name Value Description Notes
Required Properties
name string Experiment name. This field may not be changed for an experiment whose status is ENDED. This field is required when creating an experiment. writable
status string Experiment status. Possible values: "DRAFT", "READY_TO_RUN", "RUNNING", "ENDED". Experiments can be created in the "DRAFT", "READY_TO_RUN" or "RUNNING" state. This field is required when creating an experiment. writable
variations[].name string The name of the variation. This field is required when creating an experiment. This field may not be changed for an experiment whose status is ENDED. writable
Optional Properties
description string Notes about this experiment. writable
editableInGaUi boolean If true, the end user will be able to edit the experiment via the Google Analytics user interface. writable
equalWeighting boolean Boolean specifying whether to distribute traffic evenly across all variations. If the value is False, content experiments follows the default behavior of adjusting traffic dynamically based on variation performance. Optional -- defaults to False. This field may not be changed for an experiment whose status is ENDED. writable
minimumExperimentLengthInDays integer An integer number in [3, 90]. Specifies the minimum length of the experiment. Can be changed for a running experiment. This field may not be changed for an experiment whose status is ENDED. writable
objectiveMetric string The metric that the experiment is optimizing. Valid values: "ga:goal(n)Completions", "ga:adsenseAdsClicks", "ga:adsenseAdsViewed", "ga:adsenseRevenue", "ga:bounces", "ga:pageviews", "ga:sessionDuration", "ga:transactions", "ga:transactionRevenue". This field is required if status is "RUNNING" and servingFramework is one of "REDIRECT" or "API". writable
optimizationType string Whether the objectiveMetric should be minimized or maximized. Possible values: "MAXIMUM", "MINIMUM". Optional--defaults to "MAXIMUM". Cannot be specified without objectiveMetric. Cannot be modified when status is "RUNNING" or "ENDED". writable
rewriteVariationUrlsAsOriginal boolean Boolean specifying whether variations URLS are rewritten to match those of the original. This field may not be changed for an experiment whose status is ENDED. writable
servingFramework string The framework used to serve the experiment variations and evaluate the results. One of:
  • REDIRECT: Google Analytics redirects traffic to different variation pages, reports the chosen variation and evaluates the results.
  • API: Google Analytics chooses and reports the variation to serve and evaluates the results; the caller is responsible for serving the selected variation.
  • EXTERNAL: The variations will be served externally and the chosen variation reported to Google Analytics. The caller is responsible for serving the selected variation and evaluating the results.
writable
trafficCoverage double A floating-point number between 0 and 1. Specifies the fraction of the traffic that participates in the experiment. Can be changed for a running experiment. This field may not be changed for an experiment whose status is ENDED. writable
variations[] list Array of variations. The first variation in the array is the original. The number of variations may not change once an experiment is in the RUNNING state. At least two variations are required before status can be set to RUNNING. writable
variations[].status string Status of the variation. Possible values: "ACTIVE", "INACTIVE". INACTIVE variations are not served. This field may not be changed for an experiment whose status is ENDED. writable
variations[].url string The URL of the variation. This field may not be changed for an experiment whose status is RUNNING or ENDED. writable
winnerConfidenceLevel double A floating-point number between 0 and 1. Specifies the necessary confidence level to choose a winner. This field may not be changed for an experiment whose status is ENDED. writable

Response

If successful, this method returns a management.experiment resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

/*
 * 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

Uses the PHP client library.

/**
 * 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

Uses the Python client library.

# 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

Uses the JavaScript client library.

/*
 * 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. });
}