Search code examples
javascriptgoogle-apps-scriptgoogle-analyticsgoogle-analytics-api

Analytics API (Apps Script) Documentation Missing ? | "getItems" vs "items" and other issues


Hi All (and thanks in advance for helping)! I am seriously wondering about the Analytics API (Management API): https://developers.google.com/analytics/devguides/config/mgmt/v3/

Google Sheets / Apps Script / Analytics

I am working from Google Sheets so I am using Apps Script to pull data. The documentation for Apps Script itself is self explanatory enough but there seems to be nothing for the Analytics Management API in conjunction with Apps Script. So far I've been looking mostly at the Javascript samples to get something close.

Hidden Functions?

However there seem to be a number of functions available that I've found in various examples but are NOWHERE in the documentation. For example:

var properties = Analytics.Management.Webproperties.list(AcountID);
var webPropertyIdOfFirstElement = properties.getItems()[0].getId();

Will return the first element's ID.

Documentation

However as per this documentation (Javascript)

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/webproperties/list

The actual code (returning the same) should/would be:

var results = Analytics.Management.Webproperties.list(accountID);
var properties = results.items;
var property = properties[0];
var theID = property.id;

Where?

So my question is where are the functions

getItems()[X] and .getId()

coming from as those are not javascript build in functions? Is there some sort of secret documentation I keep missing? I'd honestly love to know.

And as well (on the same page as above in the example). Can someone please explain the "execute" function. I've honestly never seen this before. How does that work?

function listProperties() {
  var request = gapi.client.analytics.management.webproperties.list({
    'accountId': '123456'
  });
  request.execute(printProperties);
}

function printProperties(results) { //do stuff }

? "execute" and pass the function as a parameter? huh?


Solution

  • How about this answer?

    Answer for Question 1:

    • You want to know about the difference res.items[0].id and res.getItems()[0].getId() for var res = Analytics.Management.Webproperties.list(AcountID).

    value1 and value2 of the following script are the same. But items and getItems are the key and the function, respectively.

    var res = Analytics.Management.Webproperties.list(AcountID);
    var value1 = res.items[0].id;
    var value2 = res.getItems()[0].getId();
    

    It seems that each value can be retrieved by adding get to the top and running it as a function. The function name is required to be the camel case.

    But I couldn't find such functions at the official document and the completion function of the script editor. So I had thought that such functions might be the hidden functions.

    In Advanced Google Services, we can confirm the functions for creating the request body for API by the completion function of the script editor. Ref But although the functions for retrieving values cannot be found, those can be used as the hidden methods. Such functions can be also confirmed that there are such hidden methods for other API.

    Other case:

    For example, as other case, at Sheets API, you can see the hidden function at the following script.

    var r = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: ["Sheet1", "Sheet2"]});
    
    var value1a = r.spreadsheetId;
    var value1b = r.getSpreadsheetId();
    
    var value2a = r.valueRanges;
    var value2b = r.getValueRanges();
    

    In this case, value1a and value1b are the same. And also value2a and value2b are the same. In order to confirm whether getSpreadsheetId() and getValueRanges() can be used, I use the following script.

    var r = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: ["Sheet1", "Sheet2"]});
    Logger.log(typeof r.getSpreadsheetId)
    Logger.log(typeof r.getValueRanges)
    

    If it can be used as a function, the log shows function. If it cannot be used as a function, undefined is returned.

    Answer for Question 2:

    • You want to know about execute() for google-api-javascript-client.

    I think that the answer can be seen at Using Promises.

    Migrating from callbacks to promises

    The result parameter of the fulfilled promise value is equivalent to the first parameter in execute's callback. To update your code to use promises, change your code as shown in the before and after examples below.

    The following example shows using a callback:

    gapi.client.request({
      'path': 'plus/v1/people',
      'params': {'query': name}
     }).execute(function(resp, rawResp) {
       processResponse(resp);
     });
    

    You can rewrite the example shown above to use a promise like the following:

    gapi.client.request({
      'path': 'plus/v1/people',
      'params': {'query': name}
     }).then(function(resp) {
       processResponse(resp.result);
     });
    

    References:

    If I misunderstood your question and this was not the answer you want, I apologize.