Home > Blockchain >  How to request specific data from the Google Search Console API using Google Apps Script?
How to request specific data from the Google Search Console API using Google Apps Script?

Time:11-18

I'm trying to recover Google Discover data from the Google Search Console through its API and display it in a Google Sheet.

I'm following a tutorial that walked me through setting up the needed authentications in the Google Cloud platform and showed me how to set up a basic request that retrieves a list of websites I have access to in the Search Console.

What I don't understand is how to modify the code below so that the API retrieves Google Discover data instead of a list of websites I have access to.

The Google Apps Script code gets the mentioned websites and populates two columns, one with the website link and another with the access level I have for that website.

 function listAccountSites() {
  var service = getService();
  if (service.hasAccess()) {
  
    var apiURL = "https://www.googleapis.com/webmasters/v3/sites";
    
    var headers = {
      "Authorization": "Bearer "   getService().getAccessToken()
    };
    
    var options = {
      "headers": headers,
      "method" : "GET",
      "muteHttpExceptions": true
    };
    
    var response = UrlFetchApp.fetch(apiURL, options);
    
    var json = JSON.parse(response.getContentText());
    Logger.log(json)
    
    var URLs = []
    for (var i in json.siteEntry) {
      URLs.push([json.siteEntry[i].siteUrl, json.siteEntry[i].permissionLevel]); 
    }
    s_sites.getRange(2,1,URLs.length,2).setValues(URLs);
    
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
    Browser.msgBox('Open the following URL and re-run the script: '   authorizationUrl)
  }
}

Using this Google documentation page I was able to construct an API request that would get the Google Discover data I need, but as I said I cannot figure out how to do this. The mentioned request gets the data I need is the following one:

{
  "startDate": "2021-11-01",
  "endDate": "2021-11-05",
  "dimensions": [
    "PAGE"
  ],
  "type": "DISCOVER"
}

CodePudding user response:

I believe your goal is as follows.

  • You want to achieve the following request using Google Apps Script. Ref

      POST https://www.googleapis.com/webmasters/v3/sites/https://www.example.com//searchAnalytics/query?key={MY_API_KEY}
      {
        "startDate": "2015-04-01",
        "endDate": "2015-05-01",
        "dimensions": ["country","device"]
      }
    
  • In your situation, you want to use the following request body.

      {
        "startDate": "2021-11-01",
        "endDate": "2021-11-05",
        "dimensions": [
          "PAGE"
        ],
        "type": "DISCOVER"
      }
    

If my understanding is correct, how about the following sample script?

Sample script:

This sample script supposes that your access token can be used and you have already been able to use the API. Please be careful this.

var siteUrl = "https://www.example.com/"; // Please set the site URL.

var apiURL = `https://www.googleapis.com/webmasters/v3/sites/${encodeURIComponent(siteUrl)}/searchAnalytics/query`;
var headers = { "Authorization": "Bearer "   getService().getAccessToken() };
var payload = {
  "startDate": "2021-11-01",
  "endDate": "2021-11-05",
  "dimensions": ["PAGE"],
  "type": "DISCOVER"
};
var options = {
  "headers": headers,
  "method": "POST",
  "muteHttpExceptions": true,
  "contentType": "application/json",
  "payload": JSON.stringify(payload),
};
var response = UrlFetchApp.fetch(apiURL, options);

References:

  • Related