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); 
} }

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

Leave a Comment