Google Reporting API:客户使用情况报告

时间:2015-04-27 18:17:30

标签: google-apps-script google-reporting-api

我正在尝试从我的域中为所有用户生成使用情况报告。我希望能够使用这些参数:num_1day_logins,num_7day_logins,num_30day_logins。这样我就可以展示我们域中Google Apps的采用率是增加还是减少。以下代码来自Google。这与我想做的很接近,但并不完全正确。我认为可以将此代码自定义为我想要做的,但我不知道如何。

感谢。

function generateUserUsageReport() {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var timezone = Session.getTimeZone();
var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
var parameters = [
'accounts:last_login_time',
'gmail:num_emails_received',
'docs:num_docs'
];
var rows = [];
var pageToken, page;
do {
page = AdminReports.UserUsageReport.get('all', date, {
  parameters: parameters.join(','),
  maxResults: 500,
  pageToken: pageToken
});
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:last_login_time'],
      parameterValues['gmail:num_emails_received'],
      parameterValues['docs:num_docs']
    ];
    rows.push(row);
  }
}
pageToken = page.nextPageToken;
} while (pageToken);

if (rows.length > 0) {
var spreadsheet = SpreadsheetApp.create('Google Apps User Usage Report');
var sheet = spreadsheet.getActiveSheet();

// Append the headers.
var headers = ['Date', 'User', 'Last Login', 'Num Emails Received',
    'Num Docs'];
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;
}, {});
}

1 个答案:

答案 0 :(得分:1)

以下是包含您要查找的参数的代码。

function generateUserUsageReport() {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
var parameters = [
'accounts:num_1day_logins',
'accounts:num_7day_logins',
'accounts:num_30day_logins'
];
var rows = [];
var pageToken, page;
do {
page = AdminReports.CustomerUsageReports.get(date, {
  parameters: parameters.join(','),
  maxResults: 500,
  pageToken: pageToken
});
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,
      parameterValues['accounts:num_1day_logins'],
      parameterValues['accounts:num_7day_logins'],
      parameterValues['accounts:num_30day_logins']
    ];
    rows.push(row);
  }
}
pageToken = page.nextPageToken;
} while (pageToken);

if (rows.length > 0) {
var spreadsheet = SpreadsheetApp.create('Google Apps User Usage Report');
var sheet = spreadsheet.getActiveSheet();

// Append the headers.
var headers = ['Date', '1day_logins', '7day_logins',
    '30day_logins'];
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;
}, {});
}