295 - server-side GA events for tracking serve requests #351
3 changed files with 119 additions and 133 deletions
|
@ -5,11 +5,11 @@ const googleApiKey = config.analytics.googleId;
|
||||||
|
|
||||||
function createServeEventParams (headers, ip, originalUrl) {
|
function createServeEventParams (headers, ip, originalUrl) {
|
||||||
return {
|
return {
|
||||||
ec : 'serve',
|
eventCategory : 'client requests',
|
||||||
ea : originalUrl,
|
eventAction : 'serve request',
|
||||||
uip: ip,
|
eventLabel : originalUrl,
|
||||||
ua : headers['user-agent'],
|
ipOverride : ip,
|
||||||
ul : headers['accept-language'],
|
userAgentOverride: headers['user-agent'],
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,8 +21,7 @@ function createPublishTimingEventParams (label, startTime, endTime, ip, headers)
|
||||||
userTimingTime : durration,
|
userTimingTime : durration,
|
||||||
userTimingLabel : label,
|
userTimingLabel : label,
|
||||||
uip : ip,
|
uip : ip,
|
||||||
ua : headers['user-agent'],
|
userAgentOverride : headers['user-agent'],
|
||||||
ul : headers['accept-language'],
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,109 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const { getClaimId, getLocalFileRecord } = require('../controllers/serveController.js');
|
||||||
|
const { handleErrorResponse } = require('../helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
const SERVE = 'SERVE';
|
||||||
|
const SHOW = 'SHOW';
|
||||||
|
const NO_FILE = 'NO_FILE';
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
const NO_CLAIM = 'NO_CLAIM';
|
||||||
|
|
||||||
|
function clientAcceptsHtml ({accept}) {
|
||||||
|
return accept && accept.match(/text\/html/);
|
||||||
|
};
|
||||||
|
|
||||||
|
function requestIsFromBrowser (headers) {
|
||||||
|
return headers['user-agent'] && headers['user-agent'].match(/Mozilla/);
|
||||||
|
};
|
||||||
|
|
||||||
|
function clientWantsAsset ({accept, range}) {
|
||||||
|
const imageIsWanted = accept && accept.match(/image\/.*/) && !accept.match(/text\/html/) && !accept.match(/text\/\*/);
|
||||||
|
const videoIsWanted = accept && range;
|
||||||
|
return imageIsWanted || videoIsWanted;
|
||||||
|
};
|
||||||
|
|
||||||
|
function isValidClaimId (claimId) {
|
||||||
|
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
||||||
|
};
|
||||||
|
|
||||||
|
function isValidShortId (claimId) {
|
||||||
|
return claimId.length === 1; // it should really evaluate the short url itself
|
||||||
|
};
|
||||||
|
|
||||||
|
function isValidShortIdOrClaimId (input) {
|
||||||
|
return (isValidClaimId(input) || isValidShortId(input));
|
||||||
|
};
|
||||||
|
|
||||||
|
function serveAssetToClient (claimId, name, res) {
|
||||||
|
return getLocalFileRecord(claimId, name)
|
||||||
|
.then(fileRecord => {
|
||||||
|
// check that a local record was found
|
||||||
|
if (fileRecord === NO_FILE) {
|
||||||
|
return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
|
||||||
|
}
|
||||||
|
// serve the file
|
||||||
|
const {filePath, fileType} = fileRecord;
|
||||||
|
logger.verbose(`serving file: ${filePath}`);
|
||||||
|
const sendFileOptions = {
|
||||||
|
headers: {
|
||||||
|
'X-Content-Type-Options': 'nosniff',
|
||||||
|
'Content-Type' : fileType || 'image/jpeg',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res.status(200).sendFile(filePath, sendFileOptions);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
serveFile ({ filePath, fileType }, claimId, name, res) {
|
getClaimIdAndServeAsset (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) {
|
||||||
logger.verbose(`serving file: ${filePath}`);
|
// get the claim Id and then serve the asset
|
||||||
// set response options
|
getClaimId(channelName, channelClaimId, claimName, claimId)
|
||||||
const headerContentType = fileType || 'image/jpeg';
|
.then(fullClaimId => {
|
||||||
const options = {
|
if (fullClaimId === NO_CLAIM) {
|
||||||
headers: {
|
return res.status(404).json({success: false, message: 'no claim id could be found'});
|
||||||
'X-Content-Type-Options': 'nosniff',
|
} else if (fullClaimId === NO_CHANNEL) {
|
||||||
'Content-Type' : headerContentType,
|
return res.status(404).json({success: false, message: 'no channel id could be found'});
|
||||||
},
|
}
|
||||||
};
|
serveAssetToClient(fullClaimId, claimName, res);
|
||||||
// send the file
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'success');
|
||||||
res.status(200).sendFile(filePath, options);
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'fail');
|
||||||
|
});
|
||||||
},
|
},
|
||||||
showFile (claimInfo, shortId, res) {
|
determineResponseType (hasFileExtension, headers) {
|
||||||
logger.verbose(`showing claim: ${claimInfo.name}#${claimInfo.claimId}`);
|
let responseType;
|
||||||
res.status(200).render('index');
|
if (hasFileExtension) {
|
||||||
|
responseType = SERVE; // assume a serve request if file extension is present
|
||||||
|
if (clientAcceptsHtml(headers)) { // if the request comes from a browser, change it to a show request
|
||||||
|
responseType = SHOW;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
responseType = SHOW;
|
||||||
|
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) { // this is in case someone embeds a show url
|
||||||
|
logger.debug('Show request came from browser but wants an image/video. Changing response to serve...');
|
||||||
|
responseType = SERVE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return responseType;
|
||||||
},
|
},
|
||||||
showFileLite (claimInfo, shortId, res) {
|
flipClaimNameAndIdForBackwardsCompatibility (identifier, name) {
|
||||||
logger.verbose(`showlite claim: ${claimInfo.name}#${claimInfo.claimId}`);
|
// this is a patch for backwards compatability with '/name/claim_id' url format
|
||||||
res.status(200).render('index');
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
||||||
|
const tempName = name;
|
||||||
|
name = identifier;
|
||||||
|
identifier = tempName;
|
||||||
|
}
|
||||||
|
return [identifier, name];
|
||||||
|
},
|
||||||
|
logRequestData (responseType, claimName, channelName, claimId) {
|
||||||
|
logger.debug('responseType ===', responseType);
|
||||||
|
logger.debug('claim name === ', claimName);
|
||||||
|
logger.debug('channel name ===', channelName);
|
||||||
|
logger.debug('claim id ===', claimId);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,88 +1,9 @@
|
||||||
const logger = require('winston');
|
const { sendGAServeEvent } = require('../helpers/googleAnalytics');
|
||||||
const { getClaimId, getLocalFileRecord } = require('../controllers/serveController.js');
|
|
||||||
const serveHelpers = require('../helpers/serveHelpers.js');
|
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('../helpers/serveHelpers.js');
|
||||||
const { handleErrorResponse } = require('../helpers/errorHandlers.js');
|
|
||||||
const lbryUri = require('../helpers/lbryUri.js');
|
const lbryUri = require('../helpers/lbryUri.js');
|
||||||
|
|
||||||
const SERVE = 'SERVE';
|
const SERVE = 'SERVE';
|
||||||
const SHOW = 'SHOW';
|
|
||||||
const NO_CHANNEL = 'NO_CHANNEL';
|
|
||||||
const NO_CLAIM = 'NO_CLAIM';
|
|
||||||
const NO_FILE = 'NO_FILE';
|
|
||||||
|
|
||||||
function isValidClaimId (claimId) {
|
|
||||||
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidShortId (claimId) {
|
|
||||||
return claimId.length === 1; // it should really evaluate the short url itself
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidShortIdOrClaimId (input) {
|
|
||||||
return (isValidClaimId(input) || isValidShortId(input));
|
|
||||||
}
|
|
||||||
|
|
||||||
function clientAcceptsHtml ({accept}) {
|
|
||||||
return accept && accept.match(/text\/html/);
|
|
||||||
}
|
|
||||||
|
|
||||||
function requestIsFromBrowser (headers) {
|
|
||||||
return headers['user-agent'] && headers['user-agent'].match(/Mozilla/);
|
|
||||||
};
|
|
||||||
|
|
||||||
function clientWantsAsset ({accept, range}) {
|
|
||||||
const imageIsWanted = accept && accept.match(/image\/.*/) && !accept.match(/text\/html/) && !accept.match(/text\/\*/);
|
|
||||||
const videoIsWanted = accept && range;
|
|
||||||
return imageIsWanted || videoIsWanted;
|
|
||||||
}
|
|
||||||
|
|
||||||
function determineResponseType (hasFileExtension, headers) {
|
|
||||||
let responseType;
|
|
||||||
if (hasFileExtension) {
|
|
||||||
responseType = SERVE; // assume a serve request if file extension is present
|
|
||||||
if (clientAcceptsHtml(headers)) { // if the request comes from a browser, change it to a show request
|
|
||||||
responseType = SHOW;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
responseType = SHOW;
|
|
||||||
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) { // this is in case someone embeds a show url
|
|
||||||
logger.debug('Show request came from browser but wants an image/video. Changing response to serve...');
|
|
||||||
responseType = SERVE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return responseType;
|
|
||||||
}
|
|
||||||
|
|
||||||
function serveAssetToClient (claimId, name, res) {
|
|
||||||
return getLocalFileRecord(claimId, name)
|
|
||||||
.then(fileInfo => {
|
|
||||||
// logger.debug('fileInfo:', fileInfo);
|
|
||||||
if (fileInfo === NO_FILE) {
|
|
||||||
return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
|
|
||||||
}
|
|
||||||
return serveHelpers.serveFile(fileInfo, claimId, name, res);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function flipClaimNameAndIdForBackwardsCompatibility (identifier, name) {
|
|
||||||
// this is a patch for backwards compatability with '/name/claim_id' url format
|
|
||||||
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
|
||||||
const tempName = name;
|
|
||||||
name = identifier;
|
|
||||||
identifier = tempName;
|
|
||||||
}
|
|
||||||
return [identifier, name];
|
|
||||||
}
|
|
||||||
|
|
||||||
function logRequestData (responseType, claimName, channelName, claimId) {
|
|
||||||
logger.debug('responseType ===', responseType);
|
|
||||||
logger.debug('claim name === ', claimName);
|
|
||||||
logger.debug('channel name ===', channelName);
|
|
||||||
logger.debug('claim id ===', claimId);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
// route to serve a specific asset using the channel or claim id
|
// route to serve a specific asset using the channel or claim id
|
||||||
|
@ -98,6 +19,9 @@ module.exports = (app) => {
|
||||||
if (responseType !== SERVE) {
|
if (responseType !== SERVE) {
|
||||||
return res.status(200).render('index');
|
return res.status(200).render('index');
|
||||||
}
|
}
|
||||||
|
// handle serve request
|
||||||
|
// send google analytics
|
||||||
|
sendGAServeEvent(headers, ip, originalUrl);
|
||||||
// parse the claim
|
// parse the claim
|
||||||
let claimName;
|
let claimName;
|
||||||
try {
|
try {
|
||||||
|
@ -118,20 +42,7 @@ module.exports = (app) => {
|
||||||
// log the request data for debugging
|
// log the request data for debugging
|
||||||
logRequestData(responseType, claimName, channelName, claimId);
|
logRequestData(responseType, claimName, channelName, claimId);
|
||||||
// get the claim Id and then serve the asset
|
// get the claim Id and then serve the asset
|
||||||
getClaimId(channelName, channelClaimId, claimName, claimId)
|
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
||||||
.then(fullClaimId => {
|
|
||||||
if (fullClaimId === NO_CLAIM) {
|
|
||||||
return res.status(404).json({success: false, message: 'no claim id could be found'});
|
|
||||||
} else if (fullClaimId === NO_CHANNEL) {
|
|
||||||
return res.status(404).json({success: false, message: 'no channel id could be found'});
|
|
||||||
}
|
|
||||||
serveAssetToClient(fullClaimId, claimName, res);
|
|
||||||
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'success');
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'fail');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
// route to serve the winning asset at a claim or a channel page
|
// route to serve the winning asset at a claim or a channel page
|
||||||
app.get('/:claim', ({ headers, ip, originalUrl, params, query }, res) => {
|
app.get('/:claim', ({ headers, ip, originalUrl, params, query }, res) => {
|
||||||
|
@ -146,6 +57,9 @@ module.exports = (app) => {
|
||||||
if (responseType !== SERVE) {
|
if (responseType !== SERVE) {
|
||||||
return res.status(200).render('index');
|
return res.status(200).render('index');
|
||||||
}
|
}
|
||||||
|
// handle serve request
|
||||||
|
// send google analytics
|
||||||
|
sendGAServeEvent(headers, ip, originalUrl);
|
||||||
// parse the claim
|
// parse the claim
|
||||||
let claimName;
|
let claimName;
|
||||||
try {
|
try {
|
||||||
|
@ -156,17 +70,6 @@ module.exports = (app) => {
|
||||||
// log the request data for debugging
|
// log the request data for debugging
|
||||||
logRequestData(responseType, claimName, null, null);
|
logRequestData(responseType, claimName, null, null);
|
||||||
// get the claim Id and then serve the asset
|
// get the claim Id and then serve the asset
|
||||||
getClaimId(null, null, claimName, null)
|
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
||||||
.then(fullClaimId => {
|
|
||||||
if (fullClaimId === NO_CLAIM) {
|
|
||||||
return res.status(404).json({success: false, message: 'no claim id could be found'});
|
|
||||||
}
|
|
||||||
serveAssetToClient(fullClaimId, claimName, res);
|
|
||||||
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'success');
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
// postToStats(responseType, originalUrl, ip, claimName, fullClaimId, 'fail');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue