Tracking YouTube Videos with Google Analytics

A while back, Google Analytics and YouTube introduced YouTube Insight, a tool to provide more information about the people viewing your videos on www.youtube.com. It’s pretty cool stuff and if you’re posting videos to YouTube it provides a lot of great information.

YouTube Insight: Information about those viewing your videos on www.youtube.com.

But what about YouTube videos that are embedded in a site? What’s the best way to measure interactions with these videos? You could use some basic metrics like Avg. Time on Page and Avg. Time on Site, but they’re averages and, well, averages suck. Fear not, there is a better way.

We can track almost every aspect of an embedded YouTube video using Event Tracking, a Google Analytics beta feature.

CHROME

There is one catch. In order to track an embedded YouTube video you must use the ‘chromeless’ YouTube player. A chromeless video player has no controls which means you must create all of the controls yourself. This results in a fair amount of coding.

Why do we have to use the chromeless player? The chromeless player let’s us add JavaScript code (i.e. GA Event tracking code) to user actions, thus giving us the ability to capture visitor actions as events. I’ll do my best to walk you through all of the code.

You can read more about the chromeless YouTube player on the YouTube developer site.

Let’s get started.

Event Data Model

Before we look at any code let’s talk about data and analysis. One of the keys to any successful event tracking implementation is a clear definition of the data we want to collect. Remember, event tracking is really a flexible data collection tool. We need to explicitly define the data we want to collect.

I want to track three primary things related to the embedded video:

1. Which video does the visitor choose to watch?
2. How do they interact with the video player (i.e how do they use the controls)?
3. How much time do they spend watching each video?

Now let’s translate these needs into the Google Analytics Event model which consists of Objects, Actions, Labels and Values.

Objects

The object is the part of the page that we want to track. Defining the object in this case is pretty simple: we want to track the YouTube video player, so we’ll create one object and call it “YouTube Video Player”. The object will literally be created in the code and I’ll explain how later.

Actions

Actions are the interactions that the visitor exerts on the object. What we want to track, and what we can track, really depend on what interactions we can capture. In this example, the actions we can capture are directly related to the chromeless YouTube player and what it ‘bubbles up’ for visitor interactions.

Based on the chromeless player, and the data needs we have defined, here are the actions that we are going to track:

  • Player Loaded: Indicates the YouTube player has loaded. No video has been loaded into the player, but the player is ready for a video.
  • Video Started: Indicates the user has chosen a video and it has started playing in the player.
  • Play: User has restart the video after it has been paused.
  • Pause: Video has been paused.
  • Mute: Video has been muted.
  • Unmute: Video has been unmuted.
  • Error: An error has occurred.
  • Ended: Selected video has ended, either intentionally or on purpose by the visitor.
  • Get Embed Code: Request HTML code to embed the video in a site.
  • Get Video URL: Request YouTube URL for the current video.

Like the object, actions are actually created in the code and we’ll see how later.

There are other things that we can track, but I’ve decided to limit it to the above list. Why? I don’t think there is a lot of insight that can be gained from some of the other information that is available to us. Plus the above actions will cover the data needs we’ve defined.

Google Analytics Event Labels.

Labels

Labels are the content of the object. In the case of most video player object, the label will be the name of the video playing. In this example you can choose three potential videos from a drop down box. The name of the video will become the label that is tracked in Google Analytics.

  • Apollo 11 Launch
  • About the YouTube API
  • Phish – Weekapaug Groove
  • Authors@Google: Avinash Kaushik

Values

This is where things can get a bit unique. The value collected by GA’s Event tracking is just a plain integer. All values associated with actions are summed and averaged. So we can’t mix different types of data, like monetary values (which may be in dollars) and time (which may be in seconds). For this example, we’re going to track time, so the values will be in seconds and will represent the total time that each video played.

I’m only going to associate a value with a single actions: Ended. When a video ends we’re going to record the total number of seconds played for that video.

The Example

Now that we’ve created the data model for tracking a YouTube player, we need to create a YouTube player to track. :) Remember we can only access visitor actions when we use the chromeless YouTube player.

Here’s a simple page that I created using the chromeless player and Google Analytics Event Tracking.

If the above iFrame is not visible, you can view the entire code here.

For those interested, this test page is actually a modification of the YouTube API example.

To start the tracking, select a video from the drop down and it will start playing. Then experiment with some of the controls. All actions that we defined in our event model will be tracked.

If you want to change videos just choose a new video from the drop down box.

I don’t want to go over too much of the code, but here’s the JavaScript behind the tracking.

If the above iFrame is not visible, you can view the entire code here.

A majority of the code is not Google Analytics code, but rather YouTube player code. What I’ve done is added the Event Tracking code in the appropriate places to capture the visitor interactions. Setting up the event tracking code is really pretty simple.

The first thing I did was create the Object, which is called ‘YouTube Video Player’. I created the object in the main GA page tag.




