Running time of youtube video

Asked

Viewed 1,024 times

3

I’ve been searching and I haven’t found anything concrete, I know there’s a youtube API but I haven’t found any right source.

How can I check on php whether or not the user watched a Youtube video embedded in my website? Or even count how many minutes the user watched?

  • Did you ever read in the API if that’s possible for her? If not for her, it’s hardly possible externally.

  • 1
  • It will never be possible to determine if he watched the video. He can let the video run and go to the bakery to get a loaf of bread and on arrival close his website. Do not make any decision based on the player being watched as the person may not be in front of the screen.

  • @durtto not necessarily if he "watched the video", but if he gave the play and ran the video to the end (even if he did go to the bakery during that run)

  • Do you want to know if a human watched all the video from your site even not being in front of the pc?

  • 1

    When it comes to Youtube API I usually use Javascript. In the API has a function called onytplayerStateChange. The return of that function always brings the status current video. If it is paused, if it is running or if it was finished being viewed, which in case would be the status 0. https://developers.google.com/youtube/js_api_reference#Subscribingevents

  • 1

    This LINK https://developers.google.com/youtube/youtube_player_demo shows you the video parameters on the right. Note that when it is finished, you can skip to the end to see, it changes the Player state for ended, and if you see the report below, it shows that it is null, or 0.

Show 2 more comments

1 answer

4


I was able to solve using the google API, which shows the time if the user gives play, pause or if the video ends:

<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script>

<script type="text/javascript">
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player( 'player', {
        events: { 'onStateChange': onPlayerStateChange }
    });
}

function onPlayerStateChange(event) {
    switch(event.data) {
        case 0:
            alert('vídeo acabou');
            break;
        case 1:
            alert('começou em '+player.getCurrentTime());
            break;
        case 2:
            alert('pausado em '+player.getCurrentTime());
            break;
    }
}
</script>

And just pass the url inside an iframe:

<iframe id="player" src="https://www.youtube.com/embed/DjB1OvEYMhY?enablejsapi=1"></iframe>
  • @Diegofelipe was through your link that I was able to find the solution, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.