From 6a4dcb08742403c7a4efdaf0351d096696e46c70 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Sat, 7 Oct 2017 01:23:42 -0700 Subject: [PATCH] moved ip lookups to backend --- app/sockets/analytics_service.js | 28 ++++++++++++++----- package.json | 1 + .../submit-form.client.directive.js | 10 +++++++ .../analytics-service.client.directive.js | 19 ++----------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/sockets/analytics_service.js b/app/sockets/analytics_service.js index bb5f8ab2..477363f3 100644 --- a/app/sockets/analytics_service.js +++ b/app/sockets/analytics_service.js @@ -5,7 +5,8 @@ */ var mongoose = require('mongoose'), errorHandler = require('../controllers/errors.server.controller'), - Form = mongoose.model('Form'); + Form = mongoose.model('Form'), + request = require('request'); // Create the chat configuration module.exports = function (io, socket) { @@ -50,12 +51,25 @@ module.exports = function (io, socket) { visitorsData[current_socket.id] = data; visitorsData[current_socket.id].socketId = current_socket.id; visitorsData[current_socket.id].isSaved = false; - if (data.isSubmitted && !data.isSaved) { - visitorsData[current_socket.id].isSaved = true; - saveVisitorData(data, function() { - current_socket.disconnect(true); - }); - } + + visitorsData[current_socket.id].ipAddr = current_socket.conn.transport.socket._socket.remoteAddress; + + console.log(current_socket); + request('https://freegeoip.net/json/'+current_socket.conn.transport.socket._socket.remoteAddress, (error, response, body)=> { + console.log(body); + var geoData = body; + visitorsData[current_socket.id].geoLocation = { + city: geoData.city, + country: geoData.country_name + } + + if (data.isSubmitted && !data.isSaved) { + visitorsData[current_socket.id].isSaved = true; + saveVisitorData(data, function() { + current_socket.disconnect(true); + }); + } + }); }); current_socket.on('disconnect', function() { diff --git a/package.json b/package.json index 0dda7145..0a10e435 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,7 @@ "prerender-node": "^2.2.1", "random-js": "^1.0.8", "raven": "^0.9.0", + "request": "^2.83.0", "socket.io": "^1.4.6", "socket.io-redis": "^1.0.0", "swig": "~1.4.1", diff --git a/public/form_modules/forms/base/directives/submit-form.client.directive.js b/public/form_modules/forms/base/directives/submit-form.client.directive.js index e0fbf483..3559209f 100644 --- a/public/form_modules/forms/base/directives/submit-form.client.directive.js +++ b/public/form_modules/forms/base/directives/submit-form.client.directive.js @@ -196,6 +196,16 @@ angular.module('view-form').directive('submitFormDirective', ['$http', 'TimeCoun } }; + $scope.$watch('selected.index', function(oldValue, newValue){ + if(oldValue !== newValue && newValue < $scope.myform.form_fields.length){ + //Only send analytics data if form has not been submitted + if(!$scope.myform.submitted){ + console.log('SendVisitorData.send()'); + SendVisitorData.send($scope.myform, newValue, TimeCounter.getTimeElapsed()); + } + } + }); + //Fire event when window is scrolled $window.onscroll = function(){ if(!NOSCROLL){ diff --git a/public/form_modules/forms/directives/analytics-service.client.directive.js b/public/form_modules/forms/directives/analytics-service.client.directive.js index ed233746..c40aad44 100644 --- a/public/form_modules/forms/directives/analytics-service.client.directive.js +++ b/public/form_modules/forms/directives/analytics-service.client.directive.js @@ -28,18 +28,6 @@ deviceType = 'desktop'; } - $.ajaxSetup( { 'async': false } ); - var geoData = $.getJSON('https://freegeoip.net/json/').responseJSON; - $.ajaxSetup( { 'async': true } ); - - if(!geoData){ - geoData = { - ip: '', - city: '', - country_name: '' - }; - } - // Create a new message object var visitorData = { referrer: document.referrer, @@ -49,11 +37,8 @@ timeElapsed: timeElapsed, language: lang, deviceType: deviceType, - ipAddr: geoData.ip, - geoLocation: { - city: geoData.city, - country: geoData.country_name - } + ipAddr: null, + geoLocation: null }; Socket.emit('form-visitor-data', visitorData);