forked from albertkawmi/manyanalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
many-analytics.js
212 lines (160 loc) · 6.01 KB
/
many-analytics.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Global variables
// These are used instead of promises as app is very simple.
// If complexetiy increases will be better to switch to a promises / async library to manage state.
var csvRows = [];
var firstRow = false;
var globalQuery = {};
var queueIndex = 0;
var queueLength = null;
// Execute this function when the 'Make API Call' button is clicked
function makeApiCall() {
//reset all globals for when we run function again
csvRows = [];
firstRow = false;
globalQuery = {};
queueIndex = 0;
queueLength = null;
// hide download link and show loading animation
document.getElementById('csv-link').style.display = 'none';
document.getElementById('loading').style.display = 'block';
// show loading text
//document.getElementById('loading').style.display = 'block';
//loader = setInterval(loadingDots, 500);
// save form data into global query object
globalQuery = queryParams();
//get list of profiles
console.log('Starting Request Process...');
getProfilesList();
}
function getProfilesList() {
console.log('Querying Views (Profiles).');
// Get a list of all Views (Profiles) for the first Web Property of the first Account
gapi.client.analytics.management.profiles.list({
'accountId': '~all',
'webPropertyId': '~all'
}).execute(handleProfiles);
}
function handleProfiles(results) {
if (!results.code) {
if (results && results.items && results.items.length) {
queueLength = 0;
// Use this in for loop to point to profile names
// regex is used to filter out profiles
var itemName = "";
var regex = new RegExp(
(document.regexFilter['regex'].value == "") ? ".*" : document.regexFilter['regex'].value);
// Query the Core Reporting API
for(var i=0; i<results.items.length; i++) {
itemName = results.items[i].name;
if(itemName.match(regex) == itemName){
queueLength ++;
setTimeout(queryCoreReportingApi, 100*i, results.items[i].id);
}
}
} else {
console.log('No views (profiles) found for this user.');
}
} else {
console.log('There was an error querying views (profiles): ' + results.message);
}
}
function queryCoreReportingApi(profileId) {
console.log('Querying Core Reporting API for profile ID: ' + profileId);
var queryObject = globalQuery;
queryObject.ids = 'ga:' + profileId;
// Use the Analytics Service Object to query the Core Reporting API
gapi.client.analytics.data.ga.get(queryObject).execute(handleCoreReportingResults);
}
function handleCoreReportingResults(results) {
if (results.error) {
console.log('There was an error querying core reporting API: ' + results.message);
} else {
printResults(results);
}
}
function printResults(results) {
console.log(
results.rows && results.rows.length ? 'Completed profile ' : 'No results found for ',
results.profileInfo.profileName,
' at ', Date()
);
// Store rows
storeRows(results);
}
function storeRows(results) {
// Store first row if not already set
if(!firstRow) {
var headers = [];
for(var col in results.columnHeaders) { headers.push(results.columnHeaders[col].name); }
headers = ['Profile ID','Account ID','Profile Name'].concat(headers);
csvRows.push(headers.join(','));
firstRow = true; // first row has been set
}
// Set metaData as array of Profile ID, Account ID and Profile Name
var metaData = [results.profileInfo.profileId, results.profileInfo.accountId, results.profileInfo.profileName];
var rowArray = [];
if(results.rows && results.rows.length){
for(var rowNum in results.rows) {
rowArray = metaData.concat(results.rows[rowNum]);
csvRows.push(rowArray.join(','));
}
} else {
for(var c = 0; c<results.columnHeaders.length; c++) { rowArray.push(0);}
rowArray = metaData.concat(rowArray);
csvRows.push(rowArray.join(','));
}
queueIndex++;
if(queueIndex == queueLength) {downloadCSV();}
}
function downloadCSV() {
//join the CSV rows separated by a newline character
var csvString = csvRows.join("\n");
// create a blob for the CSV file and set the link href to point to that blob
var blob = new Blob([csvString], {type: "text/css;charset=utf-8"});
var a = document.getElementById('csv-link');
a.href = window.URL.createObjectURL(blob);
// Set the filename
a.download = "GA_" + document.queryName["query-name"].value + "_at_" + Date() + ".csv";
// Hide the 'loading...' animation and show the download link
document.getElementById('loading').style.display = 'none';
//document.getElementById('loading').innerHTML = '';
//clearInterval(loader);
a.style.display = 'block';
}
// unused (for console debugging)
function logResults(results) {
console.log("//////////////////////// RESULTS /////////////////////////////");
var logRow = "Profile ID, Account ID, Profile Name, ";
for(var col in results.columnHeaders) { logRow += results.columnHeaders[col].name + ", "; }
console.log(logRow);
logRow = "";
for(var row in results.rows) {
logRow = results.profileInfo.profileId + ', ' + results.profileInfo.accountId + ', ' + results.profileInfo.profileName + ', ';
for(var val in results.rows[row]) {
logRow += results.rows[row][val] + ', ';
}
console.log(logRow);
}
}
// Function to read the input form elements into a query object
function queryParams() {
// Read all elements in the form into an array
var queryParams = document.queryParams.elements;
// Create the query object
var qry = {};
// loop the form elements and add to query object
for(var el in queryParams){
if(queryParams[el].name && queryParams[el].value) { qry[queryParams[el].name] = queryParams[el].value; }
}
return qry;
}
// Clear the form fields
function clearForm() {
document.queryName.reset();
document.regexFilter.reset();
var form = document.queryParams.elements;
for(var element in form){
//console.dir(document.queryParams[el]);
form[element].value = "";
}
}