Skip to content

Commit

Permalink
Analytics v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lando committed Jul 30, 2024
1 parent cd4cebf commit 535fa4b
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 187 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
registry-url: 'https://registry.npmjs.org'
registry-url: "https://registry.npmjs.org"
- run: npm install --no-save
- run: npm publish --access=public
- run: npm run build && npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
6 changes: 5 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ webpack.config.js
node_modules
test
dist/test
sample
sample
index.ts
jest.config.js
samples
.github
12 changes: 11 additions & 1 deletion CHANGELOG.md
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
15 changes: 0 additions & 15 deletions dist/index.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions dist/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions dist/index.js.LICENSE.txt

This file was deleted.

Empty file removed dist/test/index.test.d.ts
Empty file.
156 changes: 60 additions & 96 deletions index.ts
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",
];
Loading

0 comments on commit 535fa4b

Please sign in to comment.