added analytics to serve routes

This commit is contained in:
bill bittner 2017-06-29 16:10:06 -07:00
parent 6725345db3
commit 9fcbcd299c
2 changed files with 24 additions and 3 deletions

View file

@ -3,6 +3,20 @@ const serveController = require('../controllers/serveController.js');
const logger = require('winston');
const { postToStats } = require('../helpers/libraries/statsHelpers.js');
function sendGoogleAnalytics (ua, googleApiKey, ip, originalUrl) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
visitor.pageview(originalUrl, 'https://spee.ch', 'Serve Route', (err) => {
if (err) {
logger.error('Google Analytics Pageview Error >>', err);
}
}).event('Serve', originalUrl, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
}
function serveFile ({ fileName, fileType, filePath }, res) {
logger.info(`serving file ${fileName}`);
// set default options
@ -32,9 +46,12 @@ function serveFile ({ fileName, fileType, filePath }, res) {
res.status(200).sendFile(filePath, options);
}
module.exports = (app) => {
module.exports = (app, ua, googleApiKey) => {
// route to fetch one free public claim
app.get('/:name/:claim_id', ({ originalUrl, params, ip }, res) => {
// google analytics
sendGoogleAnalytics(ua, googleApiKey, ip, originalUrl);
// logging
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
// begin image-serve processes
serveController
@ -49,6 +66,9 @@ module.exports = (app) => {
});
// route to fetch one free public claim
app.get('/:name', ({ originalUrl, params, ip }, res) => {
// google analytics
sendGoogleAnalytics(ua, googleApiKey, ip, originalUrl);
// logging
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
// begin image-serve processes
serveController

View file

@ -6,7 +6,8 @@ const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const config = require('config');
const winston = require('winston');
const ua = require('universal-analytics');
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
const hostedContentPath = config.get('Database.PublishUploadPath');
// configure logging
@ -57,7 +58,7 @@ app.set('view engine', 'handlebars');
// require express routes
require('./routes/api-routes.js')(app);
require('./routes/show-routes.js')(app);
require('./routes/serve-routes.js')(app);
require('./routes/serve-routes.js')(app, ua, googleApiKey);
require('./routes/home-routes.js')(app);
// require socket.io routes