2014-02-17 49 views
1

我升级到Universal Analytics并希望跟踪嵌入在我的tumblr博客中的youtube视频。任何人都可以帮助我调整以下代码以使用Universal Analytics?使用Universal Analytics跟踪嵌入的YouTube视频


<script> 
    // This code loads the IFrame Player API code asynchronously. 
    var tag = document.createElement('script'); 
    tag.src = "https://www.youtube.com/iframe_api"; 
    var firstScriptTag = document.getElementsByTagName('script')[0]; 
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

    // This function creates an <iframe> (and YouTube player) 
    // after the API code downloads. 
    var player; 
    function onYouTubeIframeAPIReady() { 
    player = new YT.Player('player', { 
     height: '289', 
     width: '428', 
     videoId: 'VIDEO ID', 
     events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    }); 
    } 
    // The API will call this function when the video player is ready. Uncomment the below code to start video when ready 
    function onPlayerReady(event) { 
    // event.target.playVideo(); 
    } 
    function onPlayerStateChange(event) { 
    var lastAction=""; 
     switch (event.data){ 
     case YT.PlayerState.PLAYING: 
     if (lastAction != 'paused'){ 
      _gaq.push(['_trackEvent','video', 'Playing', getPercentage()]); 
     } 
     else{ 
      lastAction = ''; 
      } 
     break; 
     case YT.PlayerState.ENDED: 
      _gaq.push(['_trackEvent','video', 'Completed',getPercentage()]); 
     break; 
     case YT.PlayerState.PAUSED: 
     if (lastAction != 'paused'){ 
      _gaq.push(['_trackEvent','video', 'Paused', getPercentage()]); 
      lastAction= "paused"; 
      } 
     break; 
     } 
    } 
    function getPercentage() 
    { 
    var pecentage =((player.getCurrentTime()/player.getDuration())*100).toFixed(); 
     if(pecentage > 0 && pecentage <= 25){ 
     return "0-25%";} 
     elseif(pecentage > 25 && pecentage <= 50){ 
     return "25-50%";} 
     elseif(pecentage > 50 && pecentage <= 75){ 
     return "50-75%";} 
     elseif(pecentage > 75){ 
     return "75-100%";} 
    } 
    function stopVideo() { 
    _gaq.push(['_trackEvent','video', 'Stopped', player.getDuration()]); 
     player.stopVideo(); 
    } 
</script> 
+0

在这篇对你的工作旧的分析?我现在正在努力做到这一点,但没有任何出发点。如果这样做,那么它可能只是通过https://developers.google.com/analytics/devguides/collection/upgrade/reference将'_gaq.push ...'更改为新的'ga('send'...'/gajs-analyticsjs – Ryan

回答

3

我与您的代码直接审判,有一些问题,但刚出场时的基本通知至少工作/使用此代码完成:

<div id="player"></div> 

<script> 
    // 2. This code loads the IFrame Player API code asynchronously. 
    var tag = document.createElement('script'); 

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

    // 3. This function creates an <iframe> (and YouTube player) 
    // after the API code downloads. 
    var player; 
    function onYouTubeIframeAPIReady() { 
    player = new YT.Player('player', { 
     height: '360', 
     width: '640', 
     videoId: 'VIDEOID HERE', 
     events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    }); 
    } 

    // 4. The API will call this function when the video player is ready. 
    // Uncomment the event to have video start automatically. 
    function onPlayerReady(event) { 
    // event.target.playVideo(); 
    } 

    // 5. The API calls this function when the player's state changes. 
    // The function indicates that when playing a video, 
    // the player should send to Google Analytics. 
    function onPlayerStateChange(event) { 
    if (event.data == YT.PlayerState.PLAYING) { 
     ga('send', 'event', 'YouTube', 'Watched', 'Promo from Main Page'); 
    } 
    if (event.data == YT.PlayerState.ENDED) { 
     ga('send', 'event', 'YouTube', 'Finished', 'Promo from Main Page'); 
    } 
    } 
</script>   
+1

感谢您的回复Ryan。虽然了解视频是播放还是停止是有用的,但真正有用的是了解播放的持续时间(以25%为增量)。 有关如何编码的任何建议在UA中? – Art

+0

@你是否尝试过使用与我的代码结合使用的代码(或者我在评论中留下的Google链接)? – Ryan

+0

本周末计划进行测试,工作一直很疯狂,我一直无法尝试。感谢你! – Art

相关问题