google-apps-scriptgoogle-sheetsgoogle-cloud-platformgoogle-apigoogle-meet

"Error: API call to reports.activities.list failed with error: Bad Request" when getting Google Meet data (call duration) using Google script


I am trying to get Google Meet data:

  1. Total call duration of user in the specified month
  2. Number of calls made by user in the specified month

using the following code snippet:

function getMeetLogs() {

  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1; // Months are zero-based, so add 1
  var userEmail = "123@gmail.com"; // Replace with the desired user's email address
  
  var options = {
    applicationName: "meet",
    eventName: "call_ended",
    filters: {
    date: currentYear + "-" + currentMonth.toString().padStart(2, "0"), // Format: YYYY-MM
    event_type: "call_ended",
    meeting_type: "video_call",
    initiator_email: userEmail
    },
    maxResults: 1000
    };


    var meetLogs = AdminReports.Activities.list("all", "meet", options);
    Logger.log(meetLogs);
    
    var callCount = meetLogs.items ? meetLogs.items.length : 0;
    var totalDuration = 0;

    if (callCount > 0) {
     for (var i = 0; i < meetLogs.items.length; i++) {
       var callDuration = meetLogs.items[i].parameters.duration;
       totalDuration += callDuration ? callDuration : 0;
    }
  }
}

When I try to run it, I receive the following error:

GoogleJsonResponseException: API call to reports.activities.list failed with error: Bad Request at newMeetLogsToSheet(Code:39:40)

I thought this was a parameter issue, so I went through this Reference to match the parameters, however, I am not able to get around it. The following sources can be helpful to find the solution:

  1. Google Meet Parameters
  2. User Usage Report

Can you please assist me how to resolve this issue? Thanks


Solution

  • This is just to give you an idea on how to navigate through the parameters you get in the response when you use the activities.list method. For more clarity, I added some comments to the script.

    This script aims to get the number of calls a user made in a particular month and the number of minutes spent on calls during that month.

    function getMeetLogs() {
      const userKey = 'test@domain.xyz';// Replace with the desired user's email address
      const applicationName = 'meet';
      var duration = [];
      var meets = [];
    
      var options = {eventName: "call_ended"};
      var meetLogs = AdminReports.Activities.list(userKey, applicationName, options);
    
      for (var i = 0; i < meetLogs.items.length; i++) {
        var date = new Date(meetLogs.items[i].id.time);
        var month = date.getMonth() + 1;
        var param = meetLogs.items[i].events[0].parameters;
        //console.log(param)//here you can see all the event details
    
        if (month == 5) {//change 5 to the month number you're looking for
    
          //For each event, iterate and to determine the call duration if the event name is duration_seconds
          for (var j = 0; j < param.length; j++) {
            if (param[j].name == 'meeting_code') {
              meets.push(param[j].value)          
            }
    
            if (param[j].name == 'duration_seconds') {
              duration.push(parseInt(param[j].intValue))        
            }
    
          }
        }
      }
    
      //Counting unique meetings to avoid counting duplicate meet codes (recurring meetings have the same meeting code)
      let uniqueMeets = [...new Set(meets)].length
      let sum = 0;
      duration.forEach(num => {
        sum += num;
      })
    
      console.log("Total calls for user " + userKey + ": " + uniqueMeets)
      console.log("Total minutes in call for user " + userKey + ": " + sum/60)
    }
    

    References: