-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathplay.js
58 lines (51 loc) · 1.45 KB
/
play.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
/**
* Track Initial Play Event
* This event is triggered when the video has been played for the first time.
* If you are looking to track play events, simply listen on the player for a normal
* "play" or "playing" event.
*
* Example Usage:
* player.on('tracking:firstplay', (e, data) => console.log(data))
*
* Data Attributes:
* => secondsToLoad: Total number of seconds between the player initializing
* a play request and when the first frame begins.
*
* @function PlayTracking
* @param {Object} [config={}]
* An object of config left to the plugin author to define.
*/
const PlayTracking = function(config) {
let hasBeenTriggered = false;
let loadstart = 0;
let loadend = 0;
let secondsToLoad = 0;
const reset = function() {
hasBeenTriggered = false;
loadstart = 0;
loadend = 0;
secondsToLoad = 0;
};
const onLoadStart = function() {
reset();
loadstart = new Date();
};
const onLoadedData = function() {
loadend = new Date();
secondsToLoad = ((loadend - loadstart) / 1000);
};
const onPlaying = () => {
if (!hasBeenTriggered) {
hasBeenTriggered = true;
this.trigger('tracking:firstplay', {
secondsToLoad: +(secondsToLoad.toFixed(3))
});
}
};
this.on('ended', reset);
this.on('dispose', reset);
this.on('loadstart', onLoadStart);
this.on('loadeddata', onLoadedData);
this.on('playing', onPlaying);
};
export default PlayTracking;