send recsys on dispose

This commit is contained in:
DispatchCommit 2021-06-21 15:13:51 -07:00 committed by jessopb
parent d781dead32
commit aec52dc6cb
2 changed files with 73 additions and 15 deletions

View file

@ -16,7 +16,7 @@ const select = (state, props) => {
const urlParams = new URLSearchParams(search); const urlParams = new URLSearchParams(search);
const autoplay = urlParams.get('autoplay'); const autoplay = urlParams.get('autoplay');
const position = urlParams.get('t') !== null ? urlParams.get('t') : makeSelectContentPositionForUri(props.uri)(state); const position = urlParams.get('t') !== null ? urlParams.get('t') : makeSelectContentPositionForUri(props.uri)(state);
const userId = selectUser(state).id; const userId = selectUser(state) && selectUser(state).id;
return { return {
autoplayIfEmbedded: Boolean(autoplay), autoplayIfEmbedded: Boolean(autoplay),

View file

@ -1,5 +1,6 @@
// Created by xander on 6/21/2021 // Created by xander on 6/21/2021
import videojs from 'video.js/dist/video.min.js'; import videojs from 'video.js/dist/video.min.js';
import { v4 as uuidV4 } from 'uuid';
const VERSION = '0.0.1'; const VERSION = '0.0.1';
const recsysEndpoint = 'https://clickstream.odysee.com/log/video/view'; const recsysEndpoint = 'https://clickstream.odysee.com/log/video/view';
@ -15,6 +16,26 @@ const RecsysData = {
}, },
}; };
function createRecsys(claimId, userId, events, loadedAt, isEmbed) {
// TODO: use a UUID generator
const uuid = uuidV4();
const pageLoadedAt = loadedAt;
const pageExitedAt = Date.now();
return {
uuid: uuid,
parentUuid: null,
uid: userId,
claimId: claimId,
pageLoadedAt: pageLoadedAt,
pageExitedAt: pageExitedAt,
recsysId: recsysId,
recClaimIds: null,
recClickedVideoIdx: null,
events: events,
isEmbed: isEmbed,
};
}
function newRecsysEvent(eventType, offset, arg) { function newRecsysEvent(eventType, offset, arg) {
if (arg) { if (arg) {
return { return {
@ -30,14 +51,13 @@ function newRecsysEvent(eventType, offset, arg) {
} }
} }
function sendRecsysEvent(recsysEvent) { function sendRecsysEvents(recsys) {
const requestOptions = { const requestOptions = {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'text/plain' }, // application/json
body: JSON.stringify(recsysEvent), body: JSON.stringify(recsys),
}; };
fetch(recsysEndpoint, requestOptions) fetch(recsysEndpoint, requestOptions)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
@ -61,49 +81,87 @@ class RecsysPlugin extends Component {
super(player, options); super(player, options);
// Plugin started // Plugin started
console.log(`created recsys plugin for: videoId:${options.videoId}, userId:${options.userId}`); console.log(`Created recsys plugin for: videoId:${options.videoId}, userId:${options.userId}`);
// To help with debugging, we'll add a global vjs object with the video js player // To help with debugging, we'll add a global vjs object with the video js player
window.vjs = player; window.vjs = player;
this.player = player; this.player = player;
this.recsysEvents = [];
this.lastTimeUpdate = null;
this.currentTimeUpdate = null;
this.loadedAt = Date.now();
// Plugin event listeners // Plugin event listeners
player.on('playing', (event) => this.onPlay(event)); player.on('playing', (event) => this.onPlay(event));
player.on('pause', (event) => this.onPause(event)); player.on('pause', (event) => this.onPause(event));
player.on('ended', (event) => this.onEnded(event)); player.on('ended', (event) => this.onEnded(event));
player.on('ratechange', (event) => this.onRateChange(event)); player.on('ratechange', (event) => this.onRateChange(event));
player.on('seeking', (event) => this.onSeeking(event)); player.on('timeupdate', (event) => this.onTimeUpdate(event));
player.on('seeked', (event) => this.onSeeked(event));
player.on('dispose', (event) => this.onDispose(event));
}
addRecsysEvent(recsysEvent) {
this.recsysEvents.push(recsysEvent);
}
getRecsysEvents() {
return this.recsysEvents;
}
sendRecsysEvents() {
const event = createRecsys(
this.options_.videoId,
this.options_.userId,
this.getRecsysEvents(),
this.loadedAt,
false
);
console.log(event);
sendRecsysEvents(event);
} }
onPlay(event) { onPlay(event) {
const recsysEvent = newRecsysEvent(RecsysData.event.start, this.player.currentTime()); const recsysEvent = newRecsysEvent(RecsysData.event.start, this.player.currentTime());
this.log('onPlay', recsysEvent); this.log('onPlay', recsysEvent);
sendRecsysEvent(recsysEvent); this.addRecsysEvent(recsysEvent);
} }
onPause(event) { onPause(event) {
const recsysEvent = newRecsysEvent(RecsysData.event.stop, this.player.currentTime()); const recsysEvent = newRecsysEvent(RecsysData.event.stop, this.player.currentTime());
this.log('onPause', recsysEvent); this.log('onPause', recsysEvent);
sendRecsysEvent(recsysEvent); this.addRecsysEvent(recsysEvent);
} }
onEnded(event) { onEnded(event) {
const recsysEvent = newRecsysEvent(RecsysData.event.stop, this.player.currentTime()); const recsysEvent = newRecsysEvent(RecsysData.event.stop, this.player.currentTime());
this.log('onEnded', recsysEvent); this.log('onEnded', recsysEvent);
sendRecsysEvent(recsysEvent); this.addRecsysEvent(recsysEvent);
} }
onRateChange(event) { onRateChange(event) {
const recsysEvent = newRecsysEvent(RecsysData.event.speed, this.player.currentTime()); const recsysEvent = newRecsysEvent(RecsysData.event.speed, this.player.currentTime());
this.log('onRateChange', recsysEvent); this.log('onRateChange', recsysEvent);
sendRecsysEvent(recsysEvent); this.addRecsysEvent(recsysEvent);
} }
onSeeking(event) { onTimeUpdate(event) {
const recsysEvent = newRecsysEvent(RecsysData.event.scrub, this.player.currentTime()); this.lastTimeUpdate = this.currentTimeUpdate;
this.log('onSeeking', recsysEvent); this.currentTimeUpdate = this.player.currentTime();
sendRecsysEvent(recsysEvent); }
onSeeked(event) {
console.log(event);
const recsysEvent = newRecsysEvent(RecsysData.event.scrub, this.lastTimeUpdate, this.player.currentTime());
this.log('onSeeked', recsysEvent);
this.addRecsysEvent(recsysEvent);
}
onDispose(event) {
this.sendRecsysEvents();
} }
log(...args) { log(...args) {