-
Notifications
You must be signed in to change notification settings - Fork 0
/
processData.js
103 lines (83 loc) · 3.17 KB
/
processData.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
/* Generates Columbia Daily Spectator Weekly Report Data
* through Google Analytics API v4
*/
const { google } = require('googleapis');
const key = require('./auth.json');
const fs = require('fs');
const requestBodies = require('./requestBodies');
const outputFunctions = require('./outputFunctions');
const auth = require('./auth.js');
const {
requestBodiesCDS,
requestBodiesSection,
sectionsArr,
} = require('./requestBodies');
// Generates, processes, cleans, and compiles CDS weekly report data
// 1. Authorizes Google Analytics API v4 (See auth.js)
// 2. Processes CDS Overall data
// 3. Processes each section as specified in requestBodies.sectionArr
// 4. Compiles data into data JSON object
async function generateWeeklyReportData() {
//Authorization from auth.js
const analytics = await auth.authorize();
console.log('Acquiring and Cleaning Analytics Data...\n');
// JSON object storing processed data
var data = {
cds: {},
news: {},
opinion: {},
'arts-and-entertainment': {},
sports: {},
spectrum: {},
'the-eye': {},
};
// Processes CDS Overall and stores in data.cds
console.log('Processing CDS Overall:');
// Loops through each requestBody template in requestBodies.requestBodiesCDS
for (let i = 0; i < requestBodiesCDS.length; i++) {
// JSON request template as specified by Analytics API
let request = requestBodies.requestBodiesCDS[i];
// Queries api with
let APIQueryResult = await analytics.reports.batchGet(request);
let report = APIQueryResult.data.reports[0];
// Formats report data into data.cds
request.outputFunction(report, data.cds);
}
// Same general process as above but for each section of website
// Loops through each section in requestBodies.sectionsARr
for (let i = 0; i < sectionsArr.length; i++) {
let currSection = sectionsArr[i];
console.log('Processing ' + currSection + ': ');
let sectionBody = requestBodies.requestBodiesSection(currSection);
// GETs data from Google Analytics API
for (let j = 0; j < sectionBody.length; j++) {
let request = sectionBody[j];
let APIQueryResult = await analytics.reports.batchGet(request);
let report = APIQueryResult.data.reports[0];
request.outputFunction(report, data[currSection]);
}
}
return data;
// TODO throw exception for non 2xx status code
//https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide#request
}
// Creates filename based on today's date
function generateFilename() {
// Yes, formatting dates really is this complicated...
const today = new Date();
let lastWeek = new Date();
lastWeek.setDate(today.getDate() - 7);
let todayString = today.toLocaleDateString('default', {
month: 'short',
day: '2-digit',
});
let lastWeekString = lastWeek.toLocaleDateString('default', {
month: 'short',
day: '2-digit',
});
return `${lastWeekString} - ${todayString} [CDS] Weekly Report`;
}
module.exports = {
generateWeeklyReportData,
generateFilename,
};