Skip to content

Commit

Permalink
Moving Google Analytics requests to backend (#154)
Browse files Browse the repository at this point in the history
* Moving Google Analytics requetss to backend

* Requested Changes

* Minor changes
  • Loading branch information
AuraOfDivinity authored Aug 7, 2020
1 parent 8f92e6b commit c2b4884
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const commentRouter = require('./app/routes/comment')
const projectRouter = require('./app/routes/project')
const notificationRouter = require('./app/routes/notification')
const proposalRouter = require('./app/routes/proposal')
const analyticsRouter = require('./app/routes/analytics')

const app = express()
const server = require('http').Server(app)
Expand Down Expand Up @@ -81,6 +82,7 @@ app.use('/shortUrl', shortUrlRouter)
app.use('/comment', commentRouter)
app.use('/project', projectRouter)
app.use('/proposal', proposalRouter)
app.use('/analytics', analyticsRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
Expand Down
104 changes: 104 additions & 0 deletions app/controllers/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const { google } = require('googleapis')
const analytics = google.analytics('v3')
const jwt = require('../../config/gAnalytics')
const viewId = process.env.VIEW_ID
const HANDLER = require('../utils/response-helper')
const HttpStatus = require('http-status-codes')

module.exports = {
getBrowser: async (req, res, next) => {
const { startDate, endDate, proposalId } = req.body
console.log(req.body)
try {
const result = await analytics.data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
metrics: 'ga:users',
dimensions: ['ga:browser'],
'start-date': startDate,
'end-date': endDate,
filters: `ga:pagePath==/${proposalId}`
})
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
} catch (error) {
HANDLER.handleError(res, error)
}
},

getCountries: async (req, res, next) => {
const { startDate, endDate, proposalId } = req.body

try {
const result = await analytics.data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
metrics: 'ga:users',
dimensions: ['ga:country'],
'start-date': startDate,
'end-date': endDate,
filters: `ga:pagePath==/${proposalId}`
})
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
} catch (error) {
HANDLER.handleError(res, error)
}
},

getDevice: async (req, res, next) => {
const { startDate, endDate, proposalId } = req.body

try {
const result = await analytics.data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
metrics: 'ga:users',
dimensions: ['ga:deviceCategory'],
'start-date': startDate,
'end-date': endDate,
filters: `ga:pagePath==/${proposalId}`
})
res.status(HttpStatus.OK).json({ analytics: result.data.rows })
} catch (error) {
HANDLER.handleError(res, error)
}
},

getTopProposals: async (req, res, next) => {
const { startDate, endDate } = req.body

try {
const result = await analytics.data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
metrics: 'ga:pageviews',
dimensions: ['ga:pagePath'],
'start-date': startDate,
'end-date': endDate,
filters: 'ga:pagePath!=/homepage'
})
res.status(HttpStatus.OK).json({ analytics: result.data })
} catch (error) {
HANDLER.handleError(res, error)
}
},

getProposalViews: async (req, res, next) => {
const { startDate, endDate, proposalId } = req.body

try {
const result = await analytics.data.ga.get({
auth: jwt,
ids: `ga:${viewId}`,
metrics: 'ga:pageviews',
dimensions: ['ga:date'],
'start-date': startDate,
'end-date': endDate,
filters: `ga:pagePath==/${proposalId}`
})

res.status(HttpStatus.OK).json({ analytics: result.data.rows })
} catch (error) {
HANDLER.handleError(res, error)
}
}
}
22 changes: 22 additions & 0 deletions app/routes/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express')
const router = express.Router()
const analyticsController = require('../controllers/analytics')
const auth = require('../middleware/auth')
const isUnderMaintenance = require('../middleware/maintenance')

// Get Browser analytics
router.post('/browser', isUnderMaintenance, auth, analyticsController.getBrowser)

// Get country analytics
router.post('/countries', isUnderMaintenance, auth, analyticsController.getCountries)

// Get Device analytics
router.post('/device', isUnderMaintenance, auth, analyticsController.getDevice)

// Get most viewed Proposals
router.post('/mostviewed', isUnderMaintenance, auth, analyticsController.getTopProposals)

// Get Views of a specific proposal
router.post('/views', isUnderMaintenance, auth, analyticsController.getProposalViews)

module.exports = router
10 changes: 10 additions & 0 deletions config/gAnalytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { google } = require('googleapis')
const clientEMail = process.env.CLIENT_EMAIL
const privateKey = process.env.PRIVATE_KEY
const scope = process.env.SCOPE

module.exports = new google.auth.JWT({
email: clientEMail,
key: privateKey,
scopes: [scope]
})
Loading

0 comments on commit c2b4884

Please sign in to comment.