Youtube iframe and Analytics tracking

If you need to use iframe to display youtube video on your website, tracking starts and pauses via analytics can be easily done.
The main trick is to add enablejsapi=1 into url.

E.g.
<iframe width=”560″ id=”somevideo” height=”315″ src=”https://www.youtube.com/embed/aCpGA7OrwyI?autoplay=1&enablejsapi=1″></iframe></pre>

becomes

<iframe width=”560″ id=”somevideo” height=”315″ src=”https://www.youtube.com/embed/aCpGA7OrwyI?autoplay=1&enablejsapi=1“></iframe></pre>

Now you can easily use the API. Feel free to use following example


<iframe width="560" id="somevideo" height="315" src="https://www.youtube.com/embed/aCpGA7OrwyI?autoplay=1&enablejsapi=1"></iframe></pre>

<script>
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player( 'somevideo', {
        events: { 'onStateChange': function(state){
            if(state.data){
                if(state.data == YT.PlayerState.PAUSED){
                    // paused
                    ga('send', 'event', 'somevideo', 'paused', creativecoffeebreak.getCurrentTime());
                } else if(state.data == YT.PlayerState.PLAYING){
                    if(!playedAlready){
                        playedAlready = true;
                        ga('send', 'event', 'somevideo', 'start', creativecoffeebreak.getCurrentTime());
                    }
                } else if(state.data == YT.PlayerState.ENDED){
                    ga('send', 'event', 'somevideo', 'finished', creativecoffeebreak.getCurrentTime());
                }
            }
        } }
    });
}

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

Please note that the YT.PlayerState.ENDED doesn’t work in almost any browser.
Also, the “PAUSED” event is triggered when fast forwarding as well.

You need to load iframe_api javascript, otherwise the YT object won’t exist.

Write a Comment

Comment

Webmentions

  • pharmacie canadienne kamagra pas chère

    kamagra canada échantillon gratuit

    achat kamagra francaise

  • bez lékařského předpisu kamagra

    koupit kamagra bez lékařského předpisu

    kamagra porovnat cenu cvs

  • buy itraconazole in Sydney

    itraconazole overnight

    how to buy itraconazole price in us

  • purchase fildena buy generic

    buy cheap fildena buy online no prescription

    buy cheap fildena usa pharmacy

  • cheap gabapentin generic cheapest

    online order gabapentin purchase online canada

    get gabapentin cheap canadian pharmacy

  • order dutasteride cost at walmart

    where to buy dutasteride no prescription no fees

    generique dutasteride

  • discount avodart canada with no prescription

    buying avodart cheap prices

    online order avodart uk over the counter

  • buying staxyn without prescriptions canada

    ordering staxyn uk buy over counter

    order staxyn without rx online

  • discount generic xifaxan

    discount online xifaxan

    buy xifaxan cheap europe

  • purchase rifaximin buy generic

    is generic rifaximin from canada

    how to order rifaximin usa sales

  • buy enclomiphene us overnight delivery

    enclomiphene no doctors consult

    cheap enclomiphene buy mastercard

  • how to order androxal generic free shipping

    buying androxal generic dosage

    cheap androxal australia purchase