-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd4cebf
commit 535fa4b
Showing
11 changed files
with
97 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ webpack.config.js | |
node_modules | ||
test | ||
dist/test | ||
sample | ||
sample | ||
index.ts | ||
jest.config.js | ||
samples | ||
.github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
# Changelog | ||
|
||
All changes to this project will be documented in this file. | ||
|
||
## [2.0.0] - 2024-07-31 | ||
|
||
- Switch to api.video analytics v2.0 | ||
|
||
## [1.0.5] - 2021-11-22 | ||
|
||
- Bump player-analytics version | ||
|
||
## [1.0.4] - 2021-06-30 | ||
- Add dist/* | ||
|
||
- Add dist/\* | ||
|
||
## [1.0.3] - 2021-06-30 | ||
|
||
- Bump npm player-analytics dependency version | ||
|
||
## [1.0.1] - 2021-03-31 | ||
|
||
- Bump npm dependencies version | ||
|
||
## [1.0.0] - 2021-03-24 | ||
|
||
- First release |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,75 @@ | ||
import { CommonOptions, PlayerAnalytics } from '@api.video/player-analytics'; | ||
import Hls from 'hls.js'; | ||
import { CommonOptions, PlayerAnalytics } from "@api.video/player-analytics"; | ||
import Hls from "hls.js"; | ||
|
||
export type HlsJsApiVideoAnalyticsOptions = CommonOptions; | ||
|
||
export class HlsJsApiVideoAnalytics { | ||
private isFirstPlay = true; | ||
private hls: Hls; | ||
private videoElement: HTMLMediaElement | null | undefined; | ||
private seekingStart?: number; | ||
private seekingEnd?: number; | ||
private options?: CommonOptions; | ||
private isFirstPlay = true; | ||
private hls: Hls; | ||
private videoElement: HTMLMediaElement | null | undefined; | ||
private seekingStart?: number; | ||
private seekingEnd?: number; | ||
private options?: CommonOptions; | ||
|
||
private playerAnalytics!: PlayerAnalytics; | ||
private playerAnalytics!: PlayerAnalytics; | ||
|
||
constructor(hls: Hls, options?: HlsJsApiVideoAnalyticsOptions) { | ||
this.hls = hls; | ||
this.options = options || {}; | ||
constructor(hls: Hls, options?: HlsJsApiVideoAnalyticsOptions) { | ||
this.hls = hls; | ||
this.options = options || {}; | ||
|
||
if (this.hls.media && (this.hls as any).url) { | ||
this.init(); | ||
} else { | ||
hls.on('hlsManifestLoaded', (event, data) => { | ||
this.init(); | ||
}); | ||
} | ||
if (this.hls.media && (this.hls as any).url) { | ||
this.init(); | ||
} else { | ||
hls.on("hlsManifestLoaded", (event, data) => { | ||
this.init(); | ||
}); | ||
} | ||
} | ||
|
||
private init() { | ||
this.videoElement = this.hls.media; | ||
this.isFirstPlay = true; | ||
private init() { | ||
this.videoElement = this.hls.media; | ||
this.isFirstPlay = true; | ||
|
||
if (this.playerAnalytics) { | ||
this.playerAnalytics.destroy(); | ||
} else { | ||
events.forEach((eventName) => { | ||
this.hls.media?.addEventListener(eventName, (e) => this.onEvent(eventName, e)); | ||
}); | ||
} | ||
this.playerAnalytics = new PlayerAnalytics({ | ||
...this.options, | ||
mediaUrl: (this.hls as any).url | ||
}); | ||
if (!this.playerAnalytics) { | ||
this.playerAnalytics = new PlayerAnalytics({ | ||
...this.options, | ||
mediaUrl: (this.hls as any).url, | ||
}); | ||
} | ||
|
||
|
||
private onEvent(eventName: string, event: any) { | ||
const currentTime = this.videoElement?.currentTime || 0; | ||
|
||
if (eventName === 'timeupdate') { | ||
this.playerAnalytics.updateTime(currentTime); | ||
} | ||
if (eventName === 'canplay') { | ||
if (this.isFirstPlay) { | ||
this.playerAnalytics.ready(); | ||
} | ||
} | ||
if (eventName === 'play') { | ||
if (this.isFirstPlay) { | ||
this.playerAnalytics.play(); | ||
this.isFirstPlay = false; | ||
} else { | ||
this.playerAnalytics.resume(); | ||
} | ||
} | ||
if (eventName === 'pause') { | ||
this.playerAnalytics.pause(); | ||
|
||
} | ||
if (eventName === 'ended') { | ||
this.playerAnalytics.end(); | ||
} | ||
if (eventName === 'seeked') { | ||
this.playerAnalytics.seek(this.seekingStart || 0, this.seekingEnd || 0); | ||
} | ||
if (eventName === 'seeking') { | ||
this.seekingEnd = currentTime; | ||
} | ||
if (this.videoElement?.seeking === false && eventName !== 'timeupdate') { | ||
this.seekingStart = currentTime; | ||
} | ||
if (this.videoElement) { | ||
this.playerAnalytics.ovbserveMedia(this.videoElement as HTMLVideoElement); | ||
} else { | ||
console.error("No video element found"); | ||
} | ||
} | ||
} | ||
|
||
|
||
const events = ['abort', | ||
'canplay', | ||
'canplaythrough', | ||
'durationchange', | ||
'emptied', | ||
'encrypted', | ||
'ended', | ||
'error', | ||
'interruptbegin', | ||
'interruptend', | ||
'loadeddata', | ||
'loadedmetadata', | ||
'loadstart', | ||
'mozaudioavailable', | ||
'pause', | ||
'play', | ||
'playing', | ||
'progress', | ||
'ratechange', | ||
'seeked', | ||
'seeking', | ||
'stalled', | ||
'suspend', | ||
'timeupdate', | ||
'volumechange', | ||
'waiting']; | ||
const events = [ | ||
"abort", | ||
"canplay", | ||
"canplaythrough", | ||
"durationchange", | ||
"emptied", | ||
"encrypted", | ||
"ended", | ||
"error", | ||
"interruptbegin", | ||
"interruptend", | ||
"loadeddata", | ||
"loadedmetadata", | ||
"loadstart", | ||
"mozaudioavailable", | ||
"pause", | ||
"play", | ||
"playing", | ||
"progress", | ||
"ratechange", | ||
"seeked", | ||
"seeking", | ||
"stalled", | ||
"suspend", | ||
"timeupdate", | ||
"volumechange", | ||
"waiting", | ||
]; |
Oops, something went wrong.