-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdimensions-list.gs
98 lines (84 loc) · 3.65 KB
/
dimensions-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Management Magic for Google Analytics
* Lists custom dimensions from a GA property
*
* Copyright ©2015 Pedro Avila ([email protected])
* Copyright ©2016 Gary Mu (Gary7135[at]gmail[dot]com)
***************************************************************************/
/**************************************************************************
* Obtains input from user necessary for listing custom dimensions.
*/
function requestCDList() {
// Display a dialog box with a title, message, input field, and "OK" and "Cancel" buttons. The
// user can also close the dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Property ID', 'Enter the ID of the property from which to list custom dimensions (UA-xxxx-y): ', ui.ButtonSet.OK_CANCEL);
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
// Construct the array of one or more properties from the user's input.
var propertyList = response.getResponseText();
var propertyListArray = propertyList.split(/\s*,\s*/);
// List custom dimensions from all properties entered by the user.
var listResponse = listCustomDimensions(propertyListArray);
// Output errors and log successes.
if (listResponse != "success") {
Browser.msgBox(listResponse);
} else {
Logger.log("List custom dimensions response: "+ listResponse)
}
}
// Log method by which the user chose not to proceed.
else if (response.getSelectedButton() == ui.Button.CANCEL) {
Logger.log('The user did not provide a property ID.');
} else {
Logger.log('The user clicked the close button in the dialog\'s title bar.');
}
}
/**************************************************************************
* Lists dimension settings from the property into a new sheet
* @param {string} property The tracking ID of the GA property
* @return {string} Operation output ('success' or error message)
*/
function listCustomDimensions(propertyList) {
// Set common values
var include = "✘";
var allDimensions = [];
var dataColumns = 6;
// Iterate through the array of properties from which to list dimensions
for (p = 0; p < propertyList.length; p++) {
var property = propertyList[p];
// Process a property id if it matches a valid format.
if (property.match(/UA-\d+-\d+/)) {
// Extract the account from the property id
var account = property.match(/UA-(\d+)-\d+/)[1];
// Attempt to get property information from the Management API
try {
var customDimensionList = Analytics.Management.CustomDimensions.list(account, property);
} catch (e) {
return e.message;
}
// Attempt to store the information received from the Management API in an array
try {
var dimensions = [];
// Parse each result of the API request and push it to an array
for (var i = 0; i < customDimensionList.totalResults; i++) {
var cd = customDimensionList.items[i];
dimensions[i] = [include,cd.webPropertyId,cd.name,cd.index,cd.scope,cd.active];
allDimensions.push(dimensions[i]);
}
} catch (e) {
return e.message;
}
}
// Return an error message if the property id does not match the correct format.
else return property +" is an invalid property format";
}
// Insert the values processed from the API into a formatted sheet
try {
// Set the values in the sheet
var sheet = formatDimensionSheet(false);
sheet.getRange(2,1,allDimensions.length,dataColumns).setValues(allDimensions);
} catch (e) {
return e.message;
}
return "success";
}