-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read2MeAnalyticsBackendWrapper.js
74 lines (54 loc) · 2.47 KB
/
Read2MeAnalyticsBackendWrapper.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
import Read2MeBackendWrapper from "./Read2MeBackendWrapper";
export default class Read2MeAnalyticsBackendWrapper {
static getAnalytics(audioId, successCallback, errorCallback) {
let requestUri =
Read2MeBackendWrapper.getBaseUrl() + 'analytics/' + audioId;
const request = new XMLHttpRequest();
request.open('GET', requestUri, true);
request.onload = function() {
const response = JSON.parse(request.responseText);
if (request.status >= 200 && request.status < 400) {
successCallback(response);
} else {
errorCallback(response);
}
};
request.onerror = function() {
errorCallback();
console.warn('Connection to Read2Me API failed.');
};
request.send();
}
static sendAnalytics(audioId, currentPlaybackTime, audioDuration, listeningSessionId) {
const interval = 15;
currentPlaybackTime = Math.round(currentPlaybackTime);
audioDuration = Math.round(audioDuration);
if (Read2MeAnalyticsBackendWrapper._logContains(listeningSessionId, currentPlaybackTime))
return;
if (currentPlaybackTime % interval !== 0 && currentPlaybackTime !== audioDuration)
return;
let requestUri = Read2MeBackendWrapper.getBaseUrl() + 'analytics/' + audioId + '?' +
'pt=' + currentPlaybackTime +
'&lsid=' + listeningSessionId;
const request = new XMLHttpRequest();
request.open('POST', requestUri, true);
request.send();
Read2MeAnalyticsBackendWrapper._addToLog(listeningSessionId, currentPlaybackTime);
}
static _addToLog(listeningSessionId, currentPlaybackTime) {
if (!Read2MeAnalyticsBackendWrapper._log)
Read2MeAnalyticsBackendWrapper._log = {};
if (!Read2MeAnalyticsBackendWrapper._log[listeningSessionId])
Read2MeAnalyticsBackendWrapper._log[listeningSessionId] = [];
Read2MeAnalyticsBackendWrapper._log[listeningSessionId].push(currentPlaybackTime);
}
static _logContains(listeningSessionId, currentPlaybackTime) {
if (!Read2MeAnalyticsBackendWrapper._log)
return false;
if (!Read2MeAnalyticsBackendWrapper._log[listeningSessionId])
return false;
if (Read2MeAnalyticsBackendWrapper._log[listeningSessionId].indexOf(currentPlaybackTime) === -1)
return false;
return true;
}
}