JavaScript/PHP: Track the video watching time of a user on a web page?

530 Views Asked by At

My customer offers a video tutorial site where both teachers and students can register. The students' video watching time for each video should be tracked in order to give feedback to the teachers. The site is developed using pure HTML/PHP/JS/MySQL. The videos are delivered by a video hoster and there's no special JavaScript-API provided. I know there's no way to make sure someone IS actually watching and understanding the contents of a video (except by offering exams/quiz), so by "watching" I mean tracking the time during a combination of the following technical facts:

  • the browser window is visible (not minimized/tab in the background)
  • the video is running (not paused/stopped)

Due to the fact that a video has a duration of at least 6 min it's not necessary to get the exact number of seconds - an approximate value +- 10 sec would be enough here. I've already made up the following approach but I don't know if there are any major drawbacks I haven't thought of yet: Once the video starts playing an interval fires every e.g. 10 seconds. If the video's state is "running" and the document.visibilityState is not "hidden" the interval fires an ajax request to the database which saves an entry "user_id, video_id, datetime". So a cronjob could collect these milestones once a day and calculate the total watching time on a user/video basis. Of course I'd have to make sure the time between these entries doesn't exceed a certain value, in this case let's say 15 seconds, to make sure the video hasn't been paused for a longer time.

Does anyone know a smarter and/or safer way or do you see any major drawbacks concerning

  • reliability
  • functionality (desktop/mobile browsers)
  • performance (database?)

Thanks for sharing your thoughts with me!

0

There are 0 best solutions below