@aller/blink
Version:
A library for tracking user behaviour.
87 lines • 3.73 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateVideoEventTime = exports.PLAYER_VISIBILITY_STATES = exports.PLAYER_TIMER_STATES = void 0;
var PLAYER_TIMER_STATES;
(function (PLAYER_TIMER_STATES) {
PLAYER_TIMER_STATES["RUNNING"] = "RUNNING";
PLAYER_TIMER_STATES["STOPPED"] = "STOPPED";
})(PLAYER_TIMER_STATES = exports.PLAYER_TIMER_STATES || (exports.PLAYER_TIMER_STATES = {}));
var PLAYER_VISIBILITY_STATES;
(function (PLAYER_VISIBILITY_STATES) {
PLAYER_VISIBILITY_STATES["VISIBLE"] = "VISIBLE";
PLAYER_VISIBILITY_STATES["HIDDEN"] = "HIDDEN";
})(PLAYER_VISIBILITY_STATES = exports.PLAYER_VISIBILITY_STATES || (exports.PLAYER_VISIBILITY_STATES = {}));
function calculateVideoEventTime(times, videoId, players, playerId, time) {
if (!times || times.length === 0) {
return [];
}
var forSpecificVideo = times.filter(function (t) { return t.videoId === videoId && t.playerId === playerId; });
var playerEvents = players[playerId] || [];
var events = __spreadArrays(playerEvents, forSpecificVideo);
var filtered = filterValidEvents(events);
var result = filtered.reduce(function (all, curr, i, source) {
// When a stop occurs, create a new event, and calculate time from start
if (curr.type === 'stop' || curr.type === 'hidden') {
var timestamp = i > 0 ? curr.time.getTime() - source[i - 1].time.getTime() : 0;
all.push({
watchTime: timestamp,
startEvent: source[i - 1],
stopEvent: curr,
});
}
return all;
}, []);
return result;
}
exports.calculateVideoEventTime = calculateVideoEventTime;
function filterValidEvents(times) {
var currentState = PLAYER_TIMER_STATES.STOPPED;
var currentVisibility = PLAYER_VISIBILITY_STATES.HIDDEN;
var sorted = times.sort(function (a, b) { return a.time.getTime() - b.time.getTime(); });
return sorted.filter(function (t) {
if (currentState === PLAYER_TIMER_STATES.RUNNING) {
if (currentVisibility === PLAYER_VISIBILITY_STATES.VISIBLE) {
if (t.type === 'hidden') {
currentVisibility = PLAYER_VISIBILITY_STATES.HIDDEN;
return true;
}
if (t.type === 'stop') {
currentState = PLAYER_TIMER_STATES.STOPPED;
return true;
}
}
else {
if (t.type === 'shown') {
currentVisibility = PLAYER_VISIBILITY_STATES.VISIBLE;
return true;
}
}
}
if (currentState === PLAYER_TIMER_STATES.STOPPED) {
if (currentVisibility === PLAYER_VISIBILITY_STATES.VISIBLE) {
if (t.type === 'start') {
currentState = PLAYER_TIMER_STATES.RUNNING;
return true;
}
}
else {
if (t.type === 'shown') {
currentVisibility = PLAYER_VISIBILITY_STATES.VISIBLE;
return false;
}
if (t.type === 'start') {
currentState = PLAYER_TIMER_STATES.RUNNING;
return false;
}
}
}
});
}
//# sourceMappingURL=video-event-time.js.map