If GoogleJsonResponseException: then skip and move to next row

I have a working script. need to improvise to have no manual interruption. We have multiple Profiles in Analytics, sometimes we lose access and sometimes we have. So when i run the Script, If we lost access to 1 of 60 profiles, i have to delete that entry manually then rerun the script.
What i want is, If there is below error, Then skip and continue with next row

"GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: User does not have sufficient permissions for this profile."

function GoogleAnalytics() {
 
  var doc2 = SpreadsheetApp.getActiveSpreadsheet();
  var dashboard = doc2.getSheetByName("Dashboard");
  for(var i=52;i<65;i++){ 
  var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
 
  
        
  var metric = 'ga:metric, ga:metric2, ga:metric3';
  var option = {'segment': 'gaid::-5'};
  
       
   var result = Analytics.Data.Ga.get(viewId, metric, option);


    var metric = result.totalsForAllResults['ga:metric'];
    var metric2 = result.totalsForAllResults['ga:metric2'];
    var metric3 = result.totalsForAllResults['ga:metric3'];
  
        var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
        var sheet = doc.getActiveSheet(); // Current sheet
  
    sheet.getRange(i,14,1,1).setValue(metric); 
    sheet.getRange(i,15,1,1).setValue(metric2); 
    sheet.getRange(i,16,1,1).setValue(metric3); 
} }

82 thoughts on “If GoogleJsonResponseException: then skip and move to next row”

  1. I might understand the question incorrectly (if so, please clarify) but it sounds to me like you just need to add…

    function GoogleAnalytics() {
     
      var doc2 = SpreadsheetApp.getActiveSpreadsheet();
      var dashboard = doc2.getSheetByName("Dashboard");
      for(var i=52;i<65;i++){ 
      try { //...this line and...
      var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
     
      
            
      var metric = 'ga:metric, ga:metric2, ga:metric3';
      var option = {'segment': 'gaid::-5'};
      
           
       var result = Analytics.Data.Ga.get(viewId, metric, option);
    
    
        var metric = result.totalsForAllResults['ga:metric'];
        var metric2 = result.totalsForAllResults['ga:metric2'];
        var metric3 = result.totalsForAllResults['ga:metric3'];
      
            var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
            var sheet = doc.getActiveSheet(); // Current sheet
      
        sheet.getRange(i,14,1,1).setValue(metric); 
        sheet.getRange(i,15,1,1).setValue(metric2); 
        sheet.getRange(i,16,1,1).setValue(metric3); 
      } catch(e) { //...this part
        console.log(e); //optional, catch(e){} is perfectly valid as well, or any code you might want to execute on error
      }
    } }
    Reply
  2. try it this way:

    function GoogleAnalytics() {
      var doc2 = SpreadsheetApp.getActiveSpreadsheet();
      var sh = doc2.getSheetByName("Dashboard");
      var sheet = doc2.getActiveSheet(); // Current sheet
      const vs = sh.getRange(52, 13, 13).getValues();
      for (var i = 0; i < vs.length; i++) {
        var viewId = vs[i][0]; // Your Google Analytics view ID
        var metric = 'ga:metric, ga:metric2, ga:metric3';//are you sure this isn't suppose to be an object?
        var option = { 'segment': 'gaid::-5' };
        try {
          var result = Analytics.Data.Ga.get(viewId, metric, option);
        }
        catch{
          continue;
        }
        if (result) {
          sheet.getRange(i+52,14,1,2).setValues([[result.totalsForAllResults['ga:metric'],result.totalsForAllResults['ga:metric2'],result.totalsForAllResults['ga:metric3']]]);
        }
      }
    }
    
    Reply

Leave a Comment