After creating the object I added the _trackEvent() method wherever I want to capture a visitor action, like Play or Pause. Here’s how I capture the Play action:


function play() {
  if (ytplayer) {
    ytplayer.playVideo();
    ytpEventTracker._trackEvent("Play",eventLabel);
  }
}

Most of the Play code is actually YouTube code. The only GA specific code is ytpEventTracker._trackEvent("Play",eventLabel);.

We pass three values to _trackEvent(). The first is the name of the action and this value will appear in the GA Actions report. The second value is the lable value which will appear in the Labels report. I should note that eventLabel is the name of the video (per the data model) that is currently playing when the visitor clicks on Play.

The final value, which is not shown above, is the value value. In this example, the value is the total number of seconds played. This is only recorded for the Ended action.

If you look at the example code, all I really did was add ytpEventTracker._trackEvent() to all the actions that I wanted to track.

The Results

Enough with all the techno-mumbo-jumbo. Let’s look at some of the data starting at the top of our hierarchy: the Object report.

Google Analytics Object report.

Noting too exciting here. We’re only tracking a single object here, hence the one line of data. The data can be a little confusing. The total number of events is really the total number of actions that occurred. The number of unique events is the number of visits that included an event.

But let’s dig a bit deeper. Let’s see which video was most popular by viewing the labels associated with the YouTube Video Player object. All I need to do is click on the Object name to view the associate labels and actions, and then choose ‘Labels’ using the Detail Level link above the table tabs.

Google Analytics Label report.

Here we can see that ‘Phish-Weekapaug Groove’ was the most popular video, when based on time. It had a higher total value than the other videos. Sorry Avinash. :)

One thing to notice is that measuring the popularity of each video based on Events is not correct. Just because a label has the most actions does not mean that it is the most popular.

Remember, an action can occur ever time a visitor interacts with the player. In this example it would be more accurate to judge popularity based on time.

Now let’s see how people interacted with the most popular video. What actions did visitors perform when ‘Phish-Weekapaug Groove’ was playing?

All I need to do is click on the label name to see the actions associated with the label.

Google Analytis Action report.

Cool, here we can see that the video was started 32 times, but only completed 12 times. It was viewed a total of 1775 seconds but only an average of 143 seconds.

What does it all mean? I know that the Phish video is 179 seconds long. So, on average, visitors viewed 80% of it. I can also tell, by the number of unique events, that the Phish video was viewed multiple times in the same visit. It must be pretty popular.

I also want to point out that we can view the data in other ways. We can navigate directly to the Actions report to see a list of all the actions, regardless of the object or label they are associated with. This is a great way to get a feel for the most commonly used features of our YouTube player.

Google Analytics Actions report.

Conclusion

So was this really worth it? Sure, I think so. I wanted to create a real life example that demonstrates some of the ways to use Event Tracking. I also wanted to use YouTube as an example because so may people use it as a platform to distribute video.

While it may seem like a lot of work to track video and other web 2.0 technologies it is vital. If you’re spending money producing videos, or if you’re selling video ad space, you need to know how people interact with your player and content. It’s the perfect use for Event Tracking.

