Video Analytics Hacks: A Custom Solution for Tracking Your YouTube Videos

Averaging about 30 million users watching over 1 billion videos per day, YouTube is unarguably the world’s most popular online video delivery platform. Given its unparalleled reach and its almost seamless website integration, it’s no wonder so many businesses use YouTube for video content delivery on their own websites (rather than their own native video players).

However, if you use YouTube to embed videos on your website, you cannot track your videos analytics easily from within Google Analytics or Adobe Analytics, because the content exists on a separate domain. While YouTube’s native analytics platform might give you a decent picture of how engaging your content is, without integrating this information into your own website analytics, it will be difficult to measure how video interactions contribute to engagement and conversion on your own website.

If you use frequently use YouTube as a vehicle for video content delivery on your website, you should treat the videos you post as though they were part of your own domain, rather than coming from a separate platform. In the following post, I describe how to work around the constraints getting analytics from YouTube, using code to collect the data and integrate it into your Google Analytics.

Note: While the method I’ve described reflects how to import this data into Google Analytics specifically, it can be modified for Adobe Analytics as well.

Native APIs Don’t Help Much

YouTube has a suite of APIs (Application Programming Interfaces) that help developers with everything from content management, to content delivery, to content reporting. The YouTube Data API  and the YouTube Analytics API are great for delivering content, but they offer little to no help with integrating into Google Analytics and Adobe Analytics

We have developed a method, however, that lets you get deep insights into video usages from YouTube across all of your owned properties.  And because our solution is JavaScript based, it can be implemented through Tag Management Systems like Google Tag Manager, Adobe’s DTM, and Tealium.

Reaching Into Iframes

The conventional way of adding a YouTube video to a webpage is through an HTML iframe. However, this poses a serious problem for analytics developers because we can’t reach into an iframe that is on a separate domain (in this case youtube.com) to track click events in Google Analytics or Adobe Analytics.

In all fairness to iframes, our inability to detect events is actually a security precaution more than a technical flaw. Cross domain blocking is meant to combat a vulnerability exploit called cross-site scripting (XSS).  The first thing to keep in mind when implementing YouTube tracking is how to get the needed click event access without exposing XSS risks.

For most of our YouTube tracking projects, we add YouTube’s Iframe Player API functionality to a webpage.

Using the Iframe Player API: An Overview

A standard YouTube iframe tag looks like this:

<iframe width="560" height="315" src="https://www.youtube.com/embed/NP5-BXnFnM0?autoplay=1&enablejsapi=1" frameborder="0" allowfullscreen="1" id="widget0"></iframe>

As you can see by this iframe, all the video being played is actually loaded from YouTube and is playing through our webpage.

The Iframe Player API gives us a relatively easy way to video activity and send custom events to Google Analytics – even though the video is on a different domain.  This is a huge breakthrough given the security and technical challenges we face when using iframes.  With our method, we can track video events and send them to Google Analytics with a few lines of code that look something like this:

function playerStateChange(e) {
    var video_data = e.target["getVideoData"](),
        label = video_data.title;

    if (e["data"] == 1 && YT.gtmLastAction == "p") {
        ga('send', 'event', 'videos', 'play', label);
}

As we stated, the code needed to do full Google Analytics YouTube tracking can be added through Google Tag Manager as a Custom HTML tag with an All Pages trigger.

It Works – We Promise

We use the logic we’ve described to develop some custom code for many of our clients and to track the videos on our own website.  With YouTube video tracking, content managers can get insights into their site’s most popular videos, and into optimal video lengths and placements (See Measuring Video Engagement for Digital Video by Chris Meares) We are always happy to discuss video tracking or any facet of digital analytics.  Please contact us if we can help you along your Analytics journey.

Leave a Reply