Jessica Feliciano
Jessica Feliciano

Reputation: 43

GoogleJsonResponseException: Field name is required

I'm working with the Google Analytics API for the first time and I'm trying to create a new property. I wrote a JS function in Google App Script:

function insertProperty() {
  var resource = 
      {
        //  "accountId": "177832616",
        "resource":{
          "name": "Test Property 7",
          //  "dataRetentionResetOnNewActivity": false,
          "websiteUrl": "https://www.test.com"
        }
      }
  var accountID = '177832616';
  var request = Analytics.Management.Webproperties.insert(resource, accountID);
    
 // request.execute(function (response) { console.log(property.id) });
}

This is the error the API throws:

GoogleJsonResponseException: API call to analytics.management.webproperties.insert failed with error: Field name is required. (line 56, file "Code")

The insert() method seems to take two parameters: insert(Webproperty resource, string accountId);

Since it's not recognizing the name key/value I added to resource, my guess is I haven't declared the variable as a Webproperty type and I'm not sure how to do this. I assumed Webproperty was a { } variable type, but at this point I'm not sure what to try next. Doing research online, I'm not able to find anything regarding the API's Webproperty so any context/info is helpful.

Upvotes: 2

Views: 225

Answers (1)

Tanaike
Tanaike

Reputation: 201378

From your question, I could understand that Google Analytics API is used with Advanced Google services of Google Apps Script. In this case, resource of Analytics.Management.Webproperties.insert(resource, accountId) can directly use the request body of the method of "Web Properties: insert". I think that the reason of your error is due to this. So please modify as follows and test it again.

From:

var resource = 
    {
      //  "accountId": "177832616",
      "resource":{
        "name": "Test Property 7",
        //  "dataRetentionResetOnNewActivity": false,
        "websiteUrl": "https://www.test.com"
      }
    }

To:

var resource = {
  "name": "Test Property 7",
  "websiteUrl": "https://www.test.com"
}

Note:

  • When accountId is not correct, an error occurs. Please be careful this.
  • From iansedano's comment, in this case, request of var request = Analytics.Management.Webproperties.insert(resource, accountID); directly returns the values. So you can see the value like console.log(request) and console.log(request.toString()).

Reference:

Upvotes: 1

Related Questions