2

I have a requirement to get the most recent activity for each user in the domain within Google Currents (formerly, Google plus)

I am using AdminReports.Activities.list for this, using the 'all' parameter rather than an email address.

I have tested with the following script:

function testFunc() {
  let target;

  if (testState == true) {
    target = "single.user@email.com";
  } else if (testState == false) {
    target = "all";
  } else {
    throw "State not defined";
  }

  const app = "gplus";

  const daysToCheck = 1;
  const now = new Date();
  const daysAgo = new Date(now.getTime() - daysToCheck * 24 * 60 * 60 * 1000);
  const date = daysAgo.toISOString();

  const optionalArgs = {
    startTime: date
  };

  let rows = [];

  let response;
  let pageToken;

  do {
    response = AdminReports.Activities.list(target, app, optionalArgs);
    let activities = response.items;

    if (activities) {
      activities.forEach(item => {
        let row = [
          new Date(item.id.time),
          item.actor.email,
          item.events[0].name
        ]
        rows.push(row)
      
      })
    } else {
      throw 'Error'
    }

    pageToken = response.nextPageToken;
  } while (pageToken);

  Logger.log(rows);
  /** NOTES
   * will eventually log this out to a spreadsheet
   */
}

However, this will return multiple events for a single person. I need to only return the most recent event for each user in the domain.

Does anyone have any ideas on how to achieve this?

SL8t7
  • 617
  • 2
  • 9
  • 27

1 Answers1

0

Solution:

You can sort the rows array in order by name, then descending time, then loop to return the first element of each name.

Sample Code: after Logger.log(rows)

  rows.sort(function(a,b) {
    if (a[2] == b[2])
      return a[0] < b[0] ? 1 : -1;
    return a[2] < b[2] ? -1 : 1;
  });

  let final = [];
  final.push(rows[0]);
  for (i = 1; i < rows.length; i++) {
    if (rows[i][2] != arr[i-1][2])
      final.push(rows[i]);
  }

  console.log(final);
CMB
  • 4,950
  • 1
  • 4
  • 16