-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_users.js
More file actions
105 lines (92 loc) · 2.42 KB
/
Copy pathactive_users.js
File metadata and controls
105 lines (92 loc) · 2.42 KB
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
// let propertyId = 309400325;
//let propertyId = 307224650;
let defaultPropertyId = 270599279;
// Imports the Google Analytics Data API client library.
const { BetaAnalyticsDataClient } = require("@google-analytics/data");
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
const analyticsDataClient = new BetaAnalyticsDataClient();
function getData(req, res) {
if (!req.query["startDate"]) {
res.status(400).json({
success: false,
message: "No startDate provided",
});
return;
}
if (!req.query["endDate"]) {
res.status(400).json({
success: false,
message: "No endDate provided",
});
return;
}
if (!req.query["propertyId"]) {
res.status(400).json({
success: false,
message: "No propertyId provided",
});
return;
}
let startDate = req.query["startDate"];
let endDate = req.query["endDate"];
let propertyId = req.query["propertyId"];
let metrics = ["activeUsers"];
let dimensions = ["city"];
if (req.query.metrics) {
metrics = req.query.metrics;
if (typeof metrics == "string") {
metrics = [metrics];
}
}
if (req.query.dimensions) {
dimensions = req.query.dimensions;
if (typeof dimensions == "string") {
dimensions = [dimensions];
}
}
console.log("query string " + JSON.stringify(req.query));
console.log(startDate);
analyticsDataClient
.runReport({
property: `properties/${propertyId}`,
dateRanges: [
{
startDate: startDate,
endDate: endDate,
},
],
dimensions: dimensions.map((name) => ({
name: name,
})),
metrics: metrics.map((name) => ({
name: name,
})),
})
.then((report) => {
console.log("Report result:");
console.log(JSON.stringify(report, null, 2));
res.json(report[0]);
/*
let cities = [],
data = [];
report[0].rows.forEach((row) => {
console.log(row.dimensionValues[0], row.metricValues[0]);
cities.push(row.dimensionValues[0].value);
data.push(parseInt(row.metricValues[0].value));
});
console.log("here is the result");
res.json({
cities: cities,
activeUsers: data,
});
*/
})
.catch((error) => {
res.status(500);
res.json(error);
});
}
module.exports = {
getData,
};