July 29, 2008 by Justin Cutroni
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.

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.

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.

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.
<script type='text/javascript'>
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._initData();
pageTracker._trackPageview();
// Create the event tracking object
var ytpEventTracker =
pageTracker._createEventTracker("YouTube Video Player");
var eventLabel;
</script>
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.

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.

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.

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.

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.
Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Subscribe:
19 Comments »