Inserting Google Analytics Content Experiments usi

2019-05-06 15:20发布

问题:

I'm trying to configure a content experiment using the Node.js Client Library, and have not been able to work out the syntax. Where do I put the body (an Experiment resource) as described here?

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#insert

This code, for listing existing experiments, works as expected:

var listExperiments = function(){
  googleapis
  .discover('analytics', 'v3')
  .execute(function(err, client) {
    var request = client
    .analytics.management.experiments.list({
        accountId : accountId,
        webPropertyId : webPropertyId,
        profileId : profileId
        })
    .withApiKey(browserAPIKey)
    .withAuthClient(oauth2Client)

      request.execute(function(err,result){
        if (err){
          console.log(err);
          res.send(402);          
        } else {
          console.log(result);
          res.send(200);
        }
      });
  });
}

However, when I try to insert a new experiment thusly, I receive a "Field resource is required" error.

var body = {       
  "name": "myExperimentName",
  "status": "READY_TO_RUN",
  "objectiveMetric":"ga:bounces",
  "variations": [
    { "name": "text1", "url":"http://www.asite.net", "status":"ACTIVE" },
    { "name": "text2", "url":"http://www.asite.net", "status":"ACTIVE" }
   ]
 };

var insertExperiment = function(){
  googleapis
  .discover('analytics', 'v3')
  .execute(function(err, client) {
    var request = client
    .analytics.management.experiments.insert({
        accountId : accountId,
        webPropertyId : webPropertyId,
        profileId : profileId,
        resource : body
        })
    .withApiKey(browserAPIKey)
    .withAuthClient(oauth2Client)

    request.execute(function(err,result){
      if (err){
        console.log(err);
        res.send(402);          
      } else {
        console.log(result);
        res.send(200);
      }
    });
  });
}

I've tried a few configurations. Management API writes are in limited beta, but I have beta access, so that's not the problem. I've tried inserting the new experiment information directly into the insert() object, calling the experiment info object "body : body " instead of "resource : body", JSON.stringifying the body, and a few other configurations. No luck.

Any help would be great!

I'm aware of this answer, but it uses the Javascript Client Library and makes RESTful requests, whereas I'd like to use the Node Library.

EDIT: Thanks to Burcu Dogan at Google. Here's the correct syntax:

.analytics.management.experiments.insert({
accountId : accountId,
webPropertyId : webPropertyId,
profileId : profileId
}, body)