diff --git a/helpers/googleAnalytics.js b/helpers/googleAnalytics.js new file mode 100644 index 00000000..c2d6d7ce --- /dev/null +++ b/helpers/googleAnalytics.js @@ -0,0 +1,63 @@ +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 { + ec : 'serve', + ea : originalUrl, + uip: ip, + ua : headers['user-agent'], + ul : headers['accept-language'], + }; +}; + +function createPublishTimingEventParams (label, startTime, endTime, ip, headers) { + const durration = endTime - startTime; + return { + userTimingCategory : 'lbrynet', + userTimingVariableName: 'publish', + userTimingTime : durration, + userTimingLabel : label, + uip : ip, + ua : headers['user-agent'], + ul : headers['accept-language'], + }; +}; + +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) { + const params = createPublishTimingEventParams('anonymous', startTime, endTime, ip, headers); + sendGoogleAnalyticsTiming(ip, params); + }, + sendGAChannelPublishTiming (headers, ip, originalUrl, startTime, endTime) { + const params = createPublishTimingEventParams('anonymous', startTime, endTime, ip, headers); + sendGoogleAnalyticsTiming(ip, params); + }, +}; diff --git a/helpers/publishHelpers.js b/helpers/publishHelpers.js index 5d315a49..a9cbf2bd 100644 --- a/helpers/publishHelpers.js +++ b/helpers/publishHelpers.js @@ -1,4 +1,3 @@ -const constants = require('../constants'); const logger = require('winston'); const fs = require('fs'); const { site, wallet } = require('../config/speechConfig.js'); @@ -177,11 +176,4 @@ module.exports = { nsfw, }; }, - returnPublishTimingActionType (channelName) { - if (channelName) { - return constants.PUBLISH_IN_CHANNEL_CLAIM; - } else { - return constants.PUBLISH_ANONYMOUS_CLAIM; - } - }, }; diff --git a/helpers/statsHelpers.js b/helpers/statsHelpers.js index f37e7df1..9e471172 100644 --- a/helpers/statsHelpers.js +++ b/helpers/statsHelpers.js @@ -1,22 +1,7 @@ -const constants = require('../constants'); const logger = require('winston'); -const ua = require('universal-analytics'); -const config = require('../config/speechConfig.js'); -const googleApiKey = config.analytics.googleId; const db = require('../models'); module.exports = { - createPublishTimingEventParams (publishDurration, ip, headers, label) { - return { - userTimingCategory : 'lbrynet', - userTimingVariableName: 'publish', - userTimingTime : publishDurration, - userTimingLabel : label, - uip : ip, - ua : headers['user-agent'], - ul : headers['accept-language'], - }; - }, postToStats (action, url, ipAddress, name, claimId, result) { logger.debug('action:', action); // make sure the result is a string @@ -50,46 +35,4 @@ module.exports = { logger.error('Sequelize error >>', error); }); }, - sendGoogleAnalyticsEvent (action, headers, ip, originalUrl) { - const visitorId = ip.replace(/\./g, '-'); - const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); - let params; - switch (action) { - case 'SERVE': - params = { - ec : 'serve', - 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); - } - }); - }, - sendGoogleAnalyticsTiming (action, headers, ip, originalUrl, startTime, endTime) { - const visitorId = ip.replace(/\./g, '-'); - const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true }); - const durration = endTime - startTime; - let params; - switch (action) { - case constants.PUBLISH_ANONYMOUS_CLAIM: - case constants.PUBLISH_IN_CHANNEL_CLAIM: - logger.verbose(`${action} completed successfully in ${durration}ms`); - params = module.exports.createPublishTimingEventParams(durration, ip, headers, action); - break; - default: break; - } - visitor.timing(params, (err) => { - if (err) { - logger.error('Google Analytics Event Error >>', err); - } - logger.debug(`${action} timing event successfully sent to google analytics`); - }); - }, }; diff --git a/routes/api-routes.js b/routes/api-routes.js index 21e5b656..9178834d 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -5,9 +5,9 @@ const multipartMiddleware = multipart({uploadDir: files.uploadDirectory}); const db = require('../models'); const { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.js'); const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js'); -const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel, addGetResultsToFileData, createFileData, returnPublishTimingActionType } = require('../helpers/publishHelpers.js'); +const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel, addGetResultsToFileData, createFileData } = require('../helpers/publishHelpers.js'); const errorHandlers = require('../helpers/errorHandlers.js'); -const { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.js'); +const { sendGAAnonymousPublishTiming, sendGAChannelPublishTiming } = require('../helpers/googleAnalytics.js'); const { authenticateIfNoUserToken } = require('../auth/authentication.js'); const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js'); @@ -134,12 +134,10 @@ module.exports = (app) => { app.post('/api/claim/publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => { logger.debug('api/claim-publish body:', body); logger.debug('api/claim-publish files:', files); - // record the start time of the request and create variable for storing the action type - const publishStartTime = Date.now(); - logger.debug('publish request started @', publishStartTime); - let timingActionType; // define variables let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword; + // record the start time of the request + const publishStartTime = Date.now(); // validate the body and files of the request try { // validateApiPublishRequest(body, files); @@ -166,8 +164,6 @@ module.exports = (app) => { return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName); }) .then(publishParams => { - // set the timing event type for reporting - timingActionType = returnPublishTimingActionType(publishParams.channel_name); // publish the asset return publish(publishParams, fileName, fileType); }) @@ -182,10 +178,13 @@ module.exports = (app) => { lbryTx : result, }, }); - // log the publish end time + // record the publish end time and send to google analytics const publishEndTime = Date.now(); - logger.debug('publish request completed @', publishEndTime); - sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime); + if (channelName) { + sendGAChannelPublishTiming(headers, ip, originalUrl, publishStartTime, publishEndTime); + } else { + sendGAAnonymousPublishTiming(headers, ip, originalUrl, publishStartTime, publishEndTime); + } }) .catch(error => { errorHandlers.handleErrorResponse(originalUrl, ip, error, res);