2016-11-21 51 views
1

我發現這個tutorial有關如何跟蹤的嵌入YouTube視頻GA事件,並做了我的劇本由它:跟蹤嵌入視頻事件與YouTube API - ytcfg沒有定義(...)

(function ($) { 
    $(document).ready(function() { 

    var tag = document.createElement('script'); 
    tag.src = "http://www.youtube.com/iframe_api"; 
    var firstScriptTag = document.getElementsByTagName('script')[0]; 
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

    }); 

    // This code is called by the YouTube API to create the player object 
    function onYouTubeIframeAPIReady(event) { 
    player = new YT.Player('player', { 
     events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    }); 
    } 

    var done = false; 

    function onPlayerStateChange(event) { 

    // track when user clicks to Play 
    if (event.data == YT.PlayerState.PLAYING && done===false) { 
     ga('send', 'event', 'Video', 'Prehratie videa', 'GE', 1); 
     done = true; 
    } 
    } 

}(jQuery)); 

腳本添加在theme_preprocess_page()的每一頁。然而,我發現了一個錯誤:

Uncaught ReferenceError: ytcfg is not defined(…)

Here你可以讀它在YouTube的側錯誤。確實,我的腳本中的一切都可以,我能做些什麼才能使其發揮作用?

回答

0

嗯,這是YouTube上的一個bug,現在有一個existing bug ticket。您可以隨時查看此故障單以獲取有關此問題的更新。

對於解決方法,我在此YouTube Help Forum中發現使用對象而不是iframe。如果它適用於你的情況,你可以試試這個臨時解決方案。希望它可以幫助你。