Insert experiment into Google analytics api (gapi)

I am trying to insert an experiment into Google analytics through their api (using gapi)

var experiment = {
  "name": "Testing",
  "description": "test",
  "status": "READY_TO_RUN",
     "variations": [{
        "name": "Original",
        "url": "http://abc.se",
      }, {
        "name": "Variant 1",
        "url": "http://abc.se/1",
      }],
};

gapi.client.analytics.management.experiments.insert({
  'accountId': currentProfile['accountId'],
  'profileId': currentProfile['profileId'],
  'webPropertyId': currentProfile['webPropertyId'],
  'resource': experiment
}).execute(callback);

The response code that I get is “code”: 500, “message”: “Internal error.”,

Any help would be greatly appreciated. (First stackoverflow question, I apologize in advance if my question is fuzzy.)

0
source share
2 answers

The solution was to add the "objectMetric" parameter. Although this is not indicated in the documentation.

final code:

var requestBody = {
  "name": "testing2", //required
  "status": "READY_TO_RUN", //required
  "objectiveMetric": "ga:goal11Completions", //required?
  "variations": [
  {
   "name": "test1", //required
   "status": "ACTIVE",
   "url": "http://abs.se/3" //required
  },
  {
   "name": "test2", //required
   "status": "ACTIVE",
   "url": "http://abs.se/4" //required
  }
 ]
};


var request = gapi.client.request({
  'path': '/analytics/v3/management/accounts/{accountId}/webproperties/{webPropertyId}/profiles/{profileId}/experiments',
  'method': 'POST',
  'body': JSON.stringify(requestBody)});
  request.execute(handleAccounts);
}

Many thanks to Pete for the help.

0
source

First, check out the API link for the INSERT experiment method .

, POST, Experiment. / - , . objectMetric, , "RUNNING", serveFramework "REDIRECT" "API".

JavaScript, gapi.client.request . , , GET, . POST . gapi.client.request, REST.

, :

var requestBody = {
  'name': 'Testing',
  'status': 'RUNNING',
  'objectiveMetric': 'ga:timeOnSite',
  'variations': [
    {'name': 'VER 1', 'status': 'ACTIVE', 'url': 'http://abs.com/1'},
    {'name': 'VER 2', 'status': 'ACTIVE', 'url': 'http://abs.com/2'}
  ],
};

var request = gapi.client.request({
  'path': '/analytics/v3/management/accounts/YOUR_ACCOUNT_ID/webproperties/YOUR_WEBPROPERTY_ID/profiles/YOUR_PROFILE_ID/experiments',
  'method': 'POST',
  'body': JSON.stringify(requestBody)});
request.execute(handleAccounts);
0

All Articles