forked from fdnd-task/proof-of-concept
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
183 lines (126 loc) · 4.69 KB
/
server.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
import dotenv from "dotenv";
dotenv.config()
console.log(process.env.PRIVATE_KEY)
// 1. Opzetten van de webserver
import express from 'express'
import fetchJson from './helpers/fetch-json.js'
// Import the Google Analytics Data API client library.
const propertyId = '301922918';
import {BetaAnalyticsDataClient} from '@google-analytics/data';
const analyticsDataClient = new BetaAnalyticsDataClient();
// Maak een nieuwe express app aan
const app = express()
// Stel ejs in als template engine
app.set('view engine', 'ejs')
// Stel de map met ejs templates in
app.set('views', './views')
// Gebruik de map 'public' voor statische resources, zoals stylesheets, afbeeldingen en client-side JavaScript
app.use(express.static('public'))
// Zorg dat werken met request data makkelijker wordt
app.use(express.urlencoded({ extended: true }))
// Routes and data
// GET route for the index page
app.get('/', function (request, response) {
response.render('index');
});
// GET route voor de preface
app.get('/preface', async function (request, response) {
const [apiAchievement] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data total users per hour
dateRanges: [{ startDate: '2024-06-01',
endDate: 'today',
},],
dimensions: [{ name: 'hour', },],
metrics: [{ name: 'totalUsers', },],
});
const [apiUsers] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data total users per hour
dateRanges: [{ startDate: '2024-06-01',
endDate: 'today',
},],
dimensions: [{ name: 'hour', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiNewUser] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data total users per hour
dateRanges: [{ startDate: '2024-06-01',
endDate: 'today',
},],
dimensions: [{ name: 'hour', },],
metrics: [{ name: 'newUsers', },],
});
response.render('preface', {
achievement: apiAchievement,
users: apiUsers,
newuser: apiNewUser})
})
// Route for the dashboard page
app.get('/dashboard', async function (request, response) {
const [apiSessions] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users firstsessions
dateRanges: [{ startDate: '2023-06-01',
endDate: 'today',
},],
dimensions: [{ name: 'firstSessionDate', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiContinent] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users per continent
dateRanges: [{ startDate: '2023-06-01',
endDate: 'today', },],
dimensions: [{ name: 'continent', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiCountry] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users per country
dateRanges: [{ startDate: '2021-06-01',
endDate: 'today', },],
dimensions: [{ name: 'country', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiCity] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users per city
dateRanges: [{ startDate: '2023-06-01',
endDate: 'today', },],
dimensions: [{ name: 'city', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiGoogleAd] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users per city
dateRanges: [{ startDate: '2023-06-01',
endDate: 'today', },],
dimensions: [{ name: 'sessionGoogleAdsAdGroupId', },],
metrics: [{ name: 'activeUsers', },],
});
const [apiBrowser] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
// data active users per city
dateRanges: [{ startDate: '2023-06-01',
endDate: 'today', },],
dimensions: [{ name: 'browser', },],
metrics: [{ name: 'activeUsers', },],
});
response.render('dashboard', {
session: apiSessions,
continent : apiContinent,
country : apiCountry,
city : apiCity,
browser: apiBrowser,
googleAd: apiGoogleAd
})
})
// Stel het portnummer in waar express op moet gaan
app.set('port', process.env.PORT || 8000)
// Start express op, haal daarbij het zojuist ingestelde poortnummer op
app.listen(app.get('port'), function () {
// Toon een bericht in de console en geef het poortnummer door
console.log(`Application started on http://localhost:${app.get('port')}`)
})