I am using this code to retrieve the User Usage Report. I am able to get the 7 parameters out of 12.
Questions:
- Is it okay to combine accounts and gmail parameters? If yes, how would I fix the code to display the same format as the "Last Login"?
All shows "undefined": 'gmail:timestamp_last_access', 'gmail:timestamp_last_imap', 'gmail:timestamp_last_pop', 'gmail:timestamp_last_webmail'
- What is the parameter reference for Account Status = Active, Suspended, Deleted...etc.? I have tried searching but no luck.
I am using this code below. Thanks in advance for your time and help!
function generateUserReport() {
var today = new Date();
var twoDaysAgo = new Date(today.getTime() - 2 * 24 * 60 * 60 * 1000);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(twoDaysAgo, timezone, "yyyy-MM-dd");
var parameters = [
'accounts:first_name',
'accounts:last_name',
'accounts:is_super_admin',
'accounts:last_login_time',
'accounts:gmail_used_quota_in_mb',
'accounts:drive_used_quota_in_mb',
'gmail:timestamp_last_access',
'gmail:timestamp_last_imap',
'gmail:timestamp_last_pop',
'gmail:timestamp_last_webmail'
];
var rows = [];
var pageToken;
var page;
do {
page = AdminReports.UserUsageReport.get('all',date, {
parameters: parameters.join(','),
maxResults: 500,
pageToken: pageToken
});
if (page.warnings) {
for (var i = 0; i < page.warnings.length; i++) {
var warning = page.warnings[i];
Logger.log(warning.message);
}
}
var reports = page.usageReports;
if (reports) {
for (var i = 0; i < reports.length; i++) {
var report = reports[i];
var parameterValues = getParameterValues(report.parameters);
var row = [
report.date,
report.entity.userEmail,
parameterValues['accounts:first_name'],
parameterValues['accounts:last_name'],
parameterValues['accounts:is_super_admin'],
parameterValues['accounts:last_login_time'],
parameterValues['accounts:gmail_used_quota_in_mb'],
parameterValues['accounts:drive_used_quota_in_mb'],
parameterValues['gmail:timestamp_last_access'],
parameterValues['gmail:timestamp_last_imap'],
parameterValues['gmail:timestamp_last_pop'],
parameterValues['gmail:timestamp_last_webmail']
];
rows.push(row);
}
}
pageToken = page.nextPageToken;
} while (pageToken);
if (rows.length > 0) {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('UserSheet');
sheet.clear();
// Append the headers.
var headers = ['Date','User','First Name', 'Last Name','Admin Account Status', 'Last Login', 'Email Usage', 'Drive Usage','Last Access Date',
'IMAP','POP','WEBMAIL'];
sheet.appendRow(headers);
// Append the results.
sheet.getRange(2, 1, rows.length, headers.length).setValues(rows);
Logger.log('Report spreadsheet created: %s', spreadsheet.getUrl());
} else {
Logger.log('No results returned.');
}
}
/**
* Gets a map of parameter names to values from an array of parameter objects.
* @param {Array} parameters An array of parameter objects.
* @return {Object} A map from parameter names to their values.
*/
function getParameterValues(parameters) {
return parameters.reduce(function(result, parameter) {
var name = parameter.name;
var value;
if (parameter.intValue !== undefined) {
value = parameter.intValue;
} else if (parameter.stringValue !== undefined) {
value = parameter.stringValue;
} else if (parameter.datetimeValue !== undefined) {
value = new Date(parameter.datetimeValue);
} else if (parameter.boolValue !== undefined) {
value = parameter.boolValue;
}
result[name] = value;
return result;
}, {});
}