Show Menu
TOPICS×

Queueing events when sessions response is slow

The Media Collection API is RESTful: i.e, you make an HTTP request and wait for the response. This is an important point only for when you make a Sessions request to obtain a Session ID at the beginning of video playback. This is important because the Session ID is required for all subsequent tracking calls.
It is possible that your player may fire events
before the Sessions response returns
(with the Session ID parameter) from the backend. If this occurs, your app must queue any tracking events that arrive between the Sessions request and its response. When the Sessions response arrives, you should first process any queued events , then you can start processing
live
events with the Events calls.
The Events request does not return data back to the client beyond an HTTP response code.
Check the Reference Player in your distribution for one way to process events prior to receiving a Session ID. For example:
var eventData = {}; // JSON payload eventData.playerTime = getPlayerTime(); // Required eventData.eventType = "play"; // Required eventData.params = {}; // Optional for events VideoPlayer.prototype._collectEvent = function(eventData) { // If we don't have a Session ID yet, // queue the event and return... if (!sessionStarted) { console.log("[Player] Queueing event "); _pendingEvents.push(eventData); return; } // If we DO have a Session ID, process the // tracking event... apiClient.request({ "baseUrl": "{endpoint}", "path": "api/v1/{sid}/events", // events request "method": "POST", "data": eventData }).then((response) => { […] } } VideoPlayer.prototype.collectEvent = function (eventType, eventParams) { if (typeof eventParams === 'undefined') { eventParams = {}; } this._collectEvent({ eventType: eventType, // Required playerTime: getPlayerTime(), // Required params: eventParams // Optional }); }; VideoPlayer.prototype.getPlayerTime = function() { return { playhead: this.getPlayhead(), // playhead value in seconds ts: this.getCurrentTimestamp() // timestamp value in milliseconds }; };
Process any queued events -
The reference player processes queued events as follows:
[…] this._processPendingEvents(); // Once you have a Session ID, […] // process any queued events VideoPlayer.prototype._processPendingEvents = function() { this._pendingEvents.forEach((eventData) => { this._collectEvent(eventData); }); this._pendingEvents = []; }
Continue to process tracking events as they occur.