Skip to content

Commit c2b4884

Browse files
Moving Google Analytics requests to backend (#154)
* Moving Google Analytics requetss to backend * Requested Changes * Minor changes
1 parent 8f92e6b commit c2b4884

File tree

6 files changed

+369
-4
lines changed

6 files changed

+369
-4
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const commentRouter = require('./app/routes/comment')
2222
const projectRouter = require('./app/routes/project')
2323
const notificationRouter = require('./app/routes/notification')
2424
const proposalRouter = require('./app/routes/proposal')
25+
const analyticsRouter = require('./app/routes/analytics')
2526

2627
const app = express()
2728
const server = require('http').Server(app)
@@ -81,6 +82,7 @@ app.use('/shortUrl', shortUrlRouter)
8182
app.use('/comment', commentRouter)
8283
app.use('/project', projectRouter)
8384
app.use('/proposal', proposalRouter)
85+
app.use('/analytics', analyticsRouter)
8486

8587
// catch 404 and forward to error handler
8688
app.use(function (req, res, next) {

app/controllers/analytics.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const { google } = require('googleapis')
2+
const analytics = google.analytics('v3')
3+
const jwt = require('../../config/gAnalytics')
4+
const viewId = process.env.VIEW_ID
5+
const HANDLER = require('../utils/response-helper')
6+
const HttpStatus = require('http-status-codes')
7+
8+
module.exports = {
9+
getBrowser: async (req, res, next) => {
10+
const { startDate, endDate, proposalId } = req.body
11+
console.log(req.body)
12+
try {
13+
const result = await analytics.data.ga.get({
14+
auth: jwt,
15+
ids: `ga:${viewId}`,
16+
metrics: 'ga:users',
17+
dimensions: ['ga:browser'],
18+
'start-date': startDate,
19+
'end-date': endDate,
20+
filters: `ga:pagePath==/${proposalId}`
21+
})
22+
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
23+
} catch (error) {
24+
HANDLER.handleError(res, error)
25+
}
26+
},
27+
28+
getCountries: async (req, res, next) => {
29+
const { startDate, endDate, proposalId } = req.body
30+
31+
try {
32+
const result = await analytics.data.ga.get({
33+
auth: jwt,
34+
ids: `ga:${viewId}`,
35+
metrics: 'ga:users',
36+
dimensions: ['ga:country'],
37+
'start-date': startDate,
38+
'end-date': endDate,
39+
filters: `ga:pagePath==/${proposalId}`
40+
})
41+
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
42+
} catch (error) {
43+
HANDLER.handleError(res, error)
44+
}
45+
},
46+
47+
getDevice: async (req, res, next) => {
48+
const { startDate, endDate, proposalId } = req.body
49+
50+
try {
51+
const result = await analytics.data.ga.get({
52+
auth: jwt,
53+
ids: `ga:${viewId}`,
54+
metrics: 'ga:users',
55+
dimensions: ['ga:deviceCategory'],
56+
'start-date': startDate,
57+
'end-date': endDate,
58+
filters: `ga:pagePath==/${proposalId}`
59+
})
60+
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
61+
} catch (error) {
62+
HANDLER.handleError(res, error)
63+
}
64+
},
65+
66+
getTopProposals: async (req, res, next) => {
67+
const { startDate, endDate } = req.body
68+
69+
try {
70+
const result = await analytics.data.ga.get({
71+
auth: jwt,
72+
ids: `ga:${viewId}`,
73+
metrics: 'ga:pageviews',
74+
dimensions: ['ga:pagePath'],
75+
'start-date': startDate,
76+
'end-date': endDate,
77+
filters: 'ga:pagePath!=/homepage'
78+
})
79+
res.status(HttpStatus.OK).json({ analytics: result.data })
80+
} catch (error) {
81+
HANDLER.handleError(res, error)
82+
}
83+
},
84+
85+
getProposalViews: async (req, res, next) => {
86+
const { startDate, endDate, proposalId } = req.body
87+
88+
try {
89+
const result = await analytics.data.ga.get({
90+
auth: jwt,
91+
ids: `ga:${viewId}`,
92+
metrics: 'ga:pageviews',
93+
dimensions: ['ga:date'],
94+
'start-date': startDate,
95+
'end-date': endDate,
96+
filters: `ga:pagePath==/${proposalId}`
97+
})
98+
99+
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
100+
} catch (error) {
101+
HANDLER.handleError(res, error)
102+
}
103+
}
104+
}

app/routes/analytics.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
const analyticsController = require('../controllers/analytics')
4+
const auth = require('../middleware/auth')
5+
const isUnderMaintenance = require('../middleware/maintenance')
6+
7+
// Get Browser analytics
8+
router.post('/browser', isUnderMaintenance, auth, analyticsController.getBrowser)
9+
10+
// Get country analytics
11+
router.post('/countries', isUnderMaintenance, auth, analyticsController.getCountries)
12+
13+
// Get Device analytics
14+
router.post('/device', isUnderMaintenance, auth, analyticsController.getDevice)
15+
16+
// Get most viewed Proposals
17+
router.post('/mostviewed', isUnderMaintenance, auth, analyticsController.getTopProposals)
18+
19+
// Get Views of a specific proposal
20+
router.post('/views', isUnderMaintenance, auth, analyticsController.getProposalViews)
21+
22+
module.exports = router

config/gAnalytics.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { google } = require('googleapis')
2+
const clientEMail = process.env.CLIENT_EMAIL
3+
const privateKey = process.env.PRIVATE_KEY
4+
const scope = process.env.SCOPE
5+
6+
module.exports = new google.auth.JWT({
7+
email: clientEMail,
8+
key: privateKey,
9+
scopes: [scope]
10+
})

0 commit comments

Comments
 (0)