2018-02-15 20:00:51 +01:00
|
|
|
const logger = require('winston');
|
|
|
|
const ua = require('universal-analytics');
|
2018-06-06 04:34:24 +02:00
|
|
|
const { analytics : { googleId }, details: { title } } = require('@config/siteConfig');
|
2018-02-15 20:00:51 +01:00
|
|
|
|
2018-06-06 01:24:27 +02:00
|
|
|
const createServeEventParams = (headers, ip, originalUrl) => {
|
2018-02-15 20:00:51 +01:00
|
|
|
return {
|
2018-02-15 22:18:22 +01:00
|
|
|
eventCategory : 'client requests',
|
|
|
|
eventAction : 'serve request',
|
|
|
|
eventLabel : originalUrl,
|
|
|
|
ipOverride : ip,
|
|
|
|
userAgentOverride: headers['user-agent'],
|
2018-05-25 18:34:09 +02:00
|
|
|
documentReferrer : headers['referer'],
|
2018-02-15 20:00:51 +01:00
|
|
|
};
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-02-15 20:00:51 +01:00
|
|
|
|
2018-06-06 03:01:29 +02:00
|
|
|
const createTimingEventParams = (category, variable, label, startTime, endTime) => {
|
2018-03-07 01:23:21 +01:00
|
|
|
const duration = endTime - startTime;
|
2018-02-15 20:00:51 +01:00
|
|
|
return {
|
2018-03-06 18:02:42 +01:00
|
|
|
userTimingCategory : category,
|
|
|
|
userTimingVariableName: variable,
|
2018-03-07 01:23:21 +01:00
|
|
|
userTimingTime : duration,
|
2018-02-15 20:00:51 +01:00
|
|
|
userTimingLabel : label,
|
|
|
|
};
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-02-15 20:00:51 +01:00
|
|
|
|
2018-06-06 01:24:27 +02:00
|
|
|
const sendGoogleAnalyticsEvent = (ip, params) => {
|
2018-06-07 19:33:15 +02:00
|
|
|
if (!googleId) {
|
|
|
|
return logger.debug('Skipping analytics event because no GoogleId present in configs');
|
|
|
|
}
|
2018-02-15 20:00:51 +01:00
|
|
|
const visitorId = ip.replace(/\./g, '-');
|
2018-03-06 18:02:42 +01:00
|
|
|
const visitor = ua(googleId, visitorId, { strictCidFormat: false, https: true });
|
2018-02-15 20:00:51 +01:00
|
|
|
visitor.event(params, (err) => {
|
|
|
|
if (err) {
|
2018-06-06 01:24:27 +02:00
|
|
|
return logger.error('Google Analytics Event Error >>', err);
|
2018-02-15 20:00:51 +01:00
|
|
|
}
|
2018-06-07 00:59:30 +02:00
|
|
|
logger.debug(`Event successfully sent to google analytics`, params);
|
2018-02-15 20:00:51 +01:00
|
|
|
});
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-02-15 20:00:51 +01:00
|
|
|
|
2018-06-06 03:01:29 +02:00
|
|
|
const sendGoogleAnalyticsTiming = (siteTitle, params) => {
|
2018-06-07 19:33:15 +02:00
|
|
|
if (!googleId) {
|
|
|
|
return logger.debug('Skipping analytics timing because no GoogleId present in configs');
|
|
|
|
}
|
2018-06-06 03:01:29 +02:00
|
|
|
const visitor = ua(googleId, siteTitle, { strictCidFormat: false, https: true });
|
2018-02-15 20:00:51 +01:00
|
|
|
visitor.timing(params, (err) => {
|
|
|
|
if (err) {
|
2018-06-06 01:24:27 +02:00
|
|
|
return logger.error('Google Analytics Event Error >>', err);
|
2018-02-15 20:00:51 +01:00
|
|
|
}
|
2018-06-07 00:59:30 +02:00
|
|
|
logger.debug(`Event successfully sent to google analytics`, params);
|
2018-02-15 20:00:51 +01:00
|
|
|
});
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-05-25 18:34:09 +02:00
|
|
|
|
2018-06-06 01:24:27 +02:00
|
|
|
const sendGAServeEvent = (headers, ip, originalUrl) => {
|
2018-05-25 18:34:09 +02:00
|
|
|
const params = createServeEventParams(headers, ip, originalUrl);
|
|
|
|
sendGoogleAnalyticsEvent(ip, params);
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-05-25 18:34:09 +02:00
|
|
|
|
2018-06-06 01:24:27 +02:00
|
|
|
const sendGATimingEvent = (category, variable, label, startTime, endTime) => {
|
2018-06-06 03:01:29 +02:00
|
|
|
const params = createTimingEventParams(category, variable, label, startTime, endTime);
|
2018-05-25 18:34:09 +02:00
|
|
|
sendGoogleAnalyticsTiming(title, params);
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-05-25 18:34:09 +02:00
|
|
|
|
2018-06-06 01:24:27 +02:00
|
|
|
const chooseGaLbrynetPublishLabel = ({ channel_name: channelName, channel_id: channelId }) => {
|
2018-05-25 18:34:09 +02:00
|
|
|
return (channelName || channelId ? 'PUBLISH_IN_CHANNEL_CLAIM' : 'PUBLISH_ANONYMOUS_CLAIM');
|
2018-06-06 01:24:27 +02:00
|
|
|
};
|
2018-02-15 20:00:51 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2018-05-25 18:34:09 +02:00
|
|
|
sendGAServeEvent,
|
|
|
|
sendGATimingEvent,
|
|
|
|
chooseGaLbrynetPublishLabel,
|
2018-02-15 20:00:51 +01:00
|
|
|
};
|