-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.js
183 lines (155 loc) · 5.31 KB
/
analytics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* FormCorp Analytics
* @author Alex Berriman <[email protected]>
* @website www.formcorp.com.au
*/
/*global jQuery,fc*/
if (!Date.now) {
Date.now = function () {
"use strict";
return new Date().getTime();
};
}
var fcAnalytics = (function ($) {
"use strict";
// If formcorp not initialised, return empty
if (!fc) {
return {};
}
/**
* Tje URL of the Analytics javaqscript file
* @type {string}
*/
var registerAnalyticsEventListeners = function () {
// Text value focused
$(fc.jQueryContainer).on('focus', '.fc-fieldinput', function () {
var dataId = $(this).attr('formcorp-data-id'),
params = {
dataId: dataId
},
initParams = {};
fcAnalytics.logEvent(fc.eventTypes.onFocus, params);
// Mark field as having been initialised
initParams = {
dataId: dataId
};
// Log the hesitation time
if (fc.lastCompletedTimestamp > 0) {
initParams.hesitationTime = (Date.now() - fc.lastCompletedTimestamp) / 1000;
fc.lastHesitationTime = initParams.hesitationTime;
}
fcAnalytics.logEvent(fc.eventTypes.onFieldInit, initParams);
});
// Text value focused
$(fc.jQueryContainer).on('blur', '.fc-fieldinput', function () {
var dataId = $(this).attr('formcorp-data-id'),
params = {
dataId: dataId
};
fcAnalytics.logEvent(fc.eventTypes.onBlur, params);
});
// Mouse down event
$(fc.jQueryContainer).on('mousedown', function (e) {
var x = parseInt(e.pageX - fc.formPosition.left, 10),
y = parseInt(e.pageY - fc.formPosition.top, 10);
fcAnalytics.logEvent(fc.eventTypes.onMouseDown, {
x: x,
y: y
});
});
},
/**
* Process the event queue
*/
processEventQueue = function () {
// If the event queue isn't running, default it to false
if (fcAnalytics.eventQueueRunning === undefined) {
fcAnalytics.eventQueueRunning = false;
}
// If already running, do nothing
if (fcAnalytics.eventQueueRunning) {
console.log('[FC] The event queue is already running (slow server?)');
return;
}
// If no events, do nothing
if (fcAnalytics.events.length === 0) {
return;
}
// Mark the event queue as running, move events to the queue
fcAnalytics.eventQueueRunning = true;
fcAnalytics.queuedEvents = fcAnalytics.events;
fcAnalytics.events = [];
// Format the data to send with the request
var data = {
events: fcAnalytics.queuedEvents
};
// Fire off the API call
fc.api('analytics/log', data, 'post', function (data) {
// There was an error processing the update, move the queued events back in to the queue
if (typeof data !== 'object' || typeof data.success !== 'boolean' || !data.success) {
console.log('[FC] Error processing the analytics queue');
var queue = fcAnalytics.queuedEvents.concat(fcAnalytics.events);
fcAnalytics.events = queue;
}
// Reset the queue
fcAnalytics.queuedEvents = [];
fcAnalytics.eventQueueRunning = false;
});
},
/**
* Initialise the event queue
*/
initEventQueue = function () {
// Send events off to the server
setInterval(function () {
if (fc.expired === true) {
return;
}
processEventQueue();
}, fc.config.eventQueueInterval);
};
/**
* Return class methods
*/
return {
/**
* Analytics class constants
*/
constants: {
loaded: 'fc.analytics.loaded'
},
/**
* Initialise the analytics
*/
init: function () {
this.events = [];
this.eventQueueRunning = false;
registerAnalyticsEventListeners();
initEventQueue();
},
/**
* Store an event locally to be logged
* @param event
* @param params
*/
logEvent: function (event, params) {
if (event === undefined) {
return;
}
// Default params
if (params === undefined) {
params = {};
}
var eventObject = {
'event': event,
'params': params,
'time': (new Date()).getTime()
};
fcAnalytics.events.push(eventObject);
}
};
}(jQuery));
(function ($) {
'use strict';
$(fc.jQueryContainer).trigger(fc.jsEvents.onAnalyticsLoaded);
}(jQuery));