1

The twist is if someone leaves while the video is playing that same person can go back to the video page and the site will automatically play from when the user had left at.
Currently I'm using YouTube Player API to load the video.

How can I resolved this issue my friends?
I'm not sure but I'm trying to research with this key word.
But still not done.
"How to track that how much time the user spent watching on a video in YouTube Player API".

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jason
  • 33
  • 1
  • 7
  • 1
    use the YT API and start a timer on `play`, stop it on `pause` or `stop`. Compute view time from that. Not very accurate, since user might leave the player on yet not watching the video, but might do – Nikos M. Nov 08 '15 at 13:52
  • Yes, but I dont know how to track that how much time the user spent watching on that when they leave and then come back with this video. Is there any function related to YT API to get it? – Jason Nov 08 '15 at 14:08
  • hook to YT API play pause, stop events and start/.stop timer – Nikos M. Nov 08 '15 at 14:15
  • Can you write me an example for this function? – Jason Nov 08 '15 at 14:16
  • i was using YT API long ago, but it is straight forward you hook to events as any other event in javascript and just start/stop a timer (or whatever you use to measure time) – Nikos M. Nov 08 '15 at 14:24
  • Yup, I understand your mean @NikosM. Absolute, we must hook to YT API to custom this. But my current confuse is how to get the spent time of a user whom are viewing video. Examples: I go to your site and view a video on it. After that, I leave your site and I will come back in 1 hour with the same video I've left it before. How can I get exactly the spent time I've done on that to start at. – Jason Nov 08 '15 at 16:20
  • Your title and description are two completely different questions. Ask two separate ones.. –  Nov 08 '15 at 19:11
  • Hi @DaniSpringer, Yes, I'm so sorry about this. But can you please help me for this? I mean both my title and description. Thanks you so much! – Jason Nov 09 '15 at 03:35
  • You should then ask two separate questions. Include (in both) what you managed so far and how, and what you want. If you follow the how-to-ask rules of this site, you may get help you never expected, but if you don't, you won't even get what you expected already. –  Nov 09 '15 at 09:19

1 Answers1

1

Use the YT API and start a timer on play, stop it on pause or stop. Not very accurate, since user might leave the player on yet not watching the video, but might do.

Was using YT API long ago, but it is straight forward you hook to events as any other event in javascript and just start/stop a timer (or whatever you use to measure time). Initialy set total_viewed_time to zero, then on play compute a start_time and on stop/pause/finish update total_viewed_time by the time viewed, i.e end_time-start_time. For example see below:

var total_viewed_time = 0, start_time = 0, end_time = 0;

// assume we have a yt video which we we hook for events (see YT API for exactly how to hook)
ytvideo.on('play', function( ){
    start_time = new Date().getTime(); // milliseconds
    end_time = start_time;
});
ytvideo.on('pause stop finish', function( ){
   end_time = new Date().getTime(); // milliseconds
   total_viewed_time += end_time-start_time; // milliseconds
});

// total_viewed_time measures how much time (in milliseconds) the user had the player on (maybe watching?)
// you can then convert this time to seconds or minutes or whatever suits you
// to get the time in seconds do: total_viewed_time/1000
// to get the time in minutes do: total_viewed_time/(60*1000)
// and so on..

Additionaly if the application needs to check if user has switched browser tabs or windows, the application can listen to blur or unfocus event and stop the timer and re-start it on browser tab / window focus

  1. Is there a way to detect if a browser window is not currently active?
  2. Check if window has focus
  3. Detect If Browser Tab Has Focus
Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • Hi @Nikos: Thanks your for example. It's very useful. But how can we know who is the user whom has been watched this video? – Jason Nov 09 '15 at 03:43
  • @Jason, this depends on your application, your application should keep state of the current user. Then just add this information to the examle in the answer. User management is application-dependent. some may use an external login (e.g facebook) while others an internal DB of users or both – Nikos M. Nov 09 '15 at 08:30
  • Great @Nikos! Thank you so much for your support. – Jason Nov 09 '15 at 10:40
  • @Jason, glad it helps, cheers! – Nikos M. Nov 09 '15 at 10:47