From 80e33e0ae0e5441e3f478bdbbcb4a9803df6336a Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 12 Jun 2017 19:09:39 -0700 Subject: [PATCH] added analytics to client side --- helpers/socketHelpers.js | 6 ++++++ routes/api-routes.js | 1 - routes/html-routes.js | 15 +++++++++------ routes/sockets-routes.js | 17 ++++++++++++----- server.js | 11 +++-------- views/layouts/main.handlebars | 19 ++++++++++++++----- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/helpers/socketHelpers.js b/helpers/socketHelpers.js index 3da2381e..acb07410 100644 --- a/helpers/socketHelpers.js +++ b/helpers/socketHelpers.js @@ -1,8 +1,11 @@ var fs = require('fs'); var lbryApi = require('../helpers/lbryApi.js'); var config = require('config'); +var ua = require('universal-analytics'); var walledAddress = config.get('WalletConfig.lbryAddress'); +var googleAnalyticsId = config.get('AnalyticsConfig.googleId'); +var visitor = ua(googleAnalyticsId, {https: true}); function handlePublishError(error) { if (error.code === "ECONNREFUSED"){ @@ -44,16 +47,19 @@ module.exports = { publish: function(name, filepath, license, nsfw, socket) { // update the client socket.emit("publish-status", "Your image is being published (this might take a second)..."); + visitor.event("Publish", "Publish Request", name, {filepath, license, nsfw}).send(); // create the publish object var publishParams = createPublishParams(name, filepath, license, nsfw); // get a promise to publish lbryApi.publishClaim(publishParams) .then(function(data){ + visitor.event("Publish", "Publish Success", name, {filepath, license, nsfw}).send(); console.log("publish promise success. Tx info:", data) socket.emit("publish-complete", {name: name, result: data.result}); deleteTemporaryFile(filepath); }) .catch(function(error){ + visitor.event("Publish", "Publish Failure", name, {filepath, license, nsfw}).send(); console.log("error:", error); socket.emit("publish-failure", handlePublishError(error)); deleteTemporaryFile(filepath); diff --git a/routes/api-routes.js b/routes/api-routes.js index 235caa86..51e73142 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -1,6 +1,5 @@ var path = require('path'); var axios = require('axios'); - var lbryApi = require('../helpers/lbryApi.js'); module.exports = function(app){ diff --git a/routes/html-routes.js b/routes/html-routes.js index 74b9fca6..2f321052 100644 --- a/routes/html-routes.js +++ b/routes/html-routes.js @@ -1,12 +1,17 @@ var path = require('path'); var routeHelpers = require('../helpers/routeHelpers.js'); var lbryApi = require('../helpers/lbryApi.js'); +var ua = require('universal-analytics'); +var config = require('config'); -module.exports = function(app, visitor){ +var googleAnalyticsId = config.get('AnalyticsConfig.googleId'); +var visitor = ua(googleAnalyticsId, {https: true}); + +module.exports = function(app){ // route to fetch all free public claims app.get("/:name/all", function(req, res){ console.log(">> GET request on /" + req.params.name + " (all)"); - visitor.pageview(req.url).send(); + visitor.event("Claim Request", "Name/all", req.params.name + "/all").send(); // create promise lbryApi.getAllClaims(req.params.name) .then(function(orderedFreePublicClaims){ @@ -21,7 +26,7 @@ module.exports = function(app, visitor){ }); // route to fetch one free public claim app.get("/:name/:claim_id", function(req, res){ - + visitor.event("Claim Request", "Name/ClaimId", req.params.name + "/" + req.params.claim_id).send(); var uri = req.params.name + "#" + req.params.claim_id; console.log(">> GET request on /" + uri); // create promise @@ -37,7 +42,7 @@ module.exports = function(app, visitor){ }); // route to fetch one free public claim app.get("/:name", function(req, res){ - + visitor.event("Claim Request", "Name", req.params.name).send(); console.log(">> GET request on /" + req.params.name); // create promise lbryApi.getClaimBasedOnNameOnly(req.params.name) @@ -51,12 +56,10 @@ module.exports = function(app, visitor){ }); // route for the home page app.get("/", function(req, res){ - visitor.pageview(req.url).send(); res.status(200).render('index'); }); // a catch-all route if someone visits a page that does not exist app.use("*", function(req, res){ - visitor.pageview(req.url).send(); res.status(404).render('fourOhFour'); }); } diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index 8c58a01c..517a14c5 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -1,12 +1,18 @@ +var path = require('path'); +var siofu = require("socketio-file-upload"); +var socketHelpers = require('../helpers/socketHelpers.js'); +var ua = require('universal-analytics'); +var config = require('config'); + +var googleAnalyticsId = config.get('AnalyticsConfig.googleId'); +var visitor = ua(googleAnalyticsId, {https: true}); + module.exports = function(app) { var http = require('http').Server(app); var io = require('socket.io')(http); - var path = require('path'); - var siofu = require("socketio-file-upload"); - var socketHelpers = require('../helpers/socketHelpers.js'); - + io.on('connection', function(socket){ - console.log('a user connected'); + console.log('a user connected via sockets'); // attach upload listeners var uploader = new siofu(); uploader.dir = path.join(__dirname, '../../Uploads'); @@ -17,6 +23,7 @@ module.exports = function(app) { }) uploader.on("saved", function(event){ console.log("saved " + event.file.name); + visitor.event("Publish", "Publish Request", event.file.name).send(); if (event.file.success){ socket.emit("publish-status", "file upload successfully completed"); socketHelpers.publish(event.file.meta.name, event.file.pathName, event.file.meta.license, event.file.meta.nsfw, socket) diff --git a/server.js b/server.js index f39b9701..63601032 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,6 @@ var bodyParser = require('body-parser'); var path = require('path'); var siofu = require("socketio-file-upload"); var expressHandlebars = require("express-handlebars"); -var ua = require('universal-analytics'); -var config = require('config'); - -var googleAnalyticsId = config.get('AnalyticsConfig.googleId'); -var visitor = ua(googleAnalyticsId, {https: true}); // set port var PORT = 3000; @@ -29,11 +24,11 @@ app.engine('handlebars', expressHandlebars({defaultLayout: 'main'})); app.set('view engine', 'handlebars'); // require express routes -require("./routes/api-routes.js")(app, visitor); -require("./routes/html-routes.js")(app, visitor); +require("./routes/api-routes.js")(app); +require("./routes/html-routes.js")(app); // wrap the server in socket.io to intercept incoming sockets requests -var http = require("./routes/sockets-routes.js")(app, visitor); +var http = require("./routes/sockets-routes.js")(app); // start server http.listen(PORT, function() { diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index 553d6427..ee5626f9 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -6,16 +6,25 @@ Spee.ch - - - - - + + + + + {{{ body }}} + +