spee.ch/helpers/googleAnalytics.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-02-15 20:00:51 +01:00
const logger = require('winston');
const ua = require('universal-analytics');
const config = require('../config/speechConfig.js');
const googleApiKey = config.analytics.googleId;
function createServeEventParams (headers, ip, originalUrl) {
return {
2018-02-15 22:18:22 +01:00
eventCategory : 'client requests',
eventAction : 'serve request',
eventLabel : originalUrl,
ipOverride : ip,
userAgentOverride: headers['user-agent'],
2018-02-15 20:00:51 +01:00
};
};
function createPublishTimingEventParams (label, startTime, endTime, ip, headers) {
const durration = endTime - startTime;
return {
userTimingCategory : 'lbrynet',
userTimingVariableName: 'publish',
userTimingTime : durration,
userTimingLabel : label,
uip : ip,
2018-02-15 22:18:22 +01:00
userAgentOverride : headers['user-agent'],
2018-02-15 20:00:51 +01:00
};
};
function sendGoogleAnalyticsEvent (ip, params) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
visitor.event(params, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
});
};
function sendGoogleAnalyticsTiming (ip, params) {
const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
visitor.timing(params, (err) => {
if (err) {
logger.error('Google Analytics Event Error >>', err);
}
logger.debug(`Timing event successfully sent to google analytics`);
});
};
module.exports = {
sendGAServeEvent (headers, ip, originalUrl) {
const params = createServeEventParams(headers, ip, originalUrl);
sendGoogleAnalyticsEvent(ip, params);
},
sendGAAnonymousPublishTiming (headers, ip, originalUrl, startTime, endTime) {
2018-02-27 18:52:05 +01:00
const params = createPublishTimingEventParams('PUBLISH_ANONYMOUS_CLAIM', startTime, endTime, ip, headers);
2018-02-15 20:00:51 +01:00
sendGoogleAnalyticsTiming(ip, params);
},
sendGAChannelPublishTiming (headers, ip, originalUrl, startTime, endTime) {
2018-02-27 18:52:05 +01:00
const params = createPublishTimingEventParams('PUBLISH_IN_CHANNEL_CLAIM', startTime, endTime, ip, headers);
2018-02-15 20:00:51 +01:00
sendGoogleAnalyticsTiming(ip, params);
},
};