-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_utils.js
46 lines (42 loc) · 1.28 KB
/
api_utils.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
const google = require('googleapis');
const googleApiKey = require('../google_api_key.json');
const log = require('./log');
const apiUtils = {
fetchViews() {
return new Promise((resolve, reject) => {
const jwtClient = new google.auth.JWT(
googleApiKey.client_email, null, googleApiKey.private_key,
['https://www.googleapis.com/auth/analytics.readonly'], null
);
jwtClient.authorize((err) => {
if (err) {
return reject(err);
}
const analytics = google.analytics('v3');
return apiUtils.queryData(analytics, jwtClient);
});
});
},
queryData(analytics, jwtClient) {
return new Promise((resolve, reject) => {
analytics.data.ga.get({
auth: jwtClient,
ids: process.env.GA_VIEW_ID,
metrics: 'ga:uniquePageviews',
dimensions: 'ga:pagePath',
'start-date': '30daysAgo',
'end-date': 'yesterday',
sort: '-ga:uniquePageviews',
'max-results': 10,
filters: 'ga:pagePath=~/ch_[-a-z0-9]+[.]html$',
}, (err, response) => {
if (err) {
return reject(err);
}
log.debug({ queryDataResponse: response });
return resolve(JSON.stringify(response));
});
});
},
};
module.exports = apiUtils;