Merge pull request #70 from lbryio/analytics-3

Analytics 3
This commit is contained in:
Bill Bittner 2017-07-05 09:50:37 -07:00 committed by GitHub
commit f05379c750
5 changed files with 36 additions and 43 deletions

View file

@ -12,7 +12,7 @@
"PublishUploadPath": "/home/lbry/Downloads/" "PublishUploadPath": "/home/lbry/Downloads/"
}, },
"Logging": { "Logging": {
"LogLevel": "debug", "LogLevel": "verbose",
"LogDirectory": "/home/lbry/Logs" "LogDirectory": "/home/lbry/Logs"
} }
} }

View file

@ -27,33 +27,37 @@ module.exports = {
logger.error('sequelize error', error); logger.error('sequelize error', error);
}); });
}, },
sendGoogleAnalytics (action, ip, originalUrl) { sendGoogleAnalytics (action, headers, ip, originalUrl) {
const visitorId = ip.replace(/\./g, '-'); const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
logger.verbose('visitor', visitor);
let params;
switch (action) { switch (action) {
case 'serve': case 'serve':
visitor.event('serve', originalUrl, (err) => { params = {
if (err) { ec : 'serve',
logger.error('Google Analytics Event Error >>', err); ea : originalUrl,
} uip: ip,
}); ua : headers['user-agent'],
break; ul : headers['accept-language'],
case 'show': };
visitor.pageview(originalUrl, 'https://spee.ch', 'show route', (err) => {
if (err) {
logger.error('Google Analytics Pageview Error >>', err);
}
});
break; break;
case 'publish': case 'publish':
visitor.event('publish', originalUrl, (err) => { params = {
if (err) { ec : 'publish',
logger.error('Google Analytics Event Error >>', err); ea : originalUrl,
} uip: ip,
}); ua : headers['user-agent'],
ul : headers['accept-language'],
};
break; break;
default: break; default: break;
} }
visitor.event(params, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
}, },
getStatsSummary () { getStatsSummary () {
logger.debug('retrieving site statistics'); logger.debug('retrieving site statistics');

View file

@ -1,11 +1,9 @@
const logger = require('winston'); const logger = require('winston');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats } = require('../controllers/statsController.js');
module.exports = app => { module.exports = app => {
// route for the home page // route for the home page
app.get('/', ({ headers, ip, originalUrl }, res) => { app.get('/', ({ headers, ip, originalUrl }, res) => {
// google analytics
sendGoogleAnalytics('serve', ip, originalUrl);
// logging // logging
logger.verbose(`GET request on ${originalUrl} from ${ip}`); logger.verbose(`GET request on ${originalUrl} from ${ip}`);
// send response // send response
@ -13,8 +11,6 @@ module.exports = app => {
}); });
// a catch-all route if someone visits a page that does not exist // a catch-all route if someone visits a page that does not exist
app.use('*', ({ originalUrl, ip }, res) => { app.use('*', ({ originalUrl, ip }, res) => {
// google analytics
sendGoogleAnalytics('serve', ip, originalUrl);
// logging // logging
logger.error(`Get request on ${originalUrl} from ${ip} which was a 404`); logger.error(`Get request on ${originalUrl} from ${ip} which was a 404`);
// post to stats // post to stats

View file

@ -32,17 +32,18 @@ function serveFile ({ fileName, fileType, filePath }, res) {
res.status(200).sendFile(filePath, options); res.status(200).sendFile(filePath, options);
} }
function sendAnalyticsAndLog (ip, originalUrl) { function sendAnalyticsAndLog (headers, ip, originalUrl) {
logger.verbose('headers', headers);
// google analytics // google analytics
sendGoogleAnalytics('serve', ip, originalUrl); sendGoogleAnalytics('serve', headers, ip, originalUrl);
// logging // logging
logger.verbose(`GET request on ${originalUrl} from ${ip}`); logger.verbose(`GET request on ${originalUrl} from ${ip}`);
} }
module.exports = (app) => { module.exports = (app) => {
// route to fetch one free public claim // route to fetch one free public claim
app.get('/:name/:claim_id', ({ ip, originalUrl, params }, res) => { app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(ip, originalUrl); sendAnalyticsAndLog(headers, ip, originalUrl);
// begin image-serve processes // begin image-serve processes
serveController serveController
.getClaimByClaimId(params.name, params.claim_id) .getClaimByClaimId(params.name, params.claim_id)
@ -60,8 +61,8 @@ module.exports = (app) => {
}); });
}); });
// route to fetch one free public claim // route to fetch one free public claim
app.get('/:name', ({ ip, originalUrl, params }, res) => { app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(ip, originalUrl); sendAnalyticsAndLog(headers, ip, originalUrl);
// begin image-serve processes // begin image-serve processes
serveController serveController
.getClaimByName(params.name) .getClaimByName(params.name)

View file

@ -1,26 +1,18 @@
const logger = require('winston'); const logger = require('winston');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { getAllClaims } = require('../controllers/showController.js'); const { getAllClaims } = require('../controllers/showController.js');
const { getStatsSummary, postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { getStatsSummary, postToStats } = require('../controllers/statsController.js');
function sendAnalyticsAndLog (ip, originalUrl) {
// google analytics
sendGoogleAnalytics('show', ip, originalUrl);
// logging
logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and serve the content
}
module.exports = (app) => { module.exports = (app) => {
// route to show 'about' page for spee.ch // route to show 'about' page for spee.ch
app.get('/about', ({ ip, originalUrl }, res) => { app.get('/about', ({ ip, originalUrl }, res) => {
sendAnalyticsAndLog(ip, originalUrl); logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and render the content // get and render the content
res.status(200).render('about'); res.status(200).render('about');
}); });
// route to show the meme-fodder meme maker // route to show the meme-fodder meme maker
app.get('/meme-fodder/play', ({ ip, originalUrl }, res) => { app.get('/meme-fodder/play', ({ ip, originalUrl }, res) => {
sendAnalyticsAndLog(ip, originalUrl); logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and render the content // get and render the content
getAllClaims('meme-fodder') getAllClaims('meme-fodder')
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {
@ -33,7 +25,7 @@ module.exports = (app) => {
}); });
// route to show statistics for spee.ch // route to show statistics for spee.ch
app.get('/stats', ({ ip, originalUrl }, res) => { app.get('/stats', ({ ip, originalUrl }, res) => {
sendAnalyticsAndLog(ip, originalUrl); logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and render the content // get and render the content
getStatsSummary() getStatsSummary()
.then(result => { .then(result => {
@ -46,7 +38,7 @@ module.exports = (app) => {
}); });
// route to display all free public claims at a given name // route to display all free public claims at a given name
app.get('/:name/all', ({ ip, originalUrl, params }, res) => { app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(ip, originalUrl); logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and render the content // get and render the content
getAllClaims(params.name) getAllClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreePublicClaims => {