Tracking player events
The Videojs Events plugin was designed to work with Video.js and the Nuevo plugin. The plugin fires the most important player events and counts some statistical data. The plugin includes the basic implementation for Google and Matomo (formerly Piwik) analytics platforms that you can enable in a simple way if only Analytics or Matomo code snippets are applied to your website. It is essential to include video title and video ID through Nuevo plugin options. This allows for the identification of video in statistics.Code snippet
<script src="//www.domain.com/videojs/video.min.js"></script><script src="//www.domain.com/videojs/nuevo.min.js"></script><script src="//www.domain.com/videojs/plugins/videojs.events.js"></script>
const player = videojs("my_player", {license: "key" });player.nuevo({title:"video title", video_id:"video id"});player.events({analytics:true});Code snippet
player.on('track', (e, data) => {if (data.event === 'firstPlay') {let session_id = data.sessionId;let video_id = data.videoId;let video_title = data.videoTitle;let category = data.category;let duration = data.duration;let start_time = data.initialTime;// do something with data...}});Available events that you can track through javascript are:
- firstPlay (fired when video starts). Paramter initialTime points to video start time, as not necessary this must me 0 position.
 - 10% (10% watch time progress).
 - 25% (25% watch time progress).
 - 50% (50% watch time progress).
 - 75% (75% watch time progress).
 - 90% (90% watch time progress).
 - ended (fired when media playback has been completed).
 - buffered (fired when buffering starts. Parameter bufferTime indicates the time when buffering ended).
 - pause (fired when media paused. Parameter pauseCount inidicates the number of the playback pause event).
 - resume (fired when media playback resumed. Parameter resumeCount inidicates the number of the playback resume event).
 - replay (fired ehen the user decided to replay media).
 - enterFullscreen (fired when the user goes fullscreen).
 - exitFullscreen (fired when the user goes back from fullscreen).
 - seek (fired when the user seek in media time. Parameter seekTo indicates the time position the user seek to).
 - mute (fired when media is muted).
 - unmute (fired when media is unmuted).
 - rateChange (fired when media playback rate has been changed. Parameter rate indicates new rate value).
 - resolutionChange (fired when video resolution has been changed. Parameter rate indicates new resolution, e.g. 1080p).
 - summary (fired only when media playback was ended. The most important available parameter is watchedTime with the value equal to real time that the user spent to watch video. Other parameters are totalDuration, pauseCount, bufferCount and seekCount).
 - user Provides details about the user like IP address, region, country, browser and device).
 
All three services have limits. Abstract service has only paid plans (starting at $9.00), ipadata service offers 1500 free requests daily (on-commercial use) or paid plans (starting at $10.00), and ipapi service offers up to 1000 requests daily free or paid plans (from $12.00). The first two services require you to provide an API key, which you may pass as a plugin's option. If you do not provide an apiKey, the plugin attempts to fetch the ipapi.co service. In order to be compliant with GDPR for users located in the EEA, you can anonymize the user's IP. IP anonymization sets the last digits of users' IP addresses to zeros, so the website visitor's IP address is made anonymous.
Code snippet
player.events({ abstractApiKey: "Your API Key" });orplayer.events({ ipdataApiKey: "Your API Key" });
player.events({ anonymizeIP: true });userevent's object.
Code snippet
player.on('track', (e, data) => {if (data.event === 'user') {let user_ip = data.userInfo.ip_address;let city = data.userInfo.city;let region = data.userInfo.region;let country = data.userInfo.country;let country_code = data.userInfo.country_code;let country_code = data.userInfo.country_code;let browser = data.userInfo.browser;let device = data.userInfo.device;}});Code snippet
player.events({ userIP: false });getSummaryallows you to extract summary data in JavaScript at any time, not only when the video is ended. This may be useful for apps with dynamic routing to catch video summary data before dynamic navigation.
Code snippet
let summarydata = player.getSummary();
let session_id = summarydata.session_Id;
let video_id = summarydata.video_Id;
let video_title = summarydata.videoTitle;
let category = summarydata.category;
let watched_time = summarydata.watchedTime;
let pause_count = summarydata.pauseCount;
let resume_count = summarydata.resumeCount;
let seek_count = summarydata.seekCount;
let buffer_duration = summarydata.bufferDuration;
Tracking Url
The Events plugin allows you to define custom tracking Url.Code snippet
player.events({ trackingUrl: "https://script_url" });firstPlayevent is like this:
Code snippet
{event: "firstPlay",value: 0,sessionId: "unique timestamp",category: "video",videoId: "Id of the video",videoTitle: "Titlte of the video"}| Event name | Event value(sec) | 
| firstPlay | start time (s) | 
| progress | 10%, 25%, 50%, 75% or 90% | 
| replay | null | 
| completed | video duration (sec) | 
| fullscreen | 1 or 0 | 
| watchedTime | time (sec) | 
| user | ip_address, city, region, country, country_code, browser, device | 
The set of events sent to the custom URL doesn't differ much from the ones sent to Analytics or Matomo. However, there is one extra object sessionID, which allows to identify events for the same playback, and what is more important, there is an event user containing an array of user details, usually fired shortly after the firstPlay event.
Reading events posted to a URL depends on the web server configuration and the programming language used. The event's data is sent to the server as serialized JSON. In the most popular PHP language, to get single event data, you can find this code useful:Since the value of the user event is not a single value but another array of objects, the PHP code can be like below:Once you have event data, you can save it in a database, build your own analytics application, present the data in your preferred way.
Code snippet
<?php$data = file_get_contents("php://input");$data = json_decode($data);
$event_name = $data->event;$event_value = $data->value;$session_id = $data->sessionId;$category = $data->category;$video_id = $data->videoId;$video_title = $data->videoTitle;?>Code snippet
<?php$data = file_get_contents("php://input");$data = json_decode($data);
$event_name = $data->event;if($event_name=='user') {$user = json_decode($data->value);$ip = $user->ip_address;$city = $user->city;$region = $user->region;$country = $user->country;$country_ip = $user->country_ip;$browser = $user->browser;$device = $user->device;}?>