2015-04-27 92 views
0

我正嘗試從我的域中爲我的所有用戶生成使用情況報告。我希望能夠使用這些參數:num_1day_logins,num_7day_logins,num_30day_logins。以便我可以展示Google域在我們的網域中的採用率是增加還是減少。下面的代碼來自Google。這與我想要做的接近,但不完全一樣。我認爲可以將此代碼自定義爲我想要執行的操作,但我不知道如何執行。Google Reporting API:客戶使用報告

謝謝。

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; 
}, {}); 
} 

回答

0

這裏是您正在查找的參數的代碼。

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; 
}, {}); 
} 
+0

這很好用!謝謝。我如何去改變從1WeekAgo到1個月前或3個月前的開始日期? –

+1

var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000); 值'7'指定了幾天前的樣子。更改7到30或90以及時返回。 –

+0

不要忘記接受答案。 –

相關問題