spee.ch/helpers/statsHelpers.js

96 lines
3 KiB
JavaScript
Raw Normal View History

2018-01-23 21:08:53 +01:00
import * as constants from '../constants';
const logger = require('winston');
const ua = require('universal-analytics');
2017-11-07 00:18:45 +01:00
const config = require('../config/speechConfig.js');
const googleApiKey = config.analytics.googleId;
2018-01-23 21:08:53 +01:00
const db = require('../models');
module.exports = {
2018-01-23 21:47:10 +01:00
createPublishTimingEventParams (publishDurration, ip, headers, label) {
return {
userTimingCategory : 'lbrynet',
userTimingVariableName: 'publish',
userTimingTime : publishDurration,
userTimingLabel : label,
uip : ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
},
2017-07-13 00:30:31 +02:00
postToStats (action, url, ipAddress, name, claimId, result) {
logger.debug('action:', action);
// make sure the result is a string
if (result && (typeof result !== 'string')) {
result = result.toString();
}
2017-07-20 00:42:56 +02:00
// make sure the ip address(es) are a string
if (ipAddress && (typeof ipAddress !== 'string')) {
ipAddress = ipAddress.toString();
}
2017-07-13 00:30:31 +02:00
db.File
.findOne({where: { name, claimId }})
.then(file => {
// create record in the db
let FileId;
if (file) {
FileId = file.dataValues.id;
} else {
FileId = null;
}
return db.Request
.create({
action,
url,
ipAddress,
result,
FileId,
});
})
.catch(error => {
2017-09-14 00:59:29 +02:00
logger.error('Sequelize error >>', error);
2017-07-13 00:30:31 +02:00
});
},
2018-01-23 01:14:05 +01:00
sendGoogleAnalyticsEvent (action, headers, ip, originalUrl) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
2017-07-05 18:26:22 +02:00
let params;
switch (action) {
2017-08-04 06:59:22 +02:00
case 'SERVE':
2017-07-05 18:26:22 +02:00
params = {
ec : 'serve',
ea : originalUrl,
uip: ip,
ua : headers['user-agent'],
ul : headers['accept-language'],
};
break;
2018-01-23 01:14:05 +01:00
default: break;
}
visitor.event(params, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
},
sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
2018-01-23 21:08:53 +01:00
const durration = endTime - startTime;
2018-01-23 01:14:05 +01:00
let params;
switch (action) {
2018-01-23 21:08:53 +01:00
case constants.PUBLISH_ANONYMOUS_CLAIM:
case constants.PUBLISH_IN_CHANNEL_CLAIM:
logger.verbose(`${action} completed successfully in ${durration}ms`);
2018-01-23 21:47:10 +01:00
params = module.exports.createPublishTimingEventParams(durration, ip, headers, action);
break;
default: break;
}
2018-01-23 01:14:05 +01:00
visitor.timing(params, (err) => {
2017-07-05 18:26:22 +02:00
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
2018-01-23 21:08:53 +01:00
logger.debug(`${action} timing event successfully sent to google analytics`);
2017-07-05 18:26:22 +02:00
});
},
};