split ga timing events into lbrynet and end-to-end

This commit is contained in:
bill bittner 2018-03-06 09:02:42 -08:00
parent be420b362a
commit 5a13b23016
3 changed files with 35 additions and 30 deletions

View file

@ -1,7 +1,6 @@
const logger = require('winston'); const logger = require('winston');
const ua = require('universal-analytics'); const ua = require('universal-analytics');
const config = require('../config/speechConfig.js'); const { analytics : { googleId }, site: { name: siteName } } = require('../config/speechConfig.js');
const googleApiKey = config.analytics.googleId;
function createServeEventParams (headers, ip, originalUrl) { function createServeEventParams (headers, ip, originalUrl) {
return { return {
@ -13,21 +12,19 @@ function createServeEventParams (headers, ip, originalUrl) {
}; };
}; };
function createPublishTimingEventParams (label, startTime, endTime, ip, headers) { function createPublishTimingEventParams (category, variable, label, startTime, endTime) {
const durration = endTime - startTime; const durration = endTime - startTime;
return { return {
userTimingCategory : 'lbrynet', userTimingCategory : category,
userTimingVariableName: 'publish', userTimingVariableName: variable,
userTimingTime : durration, userTimingTime : durration,
userTimingLabel : label, userTimingLabel : label,
uip : ip,
userAgentOverride : headers['user-agent'],
}; };
}; };
function sendGoogleAnalyticsEvent (ip, params) { function sendGoogleAnalyticsEvent (ip, params) {
const visitorId = ip.replace(/\./g, '-'); const visitorId = ip.replace(/\./g, '-');
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); const visitor = ua(googleId, visitorId, { strictCidFormat: false, https: true });
visitor.event(params, (err) => { visitor.event(params, (err) => {
if (err) { if (err) {
logger.error('Google Analytics Event Error >>', err); logger.error('Google Analytics Event Error >>', err);
@ -35,9 +32,8 @@ function sendGoogleAnalyticsEvent (ip, params) {
}); });
}; };
function sendGoogleAnalyticsTiming (ip, params) { function sendGoogleAnalyticsTiming (visitorId, params) {
const visitorId = ip.replace(/\./g, '-'); const visitor = ua(googleId, visitorId, { strictCidFormat: false, https: true });
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
visitor.timing(params, (err) => { visitor.timing(params, (err) => {
if (err) { if (err) {
logger.error('Google Analytics Event Error >>', err); logger.error('Google Analytics Event Error >>', err);
@ -51,12 +47,14 @@ module.exports = {
const params = createServeEventParams(headers, ip, originalUrl); const params = createServeEventParams(headers, ip, originalUrl);
sendGoogleAnalyticsEvent(ip, params); sendGoogleAnalyticsEvent(ip, params);
}, },
sendGAAnonymousPublishTiming (headers, ip, originalUrl, startTime, endTime) { sendGATimingEvent (category, variable, label, startTime, endTime) {
const params = createPublishTimingEventParams('PUBLISH_ANONYMOUS_CLAIM', startTime, endTime, ip, headers); const params = createPublishTimingEventParams(category, variable, label, startTime, endTime);
sendGoogleAnalyticsTiming(ip, params); sendGoogleAnalyticsTiming(siteName, params);
}, },
sendGAChannelPublishTiming (headers, ip, originalUrl, startTime, endTime) { chooseGaPublishLabel (publishParams) {
const params = createPublishTimingEventParams('PUBLISH_IN_CHANNEL_CLAIM', startTime, endTime, ip, headers); if (publishParams.channel_name || publishParams.channel_id) {
sendGoogleAnalyticsTiming(ip, params); return 'PUBLISH_IN_CHANNEL';
}
return 'PUBLISH_ANONYMOUS';
}, },
}; };

View file

@ -3,8 +3,9 @@ const logger = require('winston');
const config = require('../config/speechConfig.js'); const config = require('../config/speechConfig.js');
const { apiHost, apiPort } = config.api; const { apiHost, apiPort } = config.api;
const lbryApiUri = 'http://' + apiHost + ':' + apiPort; const lbryApiUri = 'http://' + apiHost + ':' + apiPort;
const { chooseGaPublishLabel, sendGATimingEvent } = require('./googleAnalytics.js');
function handleLbrynetResponse ({ data }, resolve, reject) { const handleLbrynetResponse = ({ data }, resolve, reject) => {
logger.debug('lbry api data:', data); logger.debug('lbry api data:', data);
if (data.result) { if (data.result) {
// check for an error // check for an error
@ -18,11 +19,12 @@ function handleLbrynetResponse ({ data }, resolve, reject) {
} }
// fallback in case it just timed out // fallback in case it just timed out
reject(JSON.stringify(data)); reject(JSON.stringify(data));
} };
module.exports = { module.exports = {
publishClaim (publishParams) { publishClaim (publishParams) {
logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`); logger.debug(`lbryApi >> Publishing claim to "${publishParams.name}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
@ -30,6 +32,7 @@ module.exports = {
params: publishParams, params: publishParams,
}) })
.then(response => { .then(response => {
sendGATimingEvent('lbrynet', 'publish', chooseGaPublishLabel(publishParams), gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject); handleLbrynetResponse(response, resolve, reject);
}) })
.catch(error => { .catch(error => {
@ -39,6 +42,7 @@ module.exports = {
}, },
getClaim (uri) { getClaim (uri) {
logger.debug(`lbryApi >> Getting Claim for "${uri}"`); logger.debug(`lbryApi >> Getting Claim for "${uri}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
@ -46,6 +50,7 @@ module.exports = {
params: { uri, timeout: 20 }, params: { uri, timeout: 20 },
}) })
.then(response => { .then(response => {
sendGATimingEvent('lbrynet', 'getClaim', 'GET', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject); handleLbrynetResponse(response, resolve, reject);
}) })
.catch(error => { .catch(error => {
@ -55,6 +60,7 @@ module.exports = {
}, },
getClaimList (claimName) { getClaimList (claimName) {
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`); logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
@ -62,6 +68,7 @@ module.exports = {
params: { name: claimName }, params: { name: claimName },
}) })
.then(response => { .then(response => {
sendGATimingEvent('lbrynet', 'getClaimList', 'CLAIM_LIST', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject); handleLbrynetResponse(response, resolve, reject);
}) })
.catch(error => { .catch(error => {
@ -71,6 +78,7 @@ module.exports = {
}, },
resolveUri (uri) { resolveUri (uri) {
logger.debug(`lbryApi >> Resolving URI for "${uri}"`); logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
@ -78,6 +86,7 @@ module.exports = {
params: { uri }, params: { uri },
}) })
.then(({ data }) => { .then(({ data }) => {
sendGATimingEvent('lbrynet', 'resolveUri', 'RESOLVE', gaStartTime, Date.now());
if (data.result[uri].error) { // check for errors if (data.result[uri].error) { // check for errors
reject(data.result[uri].error); reject(data.result[uri].error);
} else { // if no errors, resolve } else { // if no errors, resolve
@ -91,12 +100,14 @@ module.exports = {
}, },
getDownloadDirectory () { getDownloadDirectory () {
logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...'); logger.debug('lbryApi >> Retrieving the download directory path from lbry daemon...');
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
method: 'settings_get', method: 'settings_get',
}) })
.then(({ data }) => { .then(({ data }) => {
sendGATimingEvent('lbrynet', 'getDownloadDirectory', 'SETTINGS_GET', gaStartTime, Date.now());
if (data.result) { if (data.result) {
resolve(data.result.download_directory); resolve(data.result.download_directory);
} else { } else {
@ -110,6 +121,8 @@ module.exports = {
}); });
}, },
createChannel (name) { createChannel (name) {
logger.debug(`lbryApi >> Creating channel for ${name}...`);
const gaStartTime = Date.now();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post(lbryApiUri, { .post(lbryApiUri, {
@ -120,11 +133,10 @@ module.exports = {
}, },
}) })
.then(response => { .then(response => {
logger.verbose('createChannel response:', response); sendGATimingEvent('lbrynet', 'createChannel', 'CHANNEL_NEW', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject); handleLbrynetResponse(response, resolve, reject);
}) })
.catch(error => { .catch(error => {
logger.error('createChannel error:', error);
reject(error); reject(error);
}); });
}); });

View file

@ -7,7 +7,7 @@ const { claimNameIsAvailable, checkChannelAvailability, publish } = require('../
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js'); const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../helpers/publishHelpers.js'); const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../helpers/errorHandlers.js');
const { sendGAAnonymousPublishTiming, sendGAChannelPublishTiming } = require('../helpers/googleAnalytics.js'); const { sendGATimingEvent } = require('../helpers/googleAnalytics.js');
const { authenticateUser } = require('../auth/authentication.js'); const { authenticateUser } = require('../auth/authentication.js');
const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js'); const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js');
@ -131,9 +131,9 @@ module.exports = (app) => {
logger.debug('api/claim/publish req.body:', body); logger.debug('api/claim/publish req.body:', body);
logger.debug('api/claim/publish req.files:', files); logger.debug('api/claim/publish req.files:', files);
// define variables // define variables
let name, fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType, nsfw, license, title, description, thumbnail, channelName, channelId, channelPassword; let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
// record the start time of the request // record the start time of the request
const publishStartTime = Date.now(); gaStartTime = Date.now();
// validate the body and files of the request // validate the body and files of the request
try { try {
// validateApiPublishRequest(body, files); // validateApiPublishRequest(body, files);
@ -175,12 +175,7 @@ module.exports = (app) => {
}, },
}); });
// record the publish end time and send to google analytics // record the publish end time and send to google analytics
const publishEndTime = Date.now(); sendGATimingEvent('end-to-end', 'publish', 'publish endpoint', gaStartTime, Date.now());
if (channelName) {
sendGAChannelPublishTiming(headers, ip, originalUrl, publishStartTime, publishEndTime);
} else {
sendGAAnonymousPublishTiming(headers, ip, originalUrl, publishStartTime, publishEndTime);
}
}) })
.catch(error => { .catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res); errorHandlers.handleErrorResponse(originalUrl, ip, error, res);