UNPKG

@aller/blink

Version:

A library for tracking user behaviour.

78 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 events = players[playerId].concat(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