Analytics 3 #70

Merged
bones7242 merged 5 commits from analytics-3 into master 2017-07-05 18:50:38 +02:00
5 changed files with 36 additions and 43 deletions

View file

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

View file

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

View file

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

View file

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

View file

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