Google Drive Activity Service

The Google Drive Activity service allows you to use the Google Drive Activity API in Apps Script. This API allows users programmatic access to retrieve information about their Google Drive activity.

Reference

For detailed information on this service, see the reference documentation for the Google Drive Activity API. Like all advanced services in Apps Script, the Google Drive Activity service uses the same objects, methods, and parameters as the public API.

To report issues and find other support, see the Google Drive Activity support guide.

Sample code

The sample code below uses version 2 of the API.

Get activity in Google Drive

This sample gets recent activity in a user's Google Drive and logs information about the time, actor, action, and target of each activity.

drive/activity-v2/quickstart.gs
/**
 * Lists activity for a Drive user.
 */
function listDriveActivity() {
  var request = {pageSize: 10};
  var response = DriveActivity.Activity.query(request);
  var activities = response.activities;
  if (activities && activities.length > 0) {
    Logger.log('Recent activity:');
    for (var i = 0; i < activities.length; i++) {
      var activity = activities[i];
      var time = getTimeInfo(activity);
      var action = getActionInfo(activity.primaryActionDetail);
      var actors = activity.actors.map(getActorInfo);
      var targets = activity.targets.map(getTargetInfo);
      Logger.log(
          '%s: %s, %s, %s', time, truncated(actors), action,
          truncated(targets));
    }
  } else {
    Logger.log('No activity.');
  }
}

/** Returns a string representation of the first elements in a list. */
function truncated(array, opt_limit) {
  var limit = opt_limit || 2;
  var contents = array.slice(0, limit).join(', ');
  var more = array.length > limit ? ', ...' : '';
  return '[' + contents + more + ']';
}

/** Returns the name of a set property in an object, or else "unknown". */
function getOneOf(object) {
  for (var key in object) {
    return key;
  }
  return 'unknown';
}

/** Returns a time associated with an activity. */
function getTimeInfo(activity) {
  if ('timestamp' in activity) {
    return activity.timestamp;
  }
  if ('timeRange' in activity) {
    return activity.timeRange.endTime;
  }
  return 'unknown';
}

/** Returns the type of action. */
function getActionInfo(actionDetail) {
  return getOneOf(actionDetail);
}

/** Returns user information, or the type of user if not a known user. */
function getUserInfo(user) {
  if ('knownUser' in user) {
    var knownUser = user.knownUser;
    var isMe = knownUser.isCurrentUser || false;
    return isMe ? 'people/me' : knownUser.personName;
  }
  return getOneOf(user);
}

/** Returns actor information, or the type of actor if not a user. */
function getActorInfo(actor) {
  if ('user' in actor) {
    return getUserInfo(actor.user)
  }
  return getOneOf(actor);
}

/** Returns the type of a target and an associated title. */
function getTargetInfo(target) {
  if ('driveItem' in target) {
    var title = target.driveItem.title || 'unknown';
    return 'driveItem:"' + title + '"';
  }
  if ('drive' in target) {
    var title = target.drive.title || 'unknown';
    return 'drive:"' + title + '"';
  }
  if ('fileComment' in target) {
    var parent = target.fileComment.parent || {};
    var title = parent.title || 'unknown';
    return 'fileComment:"' + title + '"';
  }
  return getOneOf(target) + ':unknown';
}