-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving Google Analytics requests to backend (#154)
* Moving Google Analytics requetss to backend * Requested Changes * Minor changes
- Loading branch information
1 parent
8f92e6b
commit c2b4884
Showing
6 changed files
with
369 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}) |
Oops, something went wrong.