如何使用Google Analytics(分析)和YouTube API在一个网页上跟踪两​​个视频? [英] How can I track two videos on one page using Google Analytics and YouTube API?

查看:197
本文介绍了如何使用Google Analytics(分析)和YouTube API在一个网页上跟踪两​​个视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a video on our website that I'm tracking the views of using Google Analytics. This is the code:

<!-- 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而不是显示两个,它只会显示原始的。

If I copy and paste this code, and change the video ID it overwrites the original. If I copy and paste this code, and leave the video ID instead of showing two it just shows the original.

我如何工作,所以我有一个视频在页面上被跟踪?我认为它与< div id ='player'>< / div> 有关,但没有看到如何更改目标div。 / p>

How can I work this so I have more then one video being tracked on the page? I'm thinking it has to do with the <div id='player'></div> but am not seeing how to change the target div.

推荐答案

我最终如何解决这个问题是使用Nevo Lightbox。然后,我为每个视频创建一个缩略图,并将其链接起来像这样:

How I ended up working around this is using Nevo Lightbox. I then created a thumbnail for each video and linked it somewhat like so:

<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和视频说明。可能有一个更优雅的解决方案,但是完成了这项工作。

Changing the VideoID and Video Description. There's probably a more elegant solution, but its getting the job done.

这篇关于如何使用Google Analytics(分析)和YouTube API在一个网页上跟踪两​​个视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