瀏覽代碼

Add Google Analytics in production

Gaël Métais 10 年之前
父節點
當前提交
e06d7f9798

+ 5 - 2
app/node_controllers/indexController.js

@@ -5,7 +5,7 @@
 var async   = require('async');
 var fs      = require ('fs');
 
-var indexController = function(req, res) {
+var indexController = function(req, res, googleAnalyticsId) {
     'use strict';
 
     async.parallel({
@@ -15,8 +15,11 @@ var indexController = function(req, res) {
         }
 
     }, function(err, results) {
+        var html = results.htmlTemplate;
+        html = html.replace('%%GA_ID%%', googleAnalyticsId);
+
         res.setHeader('Content-Type', 'text/html');
-        res.send(results.htmlTemplate);
+        res.send(html);
     });
  };
 

+ 2 - 1
app/node_controllers/launchTestController.js

@@ -5,7 +5,7 @@
 var async   = require('async');
 var fs      = require ('fs');
 
-var launchTestController = function(req, res, testQueue) {
+var launchTestController = function(req, res, testQueue, googleAnalyticsId) {
     'use strict';
 
     // Generate test id
@@ -34,6 +34,7 @@ var launchTestController = function(req, res, testQueue) {
 
             html = html.replace('%%TEST_URL%%', url);
             html = html.replace('%%TEST_ID%%', testId);
+            html = html.replace('%%GA_ID%%', googleAnalyticsId);
 
             res.setHeader('Content-Type', 'text/html');
             res.send(html);

+ 2 - 1
app/node_controllers/resultsController.js

@@ -5,7 +5,7 @@
 var async   = require('async');
 var fs      = require ('fs');
 
-var resultsController = function(req, res) {
+var resultsController = function(req, res, googleAnalyticsId) {
     'use strict';
 
     var testId = req.params.testId;
@@ -37,6 +37,7 @@ var resultsController = function(req, res) {
         var html = results.htmlTemplate;
         html = html.replace('%%METADATA%%', results.phantomasMetadata);
         html = html.replace('%%RESULTS%%', results.phantomasResults);
+        html = html.replace('%%GA_ID%%', googleAnalyticsId);
 
         res.setHeader('Content-Type', 'text/html');
         res.send(html);

+ 13 - 0
app/node_views/index.html

@@ -17,5 +17,18 @@
     <div class="footer">
         <p><b>Yellow Lab Tools</b> is an open source project by <a href="http://www.gaelmetais.com" target="_blank">Gaël Métais</a>, based on <a href="https://github.com/macbre/phantomas" target="_blank">Phantomas</a>.<br>If you like it, <a href="https://github.com/gmetais/YellowLabTools" target="_blank" class="star">give it a <span>&#9733;</span> on GitHub</a>!</p>
     </div>
+
+    <script>
+        var GA_ID = '%%GA_ID%%';
+        if (GA_ID.length > 0 && window.location.host === 'yellowlab.tools') {
+            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+            ga('create', GA_ID, 'auto');
+            ga('send', 'pageview');
+        }
+    </script>
 </body>
 </html>

+ 24 - 1
app/node_views/launchTest.html

@@ -1,7 +1,7 @@
 <html>
 <head>
     <meta charset="utf-8"> 
-    <title>Javascript Spaghetti Profiler</title>
+    <title>Yellow Lab Tools - Awaiting</title>
     <link rel="stylesheet" type="text/css" href="/public/styles/main.css">
     <link rel="stylesheet" type="text/css" href="/public/styles/launchTest.css">
     <script src="/socket.io/socket.io.js"></script>
@@ -32,6 +32,7 @@
             } else {
                 statusElement.innerHTML = 'Waiting behind ' + (position) + ' other tests';
             }
+            sendPositionInQueueToGA(position);
         });
 
         socket.on('complete', function() {
@@ -49,6 +50,28 @@
         });
 
         askStatus();
+
+
+        var GA_ID = '%%GA_ID%%';
+        if (GA_ID.length > 0 && window.location.host === 'yellowlab.tools') {
+            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+            ga('create', GA_ID, 'auto');
+        }
+
+        // Need stats on average queue position
+        var positionAlreadySent = false;
+        function sendPositionInQueueToGA(position) {
+            if (!positionAlreadySent && GA_ID.length > 0 && window.location.host === 'yellowlab.tools') {
+                ga('send', 'pageview', {
+                    queuePosition: position
+                });
+                positionAlreadySent = true;
+            }
+        }
     </script>
 </body>
 </html>

+ 11 - 1
app/node_views/results.html

@@ -1,7 +1,7 @@
 <html>
 <head>
     <meta charset="utf-8"> 
-    <title>Javascript Spaghetti Profiler</title>
+    <title>Yellow Lab Tools - Results page</title>
     <link rel="stylesheet" type="text/css" href="/public/styles/main.css">
     <link rel="stylesheet" type="text/css" href="/public/styles/results.css">
     <script src="/bower_components/angular/angular.min.js"></script>
@@ -275,6 +275,16 @@
 
         var _phantomas_metadata = %%METADATA%%;
 
+        var GA_ID = '%%GA_ID%%';
+        if (GA_ID.length > 0 && window.location.host === 'yellowlab.tools') {
+            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+
+            ga('create', GA_ID, 'auto');
+            ga('send', 'pageview');
+        }
     </script>
 </body>
 </html>

+ 3 - 3
server.js

@@ -32,9 +32,9 @@ app.all('*', function(req, res, next) {
 });
 
 // Routes definition
-app.get('/',                    indexController);
-app.post('/launchTest',         function(req, res) { launchTestController(req, res, testQueue); });
-app.get('/results/:testId',     resultsController);
+app.get('/',                    function(req, res) { indexController(req, res, settings.googleAnalyticsId); });
+app.post('/launchTest',         function(req, res) { launchTestController(req, res, testQueue, settings.googleAnalyticsId); });
+app.get('/results/:testId',     function(req, res) { resultsController(req, res, settings.googleAnalyticsId); });
 
 
 // Static files

+ 1 - 1
server_config/server_install.sh

@@ -2,7 +2,7 @@
 
 # APT-GET
 sudo apt-get update
-sudo apt-get install lsb-release -y --force-yes
+sudo apt-get install lsb-release libfontconfig1 libfreetype6 -y --force-yes
 sudo apt-get install curl git python-software-properties -y --force-yes
 
 # Installation of NodeJS

+ 2 - 1
server_config/settings-prod.json

@@ -1,3 +1,4 @@
 {
-    "serverPort": 80
+    "serverPort": 80,
+    "googleAnalyticsId": "UA-54493828-1"
 }

+ 2 - 1
server_config/settings.json

@@ -1,3 +1,4 @@
 {
-    "serverPort": 8383
+    "serverPort": 8383,
+    "googleAnalyticsId": ""
 }