Is there a Apps Script function to export the viewer information present in the Activity Dashboard into a separate GSheet?

Viewed 565

Actually I was trying to set a repository kind of a thing with a count of number of views on my document everyday. This information is present in Activity Dashboard but I was not able to figure out a way to extract the information present in Activity Dashboard into a separate GSheet.

1 Answers

The closest to the Activity Dashboard is the Google Drive Activity API

  • You can use the method Method: activity.query
  • To see the ativity for a particular file, specify the parameter itemName setting it to items/ID_OF_YOUR_FILE
  • to get familiar with the structure of the response body - test with the Try this API feature
  • Later on, to incorporate this feature in Apps Script enable in your Apps Script editor the Advanced Service Drive Activity API

Small sample:

function listDriveActivity() {
  var fileId = "XXX";
  var request = {itemName: "items/" + fileId};
  var response = DriveActivity.Activity.query(request);
  Logger.log("The first activity: " + response.activities[0]);
}

The documentation contains an extended sample for showing what is possible with the Drive Activity API.

UPDATE

The method above returns among others editing activities, but not viewing activities.

To track viewing activities, as a domain admin you can use the Reports API.

  • To use it in Apps Script, enable it under Resources -> Advanced Google Services.
  • Use the method AdminReports.Activities.list().
  • Specify in the query the application you are interested in (in your case drive) and additional query parameters like the activity (view) or the id of the document you are interested in.
  • Loop through the results and retrieve the fields of interest (e.g. actor.email).

Sample:

function myFunction() {
  var views = AdminReports.Activities.list("all", "drive", {eventName: "view", filters: "doc_id==XXXXX"}).items;
  for ( var i = 0; i< views.length; i++){
    Logger.log(views[i].actor.email);
  }
}
Related