Google Drive Activity Service
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
The Google Drive Activity service in Apps Script provides programmatic access to retrieve Google Drive activity information using the Google Drive Activity API.
-
This is an advanced service that needs to be enabled before it can be used.
-
Detailed information and reference documentation for the service are available, mirroring the public API's objects, methods, and parameters.
-
A sample code is provided to demonstrate how to list recent Google Drive activity, including details about the time, actor, action, and target of each activity.
The Google Drive Activity service lets you use the Google Drive Activity API in Google Apps Script. This API lets users retrieve information about their Google Drive activity.
This is an advanced service that must be enabled before use.
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. For more information, see How method signatures are determined.
To report issues and find other support, see the Google Drive Activity support guide.
Sample code
The following sample code 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.
/** * Lists 10 activity for a Drive user. * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query */ functionlistDriveActivity(){ constrequest={ pageSize:10, // Use other parameter here if needed. }; try{ // Activity.query method is used Query past activity in Google Drive. constresponse=DriveActivity.Activity.query(request); constactivities=response.activities; if(!activities||activities.length===0){ console.log("No activity."); return; } console.log("Recent activity:"); for(constactivityofactivities){ // get time information of activity. consttime=getTimeInfo(activity); // get the action details/information constaction=getActionInfo(activity.primaryActionDetail); // get the actor's details of activity constactors=activity.actors.map(getActorInfo); // get target information of activity. consttargets=activity.targets.map(getTargetInfo); // print the time,actor,action and targets of drive activity. console.log("%s: %s, %s, %s",time,actors,action,targets); } }catch(err){ // TODO (developer) - Handle error from drive activity API console.log("Failed with an error %s",err.message); } } /** * @param {object} object * @return {string} Returns the name of a set property in an object, or else "unknown". */ functiongetOneOf(object){ for(constkeyinobject){ returnkey; } return"unknown"; } /** * @param {object} activity Activity object. * @return {string} Returns a time associated with an activity. */ functiongetTimeInfo(activity){ if("timestamp"inactivity){ returnactivity.timestamp; } if("timeRange"inactivity){ returnactivity.timeRange.endTime; } return"unknown"; } /** * @param {object} actionDetail The primary action details of the activity. * @return {string} Returns the type of action. */ functiongetActionInfo(actionDetail){ returngetOneOf(actionDetail); } /** * @param {object} user The User object. * @return {string} Returns user information, or the type of user if not a known user. */ functiongetUserInfo(user){ if("knownUser"inuser){ constknownUser=user.knownUser; constisMe=knownUser.isCurrentUser||false; returnisMe?"people/me":knownUser.personName; } returngetOneOf(user); } /** * @param {object} actor The Actor object. * @return {string} Returns actor information, or the type of actor if not a user. */ functiongetActorInfo(actor){ if("user"inactor){ returngetUserInfo(actor.user); } returngetOneOf(actor); } /** * @param {object} target The Target object. * @return {string} Returns the type of a target and an associated title. */ functiongetTargetInfo(target){ if("driveItem"intarget){ consttitle=target.driveItem.title||"unknown"; return`driveItem:"${title}"`; } if("drive"intarget){ consttitle=target.drive.title||"unknown"; return`drive:"${title}"`; } if("fileComment"intarget){ constparent=target.fileComment.parent||{}; consttitle=parent.title||"unknown"; return`fileComment:"${title}"`; } return`${getOneOf(target)}:unknown`; }