diff --git a/controllers/statsController.js b/controllers/statsController.js index 3aac00df..0e736d66 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -1,7 +1,60 @@ const logger = require('winston'); +const ua = require('universal-analytics'); +const config = require('config'); const db = require('../models'); +const googleApiKey = config.get('AnalyticsConfig.GoogleId'); module.exports = { + postToStats: (action, url, ipAddress, result) => { + logger.silly('creating record for statistics db'); + // make sure the result is a string + if (result && (typeof result !== 'string')) { + result = result.toString(); + } + // // make sure the ip address(es) are a string + if (ipAddress && (typeof ipAddress !== 'string')) { + ipAddress = ipAddress.toString(); + } + // create record in the db + db.Stats.create({ + action, + url, + ipAddress, + result, + }) + .then() + .catch(error => { + logger.error('sequelize error', error); + }); + }, + sendGoogleAnalytics: (action, ip, originalUrl) => { + const visitorId = ip.replace(/\./g, '-'); + const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); + switch (action) { + case 'serve': + visitor.event('serve', originalUrl, (err) => { + if (err) { + logger.error('Google Analytics Event Error >>', err); + } + }); + break; + case 'show': + visitor.pageview(originalUrl, 'https://spee.ch', 'show route', (err) => { + if (err) { + logger.error('Google Analytics Pageview Error >>', err); + } + }); + break; + case 'publish': + visitor.event('publish', originalUrl, (err) => { + if (err) { + logger.error('Google Analytics Event Error >>', err); + } + }); + break; + default: break; + } + }, getStatsSummary: () => { logger.debug('retrieving site statistics'); const deferred = new Promise((resolve, reject) => { diff --git a/helpers/libraries/errorHandlers.js b/helpers/libraries/errorHandlers.js index cc057ea5..f2ab9984 100644 --- a/helpers/libraries/errorHandlers.js +++ b/helpers/libraries/errorHandlers.js @@ -1,5 +1,5 @@ const logger = require('winston'); -const { postToStats } = require('./statsHelpers.js'); +const { postToStats } = require('./statsController.js'); module.exports = { handleRequestError (action, originalUrl, ip, error, res) { diff --git a/helpers/libraries/statsHelpers.js b/helpers/libraries/statsHelpers.js deleted file mode 100644 index f5db997a..00000000 --- a/helpers/libraries/statsHelpers.js +++ /dev/null @@ -1,58 +0,0 @@ -const db = require('../../models'); -const logger = require('winston'); -const ua = require('universal-analytics'); -const config = require('config'); -const googleApiKey = config.get('AnalyticsConfig.GoogleId'); - -module.exports = { - postToStats: (action, url, ipAddress, result) => { - logger.silly('creating record for statistics db'); - // make sure the result is a string - if (result && (typeof result !== 'string')) { - result = result.toString(); - } - // // make sure the ip address(es) are a string - if (ipAddress && (typeof ipAddress !== 'string')) { - ipAddress = ipAddress.toString(); - } - // create record in the db - db.Stats.create({ - action, - url, - ipAddress, - result, - }) - .then() - .catch(error => { - logger.error('sequelize error', error); - }); - }, - sendGoogleAnalytics: (action, ip, originalUrl) => { - const visitorId = ip.replace(/\./g, '-'); - const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); - switch (action) { - case 'serve': - visitor.event('serve', originalUrl, (err) => { - if (err) { - logger.error('Google Analytics Event Error >>', err); - } - }); - break; - case 'show': - visitor.pageview(originalUrl, 'https://spee.ch', 'show route', (err) => { - if (err) { - logger.error('Google Analytics Pageview Error >>', err); - } - }); - break; - case 'publish': - visitor.event('publish', originalUrl, (err) => { - if (err) { - logger.error('Google Analytics Event Error >>', err); - } - }); - break; - default: break; - } - }, -}; diff --git a/routes/api-routes.js b/routes/api-routes.js index 01c0df3d..c9aa51a9 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -5,7 +5,7 @@ const publishController = require('../controllers/publishController.js'); const lbryApi = require('../helpers/libraries/lbryApi.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js'); -const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); +const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js'); module.exports = app => { // route to run a claim_list request on the daemon diff --git a/routes/home-routes.js b/routes/home-routes.js index d41cd000..e475b712 100644 --- a/routes/home-routes.js +++ b/routes/home-routes.js @@ -1,5 +1,5 @@ const logger = require('winston'); -const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); +const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js'); module.exports = app => { // route for the home page diff --git a/routes/serve-routes.js b/routes/serve-routes.js index fcdc43b2..3d382b88 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -1,7 +1,7 @@ const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const serveController = require('../controllers/serveController.js'); const logger = require('winston'); -const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); +const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js'); function serveFile ({ fileName, fileType, filePath }, res) { logger.info(`serving file ${fileName}`); diff --git a/routes/show-routes.js b/routes/show-routes.js index cb89fa27..da9c5580 100644 --- a/routes/show-routes.js +++ b/routes/show-routes.js @@ -1,7 +1,7 @@ const logger = require('winston'); const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const showController = require('../controllers/showController.js'); -const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsHelpers.js'); +const { postToStats, sendGoogleAnalytics } = require('../helpers/libraries/statsController.js'); const statsController = require('../controllers/statsController.js'); function sendAnalyticsAndLog (ip, originalUrl) { diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index dfc41b53..622a4294 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -2,7 +2,7 @@ const logger = require('winston'); const publishController = require('../controllers/publishController.js'); const publishHelpers = require('../helpers/libraries/publishHelpers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js'); -const { postToStats } = require('../helpers/libraries/statsHelpers.js'); +const { postToStats } = require('../helpers/libraries/statsController.js'); module.exports = (app, siofu, hostedContentPath) => { const http = require('http').Server(app);