2016-06-07 00:37:09 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Create the SendVisitorData service
|
|
|
|
angular
|
|
|
|
.module('forms')
|
|
|
|
.factory('SendVisitorData', SendVisitorData);
|
|
|
|
|
2016-06-17 21:33:33 +00:00
|
|
|
SendVisitorData.$inject = ['Socket', '$state', '$http', 'deviceDetector'];
|
2016-06-07 00:37:09 +00:00
|
|
|
|
2016-06-17 21:33:33 +00:00
|
|
|
function SendVisitorData(Socket, $state, $http) {
|
2016-06-07 00:37:09 +00:00
|
|
|
|
|
|
|
// Create a controller method for sending visitor data
|
2016-06-17 21:33:33 +00:00
|
|
|
function send(form, lastActiveIndex, timeElapsed, deviceDetector) {
|
2016-06-07 00:37:09 +00:00
|
|
|
// Create a new message object
|
|
|
|
var visitorData = {
|
|
|
|
referrer: document.referrer,
|
|
|
|
isSubmitted: form.submitted,
|
|
|
|
formId: form._id,
|
|
|
|
lastActiveField: form.form_fields[lastActiveIndex]._id,
|
2016-06-17 21:33:33 +00:00
|
|
|
timeElapsed: timeElapsed,
|
|
|
|
//@TODO @FIXME: David: Need to make this get the language from the HTTP Header instead
|
|
|
|
language: window.navigator.userLanguage || window.navigator.language
|
2016-06-07 00:37:09 +00:00
|
|
|
};
|
2016-06-17 21:33:33 +00:00
|
|
|
|
|
|
|
$http.get('http://jsonip.com/').success(function(response) {
|
|
|
|
visitorData.ipAddr = response['ip']+'';
|
|
|
|
}).error(function(error) {
|
|
|
|
console.error('Could not get users\'s ip');
|
|
|
|
visitorData.ipAddr = '';
|
|
|
|
}).finally(function(){
|
|
|
|
visitorData.userAgent = deviceDetector.raw;
|
|
|
|
|
|
|
|
if(deviceDetector.isTablet()) {
|
|
|
|
visitorData.deviceType = 'tablet';
|
|
|
|
}else if(deviceDetector.isMobile()){
|
|
|
|
visitorData.deviceType = 'phone';
|
|
|
|
}else {
|
|
|
|
visitorData.deviceType = 'desktop';
|
|
|
|
}
|
|
|
|
Socket.emit('form-visitor-data', visitorData);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function init(){
|
|
|
|
// Make sure the Socket is connected
|
|
|
|
if (!Socket.socket) {
|
|
|
|
Socket.connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var service = {
|
|
|
|
send: send
|
|
|
|
};
|
2016-06-07 02:53:47 +00:00
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
init();
|
|
|
|
return service;
|
|
|
|
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|