Be Sociable, Share!

    Like this post? Sign up to get posts delivered to your inbox.

    Comments

    1. says

      Thanks for the heads up Eivind. I have resisted the FF 3.0 upgrade as it would break a lot of my extensions. Hopefully they get this fixed soon. I’m sure everyone will be rushing to track chromeless YouTube videos soon! :)

      Justin

    2. says

      Justin – this is awesome! I’m glad you’ve taken the time out to showcase the results that can come out of the integration of great technology and the clever use of site analytics tools.
      This is now bookmarked and will become a reference for next time I’m asked about what we can do with youtube videos, which works perfectly with the scripting engine we’ve already worked on for flash based video.
      Cheers, James.

    3. says

      Excelent post Justin. That’s just what I’m already doing at the website where I work: tracking our trailers with event tracking (I asked for access to the event tracking beta to my adwords manager ;)

      Just two points:

      1. Instead of using the YouTube chromeless player I user the JW FLV Media Player (http://www.jeroenwijering.com/?item=JW_FLV_Media_Player). It exposes the full event model (play, stop, fullscreen….), has an API, plays YouTube videos, supports playlists and what’s better, it very easy to skin it and it’s already beautiful enough ;) It’s not free for commercial use, but it costs only 20 euros.

      2. Maybe it’s an event tracking bug or maybe it only happened to me, but when I created an event tracker in my main GA profile I received an small amount of what I call “ghost visits”, visits with zero pageviews (in my case about 1% of total pageviews). You can check it at Visitors -> Visitors Loyalty -> Depth of Visit where you may find you have visits with zero pageviews. I solved it creating another profile besides the main one and tracking there the events.

      Regards

      Sergi

    4. says

      Hi Carlo,

      The same concepts that I’ve outlined above can be used for almost any video player. The big difference is the implementation. The code that was created above is specific to the chromeless YouTube player. Any other implementation needs to be customized for the player you want to track.

      Thanks for reading and thanks for the comment.

      Justin

    5. Carlo says

      Justin, just so you know, I’m a big fan of your site and you don’t know how much you’ve helped me figure things out with GA, especially coming from WebTrends or Omniture background.

      Thanks!

    6. says

      Hi Justin,

      Great post, I love anything and anybody who actually gets down and illustrate the nitty-gritty parts.
      I did a study among clients and partners not that long ago about WHICH metrics to use when tracking online video and came up with 9 Essential Online Video Metrics.

      http://visualrevenue.com/blog/2008/02/online-video-analytics-kpis.html

      Which is not far from the metrics you collect, with the one difference that being TOO detailed, left some analysts in the dark on WHAT insight to gather from the events tracked (we call them actions, but hey). Therefore we grouped some events to get a more clear set of metrics and a better opportunity for understanding the impact:

      - Online video positive consumption action
      - Online video negative consumption action

      It it of course of the utmost importance that the right events are grouped

      My 2 Euro cents :-)

      Cheers
      d.

      Dennis R. Mortensen, Director of Data Insights at Yahoo!
      Blog: http://visualrevenue.com/blog
      Book: http://visualrevenue.com/blog/yahoo-analytics-book

    7. says

      Dennis,

      Thanks so much for the comment! I am embarrassed to say that I had not read your post until today. I’ve been out of the loop and am slowly creeping back in.

      Your post is fantastic. Your emphasis on actions and insights is right on the money and something I should have emphasized more. It’s great that many of the web 2.0 measurement tools let us define the data we want to collect. As long as we keep actionability in mind we’ll be all set.

      It’s quite flattering that my list of metrics was fairly close to yours. I’m glad we’re all thinking along the same lines especially because we have no formal web 2.0 measurement standards.

      Thanks again for the comment, I appreciate it.

      Justin

    8. says

      Hi Michele,

      The Event reports are in the Content section of Google Analytics. However, event tracking is still in beta. You’ll only see the Event tracking menu if your GA account has been added to the event tracking beta.

      Thanks for the question,

      Justin

    9. says

      Hi again Justin.

      Just wanted to mention that the player works fine in Firefox 3, so I was wrong in my first comment. It was the Firefox browser on that particular pc that had problems.

      I just “had to” make my own YouTube Chromeless player because of your article, you’ll find it implemented in this article.

    10. says

      Hi Elvind,

      Thanks for the followup and thanks for making a chromeless player. It’s this type of innovation that really gets me excited. To see people with real talent, create tools that other people can use, is fantastic.

      Is this player available to everyone? I’d love to use it to post videos on my site. Perhaps you would consider adding some English instructions for other users?

      Thanks again,

      Justin

    11. says

      Hi again.

      I will make an English article shortly about my player, and also make a download available.

      Just finished bug testing with some help today (I’m not a programmer, so I usually just bang my head against the wall until the problem is solved :-)), so the only thing left is to see if I can make the CSS for the player a little bit easier before I write the article.

    12. says

      Thanks again for another great post. I have done a little with YouTube, but haven’t had the time for much more than a few videos showing how Electronic Business Cards impact sales and the like. This encourages me to push forward.

      Brad

    13. Chris says

      Is this needed anymore?

      I noticed that Insights now CLAIMS to show embedded plays as well.

      The reason I’m thinking about using your method is because I have my doubts about the accuracy of the view counts Insights shows me.

      • says

        Hi Chris,

        The Insight data does show embedded plays, and where the video is embedded. But it doesn’t contain any information about how much content was viewed or how the visitor interacted with the player. If you’re using your own video player, and you want to understand how visitors are interacting with the content and player, then event tracking is the way to go. Using Insights is a great way to understand how the community is using the content.

        Thanks for the great comment.

        Justin

    14. says

      Great piece of work here!

      Has anyone tried repurposing this to use with Vimeo? I’m also pondering how one would track vids on external sites using GA, thinking I could regulate distribution of embed code via our partner network as long as it didn’t require too much technical implementation at their end.

      Any thoughts much appreciated.

      Paul

      • says

        @ Anders – In theory this should work on iOS as long as the user does not go into full screen mode. The reason is this player is completely dependent on using the YouTube player. If that player is not run by the browser then none of this tracking will work.

    15. says

      Great piece of sharing Justin . I have uploaded one of my product promotional video to YouTube a week ago and gets viewed more than 5000 times. I really want to track my visitors through Analytics but not find any helpful blog. Your posting solved my issue.

    Trackbacks

    1. [...] Over at Analytics Talk, Justin Cutroni writes about using the Google Analytics Event model to track users viewing and interactions with online video. Using the method Cutroni describes, an embedded YouTube player on a site can capture Google Analytics data not only about which videos users watch, but also their interactions with controls and how long they spend watching each video. [...]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>