-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.video.ts
60 lines (55 loc) · 1.46 KB
/
plugin.video.ts
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
/**
* Plugin to Detect video play, progress, and complete events as visitors view embedded videos on your site.
* Scope:
* - Video state
* - Video percentage
* - Video mute
* - etc...
* @internal
* @extends PluginBuilder
*/
import { BaseOptions, BindResult } from '../../types'
import { PluginBuilder } from '../core'
export default class VideoPlugin extends PluginBuilder {
override key: string = 'video'
constructor() {
super()
}
override bind(_: BaseOptions): BindResult[] {
return [
{
name: 'video',
target: document.getElementsByTagName('video'),
event: 'load',
callback: (event) => {
return this.captureEvent(event)
}
}
]
}
/**
* A function to capture the video events on your site.
*/
private captureEvent(event: Event): Record<string, any> {
const video = event.target as HTMLVideoElement
return {
id: video.id,
state: video.paused ? 'pause' : 'play',
duration: video.duration,
currentTime: video.currentTime,
percent: video.currentTime / video.duration,
volume: video.volume,
muted: video.muted,
playbackRate: video.playbackRate,
buffered: video.buffered,
ended: video.ended,
paused: video.paused,
played: video.played,
readyState: video.readyState,
seeking: video.seeking,
src: video.src,
videoHeight: video.videoHeight,
videoWidth: video.videoWidth
}
}
}