如何使用Google Analytics和YouTube API在一个页面上跟踪两个视频?

时间:2014-05-13 14:29:32

标签: javascript dom google-analytics youtube-api analytics

我的网站上有一个视频,我正在跟踪使用Google Analytics的观点。这是代码:

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

<script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var utm_source='<?php echo $utm_source ?>';
      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: 'xxxxxxxxxx',
          frameborder: '0',
          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 (state=1),
      //    the player should play for six seconds and then stop.
      function onPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING) {
                if (utm_source == 'zilch') {
                    ga('send', 'event', 'YouTube', 'Started', 'Promo from Main Page');
                }
                else if (utm_source == 'CAMPAIGN') {
                    ga('send', 'event', 'YouTube', 'Started', 'Promo from Main Page via CAMPAIGN');
                }
                else {
                    ga('send', 'event', 'YouTube', 'Started', 'Promo from Main Page');
                }
            }
            if (event.data == YT.PlayerState.ENDED) {
                if (utm_source == 'zilch') {
                    ga('send', 'event', 'YouTube', 'Finished', 'Promo from Main Page');
                }
                else if (utm_source == 'CAMPAIGN') {
                    ga('send', 'event', 'YouTube', 'Finished', 'Promo from Main Page via CAMPAIGN');
                }
                else {
                    ga('send', 'event', 'YouTube', 'Finished', 'Promo from Main Page');
                }
        }
      }
    </script>

如果我复制并粘贴此代码,并更改视频ID,则会覆盖原始代码。如果我复制并粘贴此代码,并保留视频ID而不是显示两个,则只显示原始内容。

我如何处理这个问题,以便在页面上跟踪多个视频?我认为它与<div id='player'></div>有关,但我没有看到如何更改目标div。

1 个答案:

答案 0 :(得分:0)

我最终解决这个问题的方法是使用Nevo Lightbox。然后我为每个视频创建了一个缩略图并将其链接起来:

<a href='https://www.youtube.com/watch?v=videoID' data-lightbox-gallery='playlist' title='Video description' onclick="ga('send', 'event', 'Video', 'Play', 'Demo on videos');">

更改VideoID和视频说明。可能有一个更优雅的解决方案,但它完成了工作。