-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccountSummary-list.gs
81 lines (71 loc) · 2.55 KB
/
accountSummary-list.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Management Magic for Google Analytics
* List filters from a GA property
*
* Copyright ©2016 Gary Mu (Gary7135[at]gmail[dot]com)
***************************************************************************/
/**************************************************************************
* Obtains input from user necessary for listing filters
*/
function requestAccountSummary() {
// List filters from all accounts entered by the user.
var listAccountSummary = accountSummary();
// Output errors and log successes.
if (listAccountSummary != "success") {
Browser.msgBox(listAccountSummary);
} else {
Logger.log("List filters response: "+ listAccountSummary);
}
}
/**************************************************************************
* Lists filter settings from an account into a new sheet
* @param {string} account The account ID from which to list filters
* @return {string} Operation output ('success' or error message)
*/
function accountSummary() {
// set common values
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allAccounts = [];
var dataColumns = 8;
// Attempt to get filters from the Management API.
try {
var accountSummary = Analytics.Management.AccountSummaries.list();
} catch (e) {
return "Error getting data from Mgmt API\n"+ e.message;
}
// Attempt to store the information received from the Management API in an array
try {
var account = [];
// Parse each result of the API request and push it to an array
for (var i = 0; i < accountSummary.totalResults; i++) {
var a = accountSummary.items[i];
var account_id = a.id;
var account_name = a.name;
//Get property data
for (var j = 0; j < a.webProperties.length; j++){
var p = a.webProperties[j];
var property_id = p.id;
var property_name = p.name;
var level = p.level;
//Get view data
for (var k = 0; k < p.profiles.length; k++){
var v = p.profiles[k];
var view_id = v.id;
var view_name = v.name;
var type = v.type;
account[i] = [account_id, account_name, property_id, property_name, level, view_id, view_name, type];
allAccounts.push(account[i]);
}
}
}
}catch(e){
return e.message;}
// Insert the values processed from the API into a formatted sheet
try {
// Set the values in the sheet
var sheet = formatAccountSummarySheet(false);
sheet.getRange(2,1,allAccounts.length,dataColumns).setValues(allAccounts);
} catch (e) {
return e.message;
}
return "success";
}