|
| 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 | +} |
0 commit comments